gpt4 book ai didi

android - 强制 Cloce - 致命异常 main java.lang.nullpointerexception

转载 作者:行者123 更新时间:2023-11-29 23:19:15 24 4
gpt4 key购买 nike

我制作了一个 ListView ,显示数据库中的详细信息。我这里用的是mysql。该应用程序根本不构建,当我调试应用程序时它会强制关闭。这是日志猫:

    12-14 06:15:41.976: E/AndroidRuntime(856): FATAL EXCEPTION: main
12-14 06:15:41.976: E/AndroidRuntime(856): java.lang.NullPointerException
12-14 06:15:41.976: E/AndroidRuntime(856): at com.example.logreg.customer.model.MainActivity.populateListView(MainActivity.java:183)
12-14 06:15:41.976: E/AndroidRuntime(856): at com.example.logreg.customer.model.MainActivity.access$6(MainActivity.java:181)
12-14 06:15:41.976: E/AndroidRuntime(856): at com.example.logreg.customer.model.MainActivity$MainActivityAsync$1.run(MainActivity.java:252)
12-14 06:15:41.976: E/AndroidRuntime(856): at android.app.Activity.runOnUiThread(Activity.java:4644)
12-14 06:15:41.976: E/AndroidRuntime(856): at com.example.logreg.customer.model.MainActivity$MainActivityAsync.onPostExecute(MainActivity.java:249)
12-14 06:15:41.976: E/AndroidRuntime(856): at com.example.logreg.customer.model.MainActivity$MainActivityAsync.onPostExecute(MainActivity.java:1)
12-14 06:15:41.976: E/AndroidRuntime(856): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-14 06:15:41.976: E/AndroidRuntime(856): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-14 06:15:41.976: E/AndroidRuntime(856): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-14 06:15:41.976: E/AndroidRuntime(856): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 06:15:41.976: E/AndroidRuntime(856): at android.os.Looper.loop(Looper.java:137)
12-14 06:15:41.976: E/AndroidRuntime(856): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-14 06:15:41.976: E/AndroidRuntime(856): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 06:15:41.976: E/AndroidRuntime(856): at java.lang.reflect.Method.invoke(Method.java:511)
12-14 06:15:41.976: E/AndroidRuntime(856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-14 06:15:41.976: E/AndroidRuntime(856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-14 06:15:41.976: E/AndroidRuntime(856): at dalvik.system.NativeStart.main(Native Method)

这是主要 Activity :

private class MainActivityAsync extends AsyncTask<String, Void, String>{

@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("retrieving...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}

@Override
protected String doInBackground(String... params) {
if(params[0] == "delete"){
serverRequest.sendGetRequest(ServerRequest.urlDelete+"?kode_cust="+selectedList.getKodeCust().toString());
}else{
/** Mengirimkan request ke server dan memproses JSON response */
String response = serverRequest.sendGetRequest(ServerRequest.urlSelectAll);
list = processResponse(response);
}
return null;
}

@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
populateListView();
}
});
}

}

这是填充 View :

private void populateListView(){
adapter = new ListAdapter(getApplicationContext(), list);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View v, int pos, long id) {
if(actionMode != null){
return false;
}
actionMode = startActionMode(amCallback);
v.setSelected(true);
selectedList = (Customer) adapter.getItem(pos);
return true;
}

});


listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View v, int pos,
long id) {
selectedList = (Customer) adapter.getItem(pos);
Intent in = new Intent(getApplicationContext(), DetailCustomer.class);
in.putExtra("kode_cust", selectedList.getKodeCust().toString());
in.putExtra("nama_cust", selectedList.getNamaCust());
in.putExtra("contact_person_cust", selectedList.getContactPerson());
in.putExtra("alm_cust", selectedList.getAlmtCust());
in.putExtra("wilayah_cust", selectedList.getWilCust());
in.putExtra("telp_cust", selectedList.getTelpCust());
in.putExtra("hp_cust", selectedList.getHpCust());
in.putExtra("fax_cust", selectedList.getFaxCust());
in.putExtra("email_cust", selectedList.getEmailCust());
in.putExtra("limit_cust", selectedList.getLimitCust().toString());

startActivity(in);
}
});
}

我的populateview代码有问题还是有代码我没有输入?因为一开始它可以工作,但是当我在eclipse中的新项目android中制作它后,它显示了上面的问题。

最佳答案

您的代码可以在该方法中抛出空指针异常的唯一方法以及堆栈跟踪,是在调用该方法时 listView 是否为 null。您需要查看初始化 listView 的方式和位置。

该方法的其他部分可能会引发此类异常,但它们位于内部类内部,如果在那里引发异常,那么这些方法将显示在堆栈跟踪中。

关于android - 强制 Cloce - 致命异常 main java.lang.nullpointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27470519/

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