我尝试将ListView创建为设置页面。 第一行是EditText; 2-5行是两个文本。
所以我的Adapter类:
public class SettingsAdapter extends BaseAdapter {
private static final int TYPE_NAME = 0;
private static final int TYPE_SETTING = 1;
private static final int TYPE_MAX_COUNT = 2;
SPMEditActionActivity context;
LayoutInflater inflater;
ArrayList data;
SettingsAdapter(SPMEditActionActivity _context) {
context = _context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
data = new ArrayList();
for(int i=0;i<5;i++)
data.add("Hello");
}
@Override
public int getItemViewType(int position) {
return position==0 ? TYPE_NAME : TYPE_SETTING;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
// пункт списка
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View view = convertView;
if (view == null) {
if(type == TYPE_NAME)
view = inflater.inflate(R.layout.editactionactivity_settings_name, parent, false);
else
view = inflater.inflate(R.layout.editactionactivity_setting, parent, false);
}
return view;
}
}
的活动:
public class SPMEditActionActivity extends Activity {
final int START_TIME_DIALOG = 1;
final int END_TIME_DIALOG = 2;
//==============UI===============================================
private ListView settings_listview ;
private SettingsAdapter settings_adapter;
private Button save_button;
private Button cancel_button;
//==============================================================
//========UI LISTENERS==========================================
private OnClickListener save_button_listener = new OnClickListener() {
public void onClick(View v) {
save();
}
};
private OnClickListener cancel_button_listener = new OnClickListener() {
public void onClick(View v) {
cancel();
}
};
//==============================================================
//==============================================================
//=================LIFECYCLE METHODS============================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spmeditaction);
save_button = (Button) findViewById(R.id.EditActionActivity_save_button);
cancel_button= (Button) findViewById(R.id.EditActionActivity_cancel_button);
settings_adapter = new SettingsAdapter(SPMEditActionActivity.this);
settings_listview.setAdapter( settings_adapter );
save_button.setOnClickListener(save_button_listener);
cancel_button.setOnClickListener(cancel_button_listener);
}
//==============================================================
private void save(){
finish();
}
private void cancel(){
finish();
}
}
在活动中,当我设置适配器
时settings_adapter = new SettingsAdapter(this);
settings_listview.setAdapter( settings_adapter );
我的应用程序因NullPointerException而崩溃。
任何人都可以向我解释,我做错了吗?
PS。我用这个tutorial
LogCat输出:
04-18 15:59:19.982: E/AndroidRuntime(24150): FATAL EXCEPTION: main
04-18 15:59:19.982: E/AndroidRuntime(24150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hemul.soundprofilemanager/com.hemul.soundprofilemanager.SPMEditActionActivity}: java.lang.NullPointerException
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.os.Looper.loop(Looper.java:137)
04-18 15:59:19.982:E/AndroidRuntime(24150): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 15:59:19.982: E/AndroidRuntime(24150): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 15:59:19.982: E/AndroidRuntime(24150): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 15:59:19.982: E/AndroidRuntime(24150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 15:59:19.982: E/AndroidRuntime(24150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 15:59:19.982: E/AndroidRuntime(24150): at dalvik.system.NativeStart.main(Native Method)
04-18 15:59:19.982: E/AndroidRuntime(24150): Caused by: java.lang.NullPointerException
04-18 15:59:19.982: E/AndroidRuntime(24150): at com.hemul.soundprofilemanager.SPMEditActionActivity.onCreate(SPMEditActionActivity.java:111)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.app.Activity.performCreate(Activity.java:5008)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-18 15:59:19.982: E/AndroidRuntime(24150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-18 15:59:19.982: E/AndroidRuntime(24150): ... 11 more
答案 0 :(得分:1)
您的settings_listview
为空,因为您尚未在onCreate中指定对此的引用
从xml中获取你的listview引用
settings_listview=(ListView)findViewById(R.id.yourlistview); <-- your listview id in xml
在setContentView(R.layout.activity_spmeditaction);
之后