我的MainActivity文件执行其他一些任务。 AccountInfo用于生成我的列表。
public class AccountInfo extends Activity {
TextView sublistHeadtext;
ImageView sublistHeadImage;
ListView listItem;
String[] ItemViewNames;
AccInfoCustomAdapter adapter;
public ArrayList<String> CustomListViewValuesArr = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub_list);
int value = getIntent().getExtras().getInt("listpoition");
sublistHeadtext = (TextView) findViewById(R.id.ListItemtext);
sublistHeadtext.setText("Account Information");
sublistHeadImage = (ImageView) findViewById(R.id.ListItemimage);
sublistHeadImage.setImageResource(R.drawable.accinfoimg);
listItem = (ListView) findViewById(R.id.listItem);
ItemViewNames = getResources().getStringArray(R.array.accInfoList);
Log.wtf("AA", "executed !");
/******** Take some data in Arraylist ( CustomListViewValuesArr ) ***********/
setListData();
/**************** Create Custom Adapter *********/
adapter = new AccInfoCustomAdapter(CustomListViewValuesArr,
getBaseContext());
//adapter = new AccInfoCustomAdapter(CustomListViewValuesArr,AccountInfo.this);
Log.wtf("AA", "arrayList : "+CustomListViewValuesArr);
listItem.setAdapter(adapter);
}
/****** Function to set data in ArrayList *************/
public void setListData() {
for (int i = 0; i < ItemViewNames.length; i++)
CustomListViewValuesArr.add(ItemViewNames[i]);
}
}
AdatperFile
公共类AccInfoCustomAdapter扩展了BaseAdapter实现 OnClickListener {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public AccInfoCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
// return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.sublist_item, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.ListItemtext);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
ImageButton nextBtn = (ImageButton)view.findViewById(R.id.ListItemimgbtn);
nextBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(context, "position : "+position, Toast.LENGTH_LONG).show();
}
});
return view;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
我在行
上得到一个空指针异常listItem.setAdapter(适配器);
我认为这里存在错误
adapter = new AccInfoCustomAdapter (CustomListViewValuesArr, getBaseContext());
LOGCAT: -
02-18 18:16:46.170: E/AndroidRuntime(16303): FATAL EXCEPTION: main
02-18 18:16:46.170: E/AndroidRuntime(16303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xyzbank/com.example.xyzbank.AccountInfo}: java.lang.NullPointerException
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2085)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.os.Looper.loop(Looper.java:137)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.ActivityThread.main(ActivityThread.java:4747)
02-18 18:16:46.170: E/AndroidRuntime(16303): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 18:16:46.170: E/AndroidRuntime(16303): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 18:16:46.170: E/AndroidRuntime(16303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 18:16:46.170: E/AndroidRuntime(16303): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 18:16:46.170: E/AndroidRuntime(16303): at dalvik.system.NativeStart.main(Native Method)
02-18 18:16:46.170: E/AndroidRuntime(16303): Caused by: java.lang.NullPointerException
02-18 18:16:46.170: E/AndroidRuntime(16303): at com.example.xyzbank.AccountInfo.onCreate(AccountInfo.java:45)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.Activity.performCreate(Activity.java:5008)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 18:16:46.170: E/AndroidRuntime(16303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
02-18 18:16:46.170: E/AndroidRuntime(16303): ... 11 more
答案 0 :(得分:0)
尝试将getBaseContext()更改为“this”。