我制作了一个启动加速计传感器的android项目,并将传感器x,y,z数据存储在一个文件中。我在方法start.setEnabled(false)
中添加了代码行stop.setEnabled(true)
和start
,从那以后我的代码在start
崩溃了。错误如下:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zhenga1.photojewel, PID: 27298
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4507)
at android.view.View.performClick(View.java:5267)
at android.view.View$PerformClick.run(View.java:21540)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4502)
at android.view.View.performClick(View.java:5267)
at android.view.View$PerformClick.run(View.java:21540)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
at com.example.zhenga1.photojewel.Main3Activity.start(Main3Activity.java:120)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4502)
at android.view.View.performClick(View.java:5267)
at android.view.View$PerformClick.run(View.java:21540)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5710)
at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zhenga1.photojewel, PID: 27298
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4507)
at android.view.View.performClick(View.java:5267)
at android.view.View$PerformClick.run(View.java:21540)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4502)
at android.view.View.performClick(View.java:5267)
at android.view.View$PerformClick.run(View.java:21540)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
at com.example.zhenga1.photojewel.Main3Activity.start(Main3Activity.java:120)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4502)
at android.view.View.performClick(View.java:5267)
at android.view.View$PerformClick.run(View.java:21540)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5710)
at java.lang.reflect.Method.invoke(Native Method)
更新:这是我的.xml主布局文件:
这是我的java代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.zhenga1.photojewel.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Accelerometer"
android:id="@+id/button"
android:onClick="start"
android:layout_marginTop="300dp"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:textStyle="normal"
android:textSize="6pt"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Accelerometer"
android:onClick="end"
android:id="@+id/button2"
android:layout_alignParentRight="true"
android:layout_marginTop="300dp"
android:textStyle="normal"
android:textSize="6pt"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_marginTop="200dp"
android:layout_marginLeft="150dp"
android:src="@mipmap/vibrator_pic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_marginTop="600dp"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView3"
android:layout_alignParentBottom="true"
android:layout_marginTop="600dp"
android:layout_alignParentRight="true"/>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/imageButton2"
android:layout_alignParentLeft="true"
android:onClick="back"
android:layout_alignParentTop="true"
android:src="@mipmap/back_button"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.zhenga1.photojewel.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Accelerometer"
android:id="@+id/button"
android:onClick="start"
android:layout_marginTop="300dp"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:textStyle="normal"
android:textSize="6pt"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Accelerometer"
android:onClick="end"
android:id="@+id/button2"
android:layout_alignParentRight="true"
android:layout_marginTop="300dp"
android:textStyle="normal"
android:textSize="6pt"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_marginTop="200dp"
android:layout_marginLeft="150dp"
android:src="@mipmap/vibrator_pic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_marginTop="600dp"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView3"
android:layout_alignParentBottom="true"
android:layout_marginTop="600dp"
android:layout_alignParentRight="true"/>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/imageButton2"
android:layout_alignParentLeft="true"
android:onClick="back"
android:layout_alignParentTop="true"
android:src="@mipmap/back_button"/>
</RelativeLayout>
我已经研究了一些类似的错误,但它们都处理未正确初始化的按钮,这不是我的情况。
请帮我。
谢谢
答案 0 :(得分:1)
在您的布局文件中,您的开始按钮的标识为R.id.button
,在您的活动类中,您正在使用标识R.id.button1
查找该视图。
你的布局:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Accelerometer"
android:id="@+id/button" <--- HERE
android:onClick="start"
android:layout_marginTop="300dp"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:textStyle="normal"
android:textSize="6pt"/>
活动:
start = (Button)findViewById(R.id.button1);
您需要在这两个地方设置相同的ID 。
答案 1 :(得分:0)
即使您初始化了那些没有指向您的活动布局按钮的按钮,它也会在Android Studio