0
我gettin NullPointerException「lv.setAdapter(適配器);」我不明白,我在適配器中更改了一些代碼,但隨後我刪除了它們。然後這個例外就開始了。自定義ListView適配器 - 空指針異常
在kimdenlistduzeltilmis:4值
在konulist:4值
final LayoutInflater mLInflater = getLayoutInflater();
final ListViewAdapter adapter = new ListViewAdapter(
getApplicationContext(), kimdenlistduzeltilmis, konulist,
mLInflater);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
// adapter.Refresh();
onClickPosition = position;
try {
EkDurumu ekdurumuNesnesi = new EkDurumu();
ekdurumuNesnesi.MessagePositionEkBulunuyormu(
onClickPosition, goruntuleNesnesi.getMessages());
} catch (Exception e) {
e.printStackTrace();
}
而且ListViewAdapter:
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class ListViewAdapter extends BaseAdapter {
static HashMap<Integer, Boolean> cartItems = new HashMap<Integer, Boolean>();
Context mContext;
ArrayList<String> kimdenlist; // to load images
ArrayList<String> konulist; // for data
// ArrayList<String> imagelist;
LayoutInflater mLayoutInflater;
public ListViewAdapter(Context context, ArrayList<String> kimdenlist, ArrayList<String> konulist,
LayoutInflater layoutInflater) {
mContext = context;
this.kimdenlist = kimdenlist;
this.konulist = konulist;
mLayoutInflater = layoutInflater;
}
@Override
public int getCount()
{
return konulist.size(); // images array length
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
int count = 0;
// customized Listview
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
View v;
final int pos = position;
v = mLayoutInflater.inflate(R.layout.listust, null);
TextView kimden = (TextView) v.findViewById(R.id.textvKimden);
kimden.setText(kimdenlist.get(position));
TextView konu = (TextView) v.findViewById(R.id.textvKonu);
konu.setText(konulist.get(position));
// // saving check box state at the time of raloading
CheckBox ch = (CheckBox) v.findViewById(R.id.chk);
try {
if (count != 0) {
boolean b = cartItems.get(pos);
if (b == false)
ch.setChecked(false);
else
ch.setChecked(true);
}
} catch (NullPointerException e) {
}
ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
cartItems.put(pos, arg1);
count++;
}
});
return v;
}
public static HashMap<Integer, Boolean> getcartItems() {
return cartItems;
}
public void Remove()
{
notifyDataSetChanged();
}
}
而我的logcat:
05-16 16:13:12.962: E/AndroidRuntime(347): FATAL EXCEPTION: main
05-16 16:13:12.962: E/AndroidRuntime(347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobil.eposta/com.mobil.eposta.ListeleActivity}: java.lang.NullPointerException
05-16 16:13:12.962: E/AndroidRuntime(347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.os.Looper.loop(Looper.java:123)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 16:13:12.962: E/AndroidRuntime(347): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 16:13:12.962: E/AndroidRuntime(347): at java.lang.reflect.Method.invoke(Method.java:521)
05-16 16:13:12.962: E/AndroidRuntime(347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 16:13:12.962: E/AndroidRuntime(347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 16:13:12.962: E/AndroidRuntime(347): at dalvik.system.NativeStart.main(Native Method)
05-16 16:13:12.962: E/AndroidRuntime(347): Caused by: java.lang.NullPointerException
05-16 16:13:12.962: E/AndroidRuntime(347): at com.mobil.eposta.ListeleActivity.onCreate(ListeleActivity.java:109)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-16 16:13:12.962: E/AndroidRuntime(347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-16 16:13:12.962: E/AndroidRuntime(347): ... 11 more
通'this',而不是'getApplicationContext()'.. – Ronnie
哪裏是列表視圖LV – Akram