我想將項目添加到微調框,但出現錯誤「不幸的是,我的應用程序已停止工作」。我在strings.xml中創建了一個字符串數組,其中我有一些項目。當用戶從MainActivity中選擇添加項目時,會出現一個帶有按鈕的editText,並且我想要將editText中的任何內容傳遞給微調器。微調器位於activity_main.xml中,我正在處理addactivity.xml。我如何將新項目從EditText(在addactivity.xml和AddActivity.java中)傳遞給MainActivity.java和activity_main.xml中的微調器?將項目添加到微調框
這裏是我的代碼:
public class AddActivity extends Activity{
Spinner spnr1;
EditText edtxAddActivity;
ArrayAdapter<String> spinnerAdapter;
Button btnAddActivity;
String[] actArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addactivity);
addActivityButton();
}
public void addActivityButton() {
// TODO Auto-generated method stub
edtxAddActivity = (EditText) findViewById(R.id.etAddActivity);
spnr1 = (Spinner) findViewById(R.id.spinner1);
actArray = getResources().getStringArray(R.array.activities_array);
spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,actArray);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnr1.setAdapter(spinnerAdapter);
btnAddActivity= (Button) findViewById(R.id.btAddActivity);
btnAddActivity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String newactivity = "" + edtxAddActivity.getText().toString();
spinnerAdapter.add(newactivity);
spinnerAdapter.notifyDataSetChanged();
}
});
}
}
這是我的strings.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">MyAdroidApp</string>
<string name="activity_prompt">Choose an activity</string>
<string-array name="activities_array">
<item>5-a-side</item>
<item>Football</item>
<item>Basketball</item>
<item>Table Tennis</item>
<item>Add</item>
</string-array>
顯示的錯誤如下。
07-24 14:08:43.397: E/AndroidRuntime(24798): FATAL EXCEPTION: main
07-24 14:08:43.397: E/AndroidRuntime(24798): Process: com.example.myAndroidApp, PID: 24798
07-24 14:08:43.397: E/AndroidRuntime(24798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myAndroidApp/com.example.myAndroidApp.AddActivity}: java.lang.NullPointerException
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.os.Handler.dispatchMessage(Handler.java:102)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.os.Looper.loop(Looper.java:136)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-24 14:08:43.397: E/AndroidRuntime(24798): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 14:08:43.397: E/AndroidRuntime(24798): at java.lang.reflect.Method.invoke(Method.java:515)
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-24 14:08:43.397: E/AndroidRuntime(24798): at dalvik.system.NativeStart.main(Native Method)
07-24 14:08:43.397: E/AndroidRuntime(24798): Caused by: java.lang.NullPointerException
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.example.myAndroidApp.AddActivity.addActivityButton(AddActivity.java:46)
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.example.socialactivities.AddActivity.onCreate(AddActivity.java:33)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.Activity.performCreate(Activity.java:5231)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-24 14:08:43.397: E/AndroidRuntime(24798): ... 11 more
的問題是,spinnerAdapter產生一個NullPointerException,因爲列表視圖可能是空的或不初始化。我怎樣才能初始化我的數組?我的字符串數組在值中是在array.xml中。在com.example.myAndroidApp.AddActivity.addActivityButton(AddActivity.java:46)
是行56這一個:String newactivity =「」+ edtxAddActivity.getText()。toString();?也許你試圖設置適配器兩次,將spnr1.setAdapter(spinnerAdapter)放在spnr1 = findViewById(R.id.spinner1) – Sherekan
以下如果我這樣做,我的應用程序甚至不能進入頁面AddActivity。它說「不幸......」。 – user3561214
什麼是第56行? – Sherekan