我一直在为我的一个作业创建一个 Android 应用程序,我遇到了这个问题。我正在制作一个动态填充自身的微调器,但是应用程序似乎没有打开,我似乎无法找到问题所在。任何帮助或提示将不胜感激!
当您按下按钮创建集时,应用程序基本上会创建集,它会采用在 EditText 中输入的字符串,并使用从 EditText 中获取的字符串动态填充微调器。
基本上我的活动类是这样的:
public class MainActivity extends Activity
{
public final static String DISPLAY_MESSAGE = "com.android.setoperations.MESSAGE";
private MySet mySet;
private MySet set_list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
this.mySet = new MySet();
Spinner set_spinner = (Spinner) findViewById(R.id.set_spinner);
List<String> list = this.mySet.set_list;
ArrayAdapter<String> setNamesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
set_spinner.setAdapter(setNamesAdapter);
}
public void createSet(View view){
EditText editText = (EditText) findViewById(R.id.element);
String element = editText.getText().toString();
this.mySet.createSet(element);
}
}
此外,我在 main_activity.xml 布局中保存集的微调器和按钮如下所示:
<Spinner
android:id="@+id/set_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Set"
android:onClick="createSet"/>
最后,我的操作类 (MySet.java) 如下所示:
ArrayList<String> set_list = new ArrayList<String>();
public void createSet(String set_name){
set_list.add(set_name);
}
谢谢!应用程序将尝试运行,然后自行关闭并停止。
这是我的 logcat 信息:
09-21 20:08:18.247: E/AndroidRuntime(1070): FATAL EXCEPTION: main
09-21 20:08:18.247: E/AndroidRuntime(1070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.setoperations/com.android.setoperations.MainActivity}: java.lang.NullPointerException
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.os.Looper.loop(Looper.java:137)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-21 20:08:18.247: E/AndroidRuntime(1070): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 20:08:18.247: E/AndroidRuntime(1070): at java.lang.reflect.Method.invoke(Method.java:525)
09-21 20:08:18.247: E/AndroidRuntime(1070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-21 20:08:18.247: E/AndroidRuntime(1070): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-21 20:08:18.247: E/AndroidRuntime(1070): at dalvik.system.NativeStart.main(Native Method)
09-21 20:08:18.247: E/AndroidRuntime(1070): Caused by: java.lang.NullPointerException
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:114)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.widget.Spinner.setAdapter(Spinner.java:382)
09-21 20:08:18.247: E/AndroidRuntime(1070): at com.android.setoperations.MainActivity.onCreate(MainActivity.java:32)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.Activity.performCreate(Activity.java:5133)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-21 20:08:18.247: E/AndroidRuntime(1070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-21 20:08:18.247: E/AndroidRuntime(1070): ... 11 more