单击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):at 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):at java.lang.reflect.Method.invokeNative(Native Method) 11-11 14:38:38.681:E / AndroidRuntime(363):at java.lang.reflect.Method.invoke(Method.java:521) 11-11 14:38:38.681:E / AndroidRuntime(363):at 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:Binary 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(Handler.java:99) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.os.Looper.loop(Looper.java:123) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.app.ActivityThread.main(ActivityThread.java:4627) 11-11 16:24:39.680:E / AndroidRuntime(554):at java.lang.reflect.Method.invokeNative(Native Method) 11-11 16:24:39.680:E / AndroidRuntime(554):at java.lang.reflect.Method.invoke(Method.java:521) 11-11 16:24:39.680:E / AndroidRuntime(554):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 11-11 16:24:39.680:E / AndroidRuntime(554):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-11 16:24:39.680:E / AndroidRuntime(554):at dalvik.system.NativeStart.main(Native Method) 11-11 16:24:39.680:E / AndroidRuntime(554):引起:android.view.InflateException:二进制XML文件行#126:错误导致类textView 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.inflate(LayoutInflater.java:407) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.inflate(LayoutInflater.java:320) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.inflate(LayoutInflater.java:276) 11-11 16:24:39.680:E / AndroidRuntime(554):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.app.Activity.setContentView(Activity.java:1647) 11-11 16:24:39.680:E / AndroidRuntime(554):at com.example.sample4_3.SecondActivity.onCreate(SecondActivity.java:14) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 11-11 16:24:39.680:E / AndroidRuntime(554):... 11更多 11-11 16:24:39.680:E / AndroidRuntime(554):引起:java.lang.ClassNotFoundException:loader dalvik.system.PathClassLoader中的android.view.textView [/data/app/com.example.sample4_3-2 apk文件] 11-11 16:24:39.680:E / AndroidRuntime(554):at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 11-11 16:24:39.680:E / AndroidRuntime(554):at java.lang.ClassLoader.loadClass(ClassLoader.java:573) 11-11 16:24:39.680:E / AndroidRuntime(554):at java.lang.ClassLoader.loadClass(ClassLoader.java:532) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.createView(LayoutInflater.java:466) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.onCreateView(LayoutInflater.java:544) 11-11 16:24:39.680:E / AndroidRuntime(554):at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 11-11 16:24:39.680:E / AndroidRuntime(554):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 11-11 16:24:39.680:E / AndroidRuntime(554):... 23更多
好吧,即使我检查了第126行,我也找不到如何解决这个错误。
答案 0 :(得分:1)
你能否突出第34行。但是从我的代码中可以很容易地看出,在ButtonClickListener
中,您正在访问findViewById
而不使用作为参数传入的视图。我不知道为什么那不是错误,因为你不应该这样做。另一件可能是问题的是R.id.button1
可能拼写错误,或者拼写错误的xml。