我正在尝试向微调器添加项目,但我收到错误“不幸的是我的应用程序已停止工作”。我在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,因为listview可能是空的或未初始化。如何初始化我的数组?我的字符串数组在array.xml中的值。
答案 0 :(得分:0)
com.example.myAndroidApp.AddActivity.addActivityButton(AddActivity.java:46)中的NullPointerException
意味着您正在引用并使用当时为null的内容。从它的外观来看,它可能是你的观点参考之一。我的建议是在每行代码中放置一些调试检查点并执行应用程序,然后逐步确定什么是null
答案 1 :(得分:0)
看看R.layout.addactivity布局,你会错过一些东西,尤其是我认为findViewById(R.id.etAddActivity);是null,意味着你没有布局或smtg是错误的namie,id左右