gpt4 book ai didi

java - 从 onClickListener 调用新方法时返回 Null

转载 作者:行者123 更新时间:2023-12-02 11:06:12 25 4
gpt4 key购买 nike

我已经搜索了很多,我认为这个问题与上下文有关,但我仍然在挣扎(编程新手)。问题如下:

我有一个将我的 sqlite 数据库同步到外部数据库的方法,当下面的方法存在于带有按钮的类中时,该方法可以正常工作,但我需要在其自己的名为 SyncToMYSQL 的类中使用此方法,如下所示。 .

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();
}
}
}

从我创建的另一个 Activity 中调用它的按钮,如下所示:

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

这会返回一个错误

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)

最佳答案

尝试将 syncToMYSQL 定义为 private final SyncToMYSQLsyncToMYSQL;

关于java - 从 onClickListener 调用新方法时返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39686981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com