package org.example.mbtiapplication;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MBTITest extends Activity implements OnItemSelectedListener {
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1);
Spinner secondSpinner = (Spinner) findViewById(R.id.spinner2);
Spinner thirdSpinner = (Spinner) findViewById(R.id.spinner3);
Spinner fourthSpinner = (Spinner) findViewById(R.id.spinner4);
ArrayList<String> Evaluator = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mbtitest);
addItemsOnSpinner1();
addItemsOnSpinner2();
addItemsOnSpinner3();
addItemsOnSpinner4();
}
public void addItemsOnSpinner1(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
public void addItemsOnSpinner2(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner2);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
public void addItemsOnSpinner3(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner3);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
public void addItemsOnSpinner4(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner4);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
long arg3) {
switch (parent.getId()){
case R.id.spinner1:
String firstAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.add(firstAnswer);
case R.id.spinner2:
String secondAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.add(secondAnswer);
case R.id.spinner3:
String thirdAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.add(thirdAnswer);
case R.id.spinner4:
String fourthAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.add(fourthAnswer);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
堆栈跟踪:
11-24 14:06:55.778: E/AndroidRuntime(1842): FATAL EXCEPTION: main
11-24 14:06:55.778: E/AndroidRuntime(1842): Process: org.example.mbtiapplication, PID: 1842
11-24 14:06:55.778: E/AndroidRuntime(1842): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.mbtiapplication/org.example.mbtiapplication.MBTITest}: java.lang.NullPointerException
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.os.Handler.dispatchMessage(Handler.java:102)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.os.Looper.loop(Looper.java:137)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-24 14:06:55.778: E/AndroidRuntime(1842): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 14:06:55.778: E/AndroidRuntime(1842): at java.lang.reflect.Method.invoke(Method.java:515)
11-24 14:06:55.778: E/AndroidRuntime(1842): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-24 14:06:55.778: E/AndroidRuntime(1842): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-24 14:06:55.778: E/AndroidRuntime(1842): at dalvik.system.NativeStart.main(Native Method)
11-24 14:06:55.778: E/AndroidRuntime(1842): Caused by: java.lang.NullPointerException
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.Activity.findViewById(Activity.java:1883)
11-24 14:06:55.778: E/AndroidRuntime(1842): at org.example.mbtiapplication.MBTITest.<init>(MBTITest.java:15)
11-24 14:06:55.778: E/AndroidRuntime(1842): at java.lang.Class.newInstanceImpl(Native Method)
11-24 14:06:55.778: E/AndroidRuntime(1842): at java.lang.Class.newInstance(Class.java:1208)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-24 14:06:55.778: E/AndroidRuntime(1842): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
11-24 14:06:55.778: E/AndroidRuntime(1842): ... 11 more
当我按下我的按钮开始我的新活动时,我会在我的日志中找到这个并且错误地告诉我我的应用程序已停止。我不确定如何在做了一些研究之后如何解决这个问题,并且想知道如何解决这个问题。谢谢
答案 0 :(得分:1)
在onCreate
之后的setContentView
中移动初始化,如下所示
Spinner firstSpinner ;
Spinner secondSpinner ;
Spinner thirdSpinner;
Spinner fourthSpinner ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mbtitest);
// initialize after setting the layout to the activity
firstSpinner = (Spinner) findViewById(R.id.spinner1);
secondSpinner = (Spinner) findViewById(R.id.spinner2);
thirdSpinner = (Spinner) findViewById(R.id.spinner3);
fourthSpinner = (Spinner) findViewById(R.id.spinner4);
... //rest of the code
}
此外,无需初始化微调器再次移除addItemsOnSpinner2()
中的微调器初始化,只需使用firstSpinner,因为它是一个类存储器。其他人也一样。
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner2);