引起:onClickListener 上的 java.lang.NullPointerException [重复]
Posted
技术标签:
【中文标题】引起:onClickListener 上的 java.lang.NullPointerException [重复]【英文标题】:Caused by: java.lang.NullPointerException on onClickListener [duplicate] 【发布时间】:2014-08-18 21:33:12 【问题描述】:我尝试为多个按钮创建一个 onclicklistener 并出现以下错误
06-27 20:12:27.468: D/androidRuntime(4810): Shutting down VM
06-27 20:12:27.468: W/dalvikvm(4810): threadid=1: thread exiting with uncaught exception (group=0x415caba8)
06-27 20:12:27.468: E/AndroidRuntime(4810): FATAL EXCEPTION: main
06-27 20:12:27.468: E/AndroidRuntime(4810): Process: com.example.calculatorrr, PID: 4810
06-27 20:12:27.468: E/AndroidRuntime(4810): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.calculatorrr/com.example.calculatorrr.MainActivity: java.lang.NullPointerException
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.os.Handler.dispatchMessage(Handler.java:102)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.os.Looper.loop(Looper.java:136)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-27 20:12:27.468: E/AndroidRuntime(4810): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 20:12:27.468: E/AndroidRuntime(4810): at java.lang.reflect.Method.invoke(Method.java:515)
06-27 20:12:27.468: E/AndroidRuntime(4810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-27 20:12:27.468: E/AndroidRuntime(4810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-27 20:12:27.468: E/AndroidRuntime(4810): at dalvik.system.NativeStart.main(Native Method)
06-27 20:12:27.468: E/AndroidRuntime(4810): Caused by: java.lang.NullPointerException
06-27 20:12:27.468: E/AndroidRuntime(4810): at com.example.calculatorrr.MainActivity.onCreate(MainActivity.java:426)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.Activity.performCreate(Activity.java:5231)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-27 20:12:27.468: E/AndroidRuntime(4810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-27 20:12:27.468: E/AndroidRuntime(4810): ... 11 more
我的主要活动课程是这样的:
public class MainActivity extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
FrameLayout mframelayout = (FrameLayout)findViewById(R.id.container);
Button mButton0 = (Button) mframelayout.findViewById(R.id.bt0);
Log.i("test1", mButton0.toString());
Button mButton1 = (Button) mframelayout.findViewById(R.id.bt1);
Button mButton2 = (Button) mframelayout.findViewById(R.id.bt2);
Button mButton3 = (Button) mframelayout.findViewById(R.id.bt3);
Button mButton4 = (Button) mframelayout.findViewById(R.id.bt4);
Button mButton5 = (Button) mframelayout.findViewById(R.id.bt5);
Button mButton6 = (Button) mframelayout.findViewById(R.id.bt6);
Button mButton7 = (Button) mframelayout.findViewById(R.id.bt7);
Button mButton8 = (Button) mframelayout.findViewById(R.id.bt8);
Button mButton9 = (Button) mframelayout.findViewById(R.id.bt9);
Button mButtonmul = (Button) mframelayout.findViewById(R.id.bt_multiplication);
Button mButtonplu = (Button) mframelayout.findViewById(R.id.bt_plus);
Button mButtonmin = (Button) mframelayout.findViewById(R.id.btminus);
Button mButtondiv = (Button) mframelayout.findViewById(R.id.bt_divide);
mButton0.setOnClickListener(onClickListener);
mButton1.setOnClickListener(onClickListener);
mButton2.setOnClickListener(onClickListener);
mButton3.setOnClickListener(onClickListener);
mButton4.setOnClickListener(onClickListener);
mButton5.setOnClickListener(onClickListener);
mButton6.setOnClickListener(onClickListener);
mButton7.setOnClickListener(onClickListener);
mButton8.setOnClickListener(onClickListener);
mButton9.setOnClickListener(onClickListener);
mButtonmul.setOnClickListener(onClickListener);
mButtonplu.setOnClickListener(onClickListener);
mButtonmin.setOnClickListener(onClickListener);
mButtondiv.setOnClickListener(onClickListener);
private View.OnClickListener onClickListener = new View.OnClickListener()
@Override
public void onClick(View v)
TextView txtview = (TextView) findViewById(R.id.txt1);
String message = txtview.getText().toString();
switch (v.getId())
case R.id.bt0:
message = message + "0";
break;
case R.id.bt1:
message = message + "1";
break;
case R.id.bt2:
message = message + "2";
break;
case R.id.bt3:
message = message + "3";
break;
case R.id.bt4:
message = message + "4";
break;
case R.id.bt5:
message = message + "5";
break;
case R.id.bt6:
message = message + "6";
break;
case R.id.bt7:
message = message + "7";
break;
case R.id.bt8:
message = message + "8";
break;
case R.id.bt9:
message = message + "9";
break;
case R.id.bt_multiplication:
message = message + "*";
break;
case R.id.bt_plus:
message = message + "+";
break;
case R.id.btminus:
message = message + "-";
break;
case R.id.bt_divide:
message = message + "/";
break;
txtview.setText(message);
;
我的两个资源文件是:
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_
android:layout_
tools:context="com.example.calculatorrr.MainActivity"
tools:ignore="MergeRootFrame" />
还有fragment_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
>
<TextView
android:id="@+id/txt1"
android:layout_
android:layout_
android:background="@android:color/darker_gray"
android:hint="0"
android:textSize="40dp" />
........ all the other butttons
我在谷歌上搜索过,这里的按钮到目前为止没有任何效果!也许是我没有看到的明显的东西!感谢您的关注!
编辑: 这是我得到错误的那一行:
mButton0.setOnClickListener(onClickListener);
【问题讨论】:
MainActivity.java:426 是哪一行?如果你有一个好的 IDE,只需点击at com.example.calculatorrr.MainActivity.onCreate(MainActivity.java:426)
就会带你去它
【参考方案1】:
在 onClicklistener
你的 textview tx1
in null,因为 textview txt1
是 fragment
的一部分而不是 activity_main
。
所以要么在activity_main
布局中使用tx1。或在片段的onCreateView
方法中使用。
【讨论】:
code
感谢您的帮助!有用!我移动片段的onCreateView上的按钮!code
@Syleri22 乐于提供帮助,如果这个答案有帮助,那么不要忘记接受它,无论如何享受编码。【参考方案2】:
你不能在你的活动中访问你的片段按钮。所以请只在 Fragment java 类中使用。您可以在 PlaceholderFragment Fragment 类而不是在活动中设置您的按钮单击侦听器
【讨论】:
以上是关于引起:onClickListener 上的 java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章
第二个按钮上的 OnClickListener 既没有正确初始化也没有按预期工作
RecyclerView onClickListener 适用于所有片段
Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法