2016-09-25 5 views
2

Ich habe ziemlich abit gesucht und ich denke, das Problem hat etwas mit Kontext zu tun, aber ich kämpfe immer noch (neu in der Programmierung). Das Problem ist wie folgt:Null zurückgeben, wenn ich eine neue Methode von onClickListener anrufe

Ich habe eine Methode, die meine sqlite db zu einer externen db synchronisiert, die gut funktioniert, wenn diese Methode in der Klasse mit der Schaltfläche vorhanden ist, aber ich muss diese Methode in einer eigenen Klasse verwenden genannt SyncToMYSQL wie unten zu sehen ...

public class SyncToMYSQL { 

DowncroftDatabase downcroftDatabase; 
ProgressDialog prgDialog; 

public void syncSQLiteMySQLDB() { 
    //Create AsycHttpClient object 
    AsyncHttpClient client = new AsyncHttpClient(); 
    RequestParams params = new RequestParams(); 
    ArrayList<HashMap<String, String>> userList = downcroftDatabase.getAllUsers(); 
    if (userList.size() != 0) { 
     if (downcroftDatabase.dbSyncCount() != 0) { 
      prgDialog.show(); 
      params.put("usersJSON", downcroftDatabase.composeJSONfromSQLite()); 
      client.post("http://jakebreen.co.uk/android/sqlitemysqlsync/inseruser.php", params, new AsyncHttpResponseHandler() { 
       @Override 
       public void onSuccess(int statusCode, Header[] headers, byte[] response) { 
        System.out.println(response); 
        prgDialog.hide(); 
        try { 
         JSONArray arr = new JSONArray(new String(response)); //new JSONArray(new String(response)); 
         System.out.println(arr.length()); 
         for (int i = 0; i < arr.length(); i++) { 
          JSONObject obj = (JSONObject) arr.get(i); 
          System.out.println(obj.get("id")); 
          System.out.println(obj.get("status")); 
          downcroftDatabase.updateSyncStatus(obj.get("id").toString(), obj.get("status").toString()); 
         } 
         //Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show(); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         //Toast.makeText(getBaseContext(), "DB Sync completed!", Toast.LENGTH_LONG).show(); //Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show(); 
         e.printStackTrace(); 
        } 
       } 

       @Override 
       public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 

        // TODO Auto-generated method stub 
        prgDialog.hide(); 
        if (statusCode == 404) { 
         //Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show(); 
        } else if (statusCode == 500) { 
         //Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show(); 
        } else { 
         //Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 
     } else { 
      //Toast.makeText(getApplicationContext(), "SQLite and Remote MySQL DBs are in Sync!", Toast.LENGTH_LONG).show(); 
     } 
    } else { 
     //Toast.makeText(getApplicationContext(), "No data in SQLite DB, please do enter User name to perform Sync action", Toast.LENGTH_LONG).show(); 
    } 
} 
} 

die Schaltfläche, die es von einer anderen Aktivität ruft ich wie so erstellt habe:

public void SyncButton() { 
    btn_syncTo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      syncToMYSQL.syncSQLiteMySQLDB(); 
     } 
    }); 
} 

dies einen Fehler von

kehrt
E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.year2.dck, PID: 2492 
       java.lang.NullPointerException: Attempt to invoke virtual method 'void com.year2.dck.SyncToMYSQL.syncSQLiteMySQLDB()' on a null object reference 
        at com.year2.dck.LoginActivity$5.onClick(LoginActivity.java:157) 
        at android.view.View.performClick(View.java:5198) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+1

Sie sind nicht eine Instanz des Objekts ‚SyncToMYSQL‘ definieren –

+0

es definiert wurde, wie folgt zu definieren SyncToMYSQL syncToMYSQL; –

+0

SyncToMySQL syncToMySQL = neu SyncTomySQL(); –

Antwort

0

Try syncToMYSQL als private final SyncToMYSQL syncToMYSQL;

+0

kein Glück. oh liebes :( –

0

ändern diese Methode syncSQLiteMySQLDB auf statische Methode und versuchen,

SyncToMYSQL.syncSQLiteMySQLDB(); 
+0

das hilft nicht ... vielleicht können Sie einen Blick auf die Projektdatei? –

+0

ja bitte ..... – Bob

+0

E-Mail? Github?? –

Verwandte Themen