我正在尝试从列表视图中获取值并将其设置为共享首选项并开始新活动。
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
TextView textViewItem = ((TextView) v.findViewById(R.id.listItem));
// get the clicked item name
String listItemText = textViewItem.getText().toString();
//set shared preferences to kasnaam
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MijnKassen.this);
Editor edit = sp.edit();
edit.putString("kasnaam", listItemText);
edit.commit();
Intent intent = new Intent(MijnKassen.this, Kas.class);
startActivity(intent);
}
});
直到我想开始一项活动。 我的logcat输出是:
06-02 20:32:25.028: W/dalvikvm(21683): threadid=1: thread exiting with uncaught exception (group=0x41674e18)
06-02 20:32:25.038: E/AndroidRuntime(21683): FATAL EXCEPTION: main
06-02 20:32:25.038: E/AndroidRuntime(21683): Process: com.example.testv3, PID: 21683
06-02 20:32:25.038: E/AndroidRuntime(21683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testv3/com.example.testv3.Kas}: java.lang.NullPointerException
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.ActivityThread.access$800(ActivityThread.java:156)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.os.Handler.dispatchMessage(Handler.java:102)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.os.Looper.loop(Looper.java:157)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.ActivityThread.main(ActivityThread.java:5872)
06-02 20:32:25.038: E/AndroidRuntime(21683): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 20:32:25.038: E/AndroidRuntime(21683): at java.lang.reflect.Method.invoke(Method.java:515)
06-02 20:32:25.038: E/AndroidRuntime(21683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
06-02 20:32:25.038: E/AndroidRuntime(21683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
06-02 20:32:25.038: E/AndroidRuntime(21683): at dalvik.system.NativeStart.main(Native Method)
06-02 20:32:25.038: E/AndroidRuntime(21683): Caused by: java.lang.NullPointerException
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
06-02 20:32:25.038: E/AndroidRuntime(21683): at com.example.testv3.Kas.onCreate(Kas.java:88)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.Activity.performCreate(Activity.java:5312)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
06-02 20:32:25.038: E/AndroidRuntime(21683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
06-02 20:32:25.038: E/AndroidRuntime(21683): ... 11 more
答案 0 :(得分:0)
我需要声明我的列表视图,这是我工作代码的一部分。
ListView listView = (ListView) findViewById(R.id.list);
SimpleAdapter adapter = new SimpleAdapter(MijnKassen.this, mKassenList, R.layout.list_layout, new String[] {TAG_KASNAAM}, new int[]{ R.id.listItem });
// I shouldn't have to comment on this one:
listView.setAdapter(adapter);
// Optional: when the user clicks a list item we
//could do something. However, we will choose
//to do nothing...
//ListView lv = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
TextView textViewItem = ((TextView) v.findViewById(R.id.listItem));
// get the clicked item name
String listItemText = textViewItem.getText().toString();
//set shared preferences to kasnaam
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MijnKassen.this);
Editor edit = sp.edit();
edit.putString("kasnaam", listItemText);
edit.commit();
Intent intent = new Intent(MijnKassen.this, Kas.class);
startActivity(intent);
}
});