0

单击 button1 后立即出现空指针异常错误。但我不确定路径原因是什么。有人可以帮助我吗?

public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new ButtonClickListener());
}
class ButtonClickListener implements OnClickListener {
    public void onClick(View v){
        EditText name = (EditText)findViewById(R.id.name);
        EditText address = (EditText)findViewById(R.id.address);

        Spinner month = (Spinner)findViewById(R.id.month);
        Spinner day = (Spinner)findViewById(R.id.day);

        RadioGroup radio = (RadioGroup)findViewById(R.id.gendar);
        RadioButton radiobutton=
                (RadioButton)findViewById(radio.getCheckedRadioButtonId());

        CheckBox applecheck = (CheckBox)findViewById(R.id.applecheck);
        CheckBox orangecheck = (CheckBox)findViewById(R.id.orangecheck);
        CheckBox peachcheck = (CheckBox)findViewById(R.id.peachcheck);

        EditText appleqty = (EditText)findViewById(R.id.appleqty);
        EditText orangeqty = (EditText)findViewById(R.id.orangeqty);
        EditText peachqty = (EditText)findViewById(R.id.peachqty);

        Intent intent = new Intent(FirstActivity.this,SecondActivity.class);

        intent.putExtra("NAME", name.getText().toString());
        intent.putExtra("ADDRESS", address.getText().toString());
        intent.putExtra("MONTH",month.getSelectedItem().toString());
        intent.putExtra("DAY", day.getSelectedItem().toString());
        intent.putExtra("GENDAR", radiobutton.getText().toString());
        if(applecheck.isChecked())
            intent.putExtra("APPLE", appleqty.getText().toString());
        if(orangecheck.isChecked())
            intent.putExtra("ORANGE", orangeqty.getText().toString());
        if(peachcheck.isChecked())
            intent.putExtra("PEACH", peachqty.getText().toString());

        startActivity(intent);

    }
}

}

public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    Bundle extra= getIntent().getExtras();

    String name = extra.getString("NAME");
    String address = extra.getString("ADDRESS");
    String month = extra.getString("MONTH");
    String day = extra.getString("DAY");
    String gendar = extra.getString("GENDAR");
    String apple = extra.getString("APPLE");
    String orange = extra.getString("ORANGE");
    String peach = extra.getString("PEACH");

11-11 14:38:38.681:E/AndroidRuntime(363):致命异常:主要 11-11 14:38:38.681:E/AndroidRuntime(363):java.lang.NullPointerException 11-11 14:38:38.681: E/AndroidRuntime(363): 在 com.example.sample4_3.FirstActivity$ButtonClickListener.onClick(FirstActivity.java:34) 11-11 14:38:38.681: E/AndroidRuntime(363): 在 android.view.View.performClick (View.java:2408) 11-11 14:38:38.681: E/AndroidRuntime(363): 在 android.view.View$PerformClick.run(View.java:8816) 11-11 14:38:38.681: E /AndroidRuntime(363): 在 android.os.Handler.handleCallback(Handler.java:587) 11-11 14:38:38.681: E/AndroidRuntime(363): 在 android.os.Handler.dispatchMessage(Handler.java: 92) 11-11 14:38:38.681: E/AndroidRuntime(363): 在 android.os.Looper.loop(Looper.java:123) 11-11 14:38:38.681: E/AndroidRuntime(363): 在android.app.ActivityThread.main(ActivityThread.java:4627) 11-11 14:38:38.681: E/AndroidRuntime(363): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-11 14:38:38.681: E/AndroidRuntime(363): 在 java .lang.reflect.Method.invoke(Method.java:521) 11-11 14:38:38.681: E/AndroidRuntime(363): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 868)

我遇到的新错误如下;11-11 16:24:39.680: E/AndroidRuntime(554): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.sample4_3/com.example.sample4_3.SecondActivity}: android.view.InflateException: 二进制XML 文件第 126 行:膨胀类 textView 11-11 16:24:39.680 时出错:E/AndroidRuntime(554):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 11-11 16:24:39.680: E/AndroidRuntime(554): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-11 16:24:39.680: E/AndroidRuntime(554): 在 android.app.ActivityThread.access$2300(ActivityThread. java:125) 11-11 16:24:39.680: E/AndroidRuntime(554): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-11 16:24:39.680: E/AndroidRuntime( 554):在 android.os.Handler.dispatchMessage(处理程序。

好吧,即使我检查了第 126 行,我也找不到解决此错误的方法。

4

1 回答 1

1

您能否突出显示第 34 行。但是从您的代码中我可以很容易地看出ButtonClickListener,您正在访问findViewById而不使用作为参数传入的视图。我不确定为什么那不是错误,因为您不应该这样做。可能是问题的另一件事是R.id.button1可能拼写错误,或者在 xml 中拼写错误。

于 2012-11-11T06:20:39.603 回答