扩展应用程序示例导致程序崩溃如何?

时间:2014-10-17 18:08:52

标签: java android eclipse android-intent nullpointerexception

有什么想法吗?

public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener{
    TextView txv=(TextView)findViewById(R.id.textView1);
    Spinner spinner1=(Spinner)findViewById(R.id.spinner1);
    RadioGroup rdg=(RadioGroup)findViewById(R.id.radioGroup1);
    String msg="";
    String[] way1={"商務車廂","經濟車廂","自由座"};
    String[] way2={"商務艙","頭等艙","經濟艙"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rdg.setOnCheckedChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(RadioGroup radio0, int radio1) {
        // TODO Auto-generated method stub      
        if (radio0.getCheckedRadioButtonId()==R.id.radio0){
            msg="高鐵";
            ArrayAdapter<String> tempAd =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,way1);
            tempAd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(tempAd);
        }
        else{
            msg="飛機";
            ArrayAdapter<String> tempAd =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,way2);
            tempAd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(tempAd);
        }
    }

    public void buy(View v){
        msg=(msg+"-"+spinner1.getSelectedItem());
        txv.setText("XXX您好,您訂購的項目為:"+msg);
    }

以下是logcat消息:

10-17 17:48:47.224: E/AndroidRuntime(720): FATAL EXCEPTION: main
10-17 17:48:47.224: E/AndroidRuntime(720): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.asd/com.example.asd.MainActivity}: java.lang.NullPointerException
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.os.Looper.loop(Looper.java:137)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-17 17:48:47.224: E/AndroidRuntime(720):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 17:48:47.224: E/AndroidRuntime(720):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 17:48:47.224: E/AndroidRuntime(720):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-17 17:48:47.224: E/AndroidRuntime(720):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-17 17:48:47.224: E/AndroidRuntime(720):  at dalvik.system.NativeStart.main(Native Method)
10-17 17:48:47.224: E/AndroidRuntime(720): Caused by: java.lang.NullPointerException
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.Activity.findViewById(Activity.java:1839)
10-17 17:48:47.224: E/AndroidRuntime(720):  at com.example.asd.MainActivity.<init>(MainActivity.java:17)
10-17 17:48:47.224: E/AndroidRuntime(720):  at java.lang.Class.newInstanceImpl(Native Method)
10-17 17:48:47.224: E/AndroidRuntime(720):  at java.lang.Class.newInstance(Class.java:1319)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
10-17 17:48:47.224: E/AndroidRuntime(720):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
10-17 17:48:47.224: E/AndroidRuntime(720):  ... 11 more

1 个答案:

答案 0 :(得分:2)

嗯,它是构造函数中抛出的NullPointerException。

你不应该在那里做findViewById。在致电onCreate()后,您应该在setContentView()中执行此操作。