我是新手程序员。编写程序并遇到错误。搜索互联网,但没有找到。可能是什么问题呢?请帮帮我。 这是代码。
public void onAddExpenseClick(View viev){
final String[] mChoose = getResources().getStringArray(R.array.mChoose);
Dialog dialog = new Dialog(HistoryActivity.this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mChoose);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
dialog.setContentView(R.layout.expense_dialog);
dialog.show();
spinner.setPrompt("Title");
spinner.setSelection(2);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getBaseContext(), "Position = " + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
这里是Log
04-09 02:33:37.512: E/AndroidRuntime(798): FATAL EXCEPTION: main
04-09 02:33:37.512: E/AndroidRuntime(798): java.lang.IllegalStateException: Could not execute method of the activity
04-09 02:33:37.512: E/AndroidRuntime(798): at android.view.View$1.onClick(View.java:2144)
04-09 02:33:37.512: E/AndroidRuntime(798): at android.view.View.performClick(View.java:2485)
04-09 02:33:37.512: E/AndroidRuntime(798): at android.view.View$PerformClick.run(View.java:9080)
04-09 02:33:37.512: E/AndroidRuntime(798): at android.os.Handler.handleCallback(Handler.java:587)
04-09 02:33:37.512: E/AndroidRuntime(798): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 02:33:37.512: E/AndroidRuntime(798): at android.os.Looper.loop(Looper.java:123)
04-09 02:33:37.512: E/AndroidRuntime(798): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-09 02:33:37.512: E/AndroidRuntime(798): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 02:33:37.512: E/AndroidRuntime(798): at java.lang.reflect.Method.invoke(Method.java:507)
04-09 02:33:37.512: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-09 02:33:37.512: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-09 02:33:37.512: E/AndroidRuntime(798): at dalvik.system.NativeStart.main(Native Method)
04-09 02:33:37.512: E/AndroidRuntime(798): Caused by: java.lang.reflect.InvocationTargetException
04-09 02:33:37.512: E/AndroidRuntime(798): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 02:33:37.512: E/AndroidRuntime(798): at java.lang.reflect.Method.invoke(Method.java:507)
04-09 02:33:37.512: E/AndroidRuntime(798): at android.view.View$1.onClick(View.java:2139)
04-09 02:33:37.512: E/AndroidRuntime(798): ... 11 more
04-09 02:33:37.512: E/AndroidRuntime(798): Caused by: java.lang.NullPointerException
04-09 02:33:37.512: E/AndroidRuntime(798): at com.droidbrew.travelcheap.HistoryActivity.onAddExpenseClick(HistoryActivity.java:150)
答案 0 :(得分:1)
确保您的布局中有R.id.spinner
。
findViewById(R.id.spinner)
必须返回null,因为它无法找到它并且在下一步尝试setAdapter时会获得NPE。
我们可以确定它不是adapter
变量,因为你会在149行获得NPE。