当执行我的onClick方法按钮以显示来自editText的文本时,我的应用程序由于NullPointerException而崩溃,我无法弄清楚原因。当我完全删除对editText的引用时,一切正常。我做错了什么?
public void onClick(View v) {
EditText editText1 = (EditText) v.findViewById (R.id.editText1);
switch (v.getId()) {
//add entry to the List
case R.id.button1:
Toast.makeText(getActivity(), "Clicked on add button" + " CRN: " + editText1.getText().toString(), Toast.LENGTH_LONG).show();
case R.id.button2:
Toast.makeText(getActivity(), "Clicked on delete button", Toast.LENGTH_LONG).show();
}
}
有问题的错误:
01-12 19:32:57.726: E/AndroidRuntime(23216): FATAL EXCEPTION: main
01-12 19:32:57.726: E/AndroidRuntime(23216): java.lang.NullPointerException
01-12 19:32:57.726: E/AndroidRuntime(23216): at com.example.project.ClassesFragment.onClick(ClassesFragment.java:85)
01-12 19:32:57.726: E/AndroidRuntime(23216): at android.view.View.performClick(View.java:4475)
01-12 19:32:57.726: E/AndroidRuntime(23216): at android.view.View$PerformClick.run(View.java:18786)
01-12 19:32:57.726: E/AndroidRuntime(23216): at android.os.Handler.handleCallback(Handler.java:730)
01-12 19:32:57.726: E/AndroidRuntime(23216): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 19:32:57.726: E/AndroidRuntime(23216): at android.os.Looper.loop(Looper.java:137)
01-12 19:32:57.726: E/AndroidRuntime(23216): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-12 19:32:57.726: E/AndroidRuntime(23216): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 19:32:57.726: E/AndroidRuntime(23216): at java.lang.reflect.Method.invoke(Method.java:525)
01-12 19:32:57.726: E/AndroidRuntime(23216): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-12 19:32:57.726: E/AndroidRuntime(23216): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-12 19:32:57.726: E/AndroidRuntime(23216): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
01-12 19:32:57.726: E/AndroidRuntime(23216): at dalvik.system.NativeStart.main(Native Method)
01-12 19:33:12.040: I/Process(23216): Sending signal. PID: 23216 SIG: 9
答案 0 :(得分:2)
如果是片段布局,则在onCreateView
EditText ediText1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragmentlayout, container, false);
editText1 = (EditText) v.findViewById (R.id.editText1);
return v;
}
或
您可能引用了错误的ID。
你错过了休息
case R.id.button1:
Toast.makeText(getActivity(), "Clicked on add button" + " CRN: " + editText1.getText().toString(), Toast.LENGTH_LONG).show();
break; // missing
答案 1 :(得分:0)
public void onClick(View v) {
//defining edit text
EditText editText1 = (EditText) findViewById (R.id.editText1);
switch (v.getId()) {
//add entry to the List
case R.id.button1:
Toast.makeText(getActivity(), "Clicked on add button" + " CRN: " + editText1.getText().toString(), Toast.LENGTH_LONG).show();
//stop button event
break;
case R.id.button2:
Toast.makeText(getActivity(), "Clicked on delete button", Toast.LENGTH_LONG).show();
//stop button event
break;
}
}