Android按钮onClick侦听器错误
这太荒谬了,我的项目中有一个简单的活动,有一个按钮和一些微调器。在活动的onCreate中,我想为按钮设置onClick事件(就像我对项目中的其他活动所做的那样。但是由于一些未知的原因,我在创建引用setOnClickListener行的活动时产生了一个错误。如果我对整个侦听器内容进行注释,则该活动会起作用,因此它都在单击侦听器声明中。 这是我的密码:Android按钮onClick侦听器错误,android,android-button,Android,Android Button,这太荒谬了,我的项目中有一个简单的活动,有一个按钮和一些微调器。在活动的onCreate中,我想为按钮设置onClick事件(就像我对项目中的其他活动所做的那样。但是由于一些未知的原因,我在创建引用setOnClickListener行的活动时产生了一个错误。如果我对整个侦听器内容进行注释,则该活动会起作用,因此它都在单击侦听器声明中。 这是我的密码: protected void onCreate(Bundle savedInstanceState) { super.onCr
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_send_msg);
final Button btn_rev = (Button) findViewById(R.id.btn_reveniremsg);
btn_rev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
(错误将指向带有上述代码的最终按钮btn\U rev…的行)
现在是Logat:
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62d9288)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{app_pathmyblabla./activitymyblabla._path}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at myblabla.app.SendMsgActivity.onCreate (SendMsgActivity.java:32)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I/Process﹕ Sending signal. PID: 5821 SIG: 9
蓝色的行位于myblabla.app.SendMsgActivity.onCreate(SendMsgActivity.java:32)
蓝色链接指向的行是上面代码的第一行
发生了什么事?
我已经自动生成了clickListener的代码,它是由IDE(Android Studio)生成的。我只是在onClick中添加了finish();
另外,我在其他活动中使用了相同的侦听器代码,它们工作得很好
请告诉我我做错了什么
布局的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mypack.app.SendMsgActivity"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc_spinner_ab_focused_holo_light" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="right">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Renunta"
android:id="@+id/button"
android:layout_gravity="right"
android:gravity="right|end|fill_vertical" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titlu mesaj"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continut mesaj:"
android:id="@+id/textView3"
android:layout_gravity="center_vertical"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="480dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editText2"
android:layout_gravity="center_vertical"
android:layout_below="@+id/linearLayout2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/textView3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Destinatie mesaj"
android:id="@+id/textView2" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinnertipdest"
android:entries="@array/tipuridestinatie"
android:prompt="@string/tipdest_prompt"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alege reprezentant"
android:id="@+id/lbl_destinatie" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinneruzer"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
谢谢您的
按钮
具有以下id:
android:id="@+id/button"
这就是导致NullPointerException
的原因。请尝试按如下方式设置正确的id:
Button btn_rev = (Button) findViewById(R.id.button);
您的
按钮具有以下id:
android:id="@+id/button"
这就是导致NullPointerException
的原因。请尝试按如下方式设置正确的id:
Button btn_rev = (Button) findViewById(R.id.button);
此xml将起作用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mypack.app.SendMsgActivity"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc_spinner_ab_focused_holo_light" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="right">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Renunta"
android:id="@+id/btn_reveniremsg"
android:layout_gravity="right"
android:gravity="right|end|fill_vertical" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titlu mesaj"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continut mesaj:"
android:id="@+id/textView3"
android:layout_gravity="center_vertical"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="480dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editText2"
android:layout_gravity="center_vertical"
android:layout_below="@+id/linearLayout2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/textView3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Destinatie mesaj"
android:id="@+id/textView2" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinnertipdest"
android:entries="@array/tipuridestinatie"
android:prompt="@string/tipdest_prompt"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alege reprezentant"
android:id="@+id/lbl_destinatie" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinneruzer"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
此xml将起作用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mypack.app.SendMsgActivity"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc_spinner_ab_focused_holo_light" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="right">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Renunta"
android:id="@+id/btn_reveniremsg"
android:layout_gravity="right"
android:gravity="right|end|fill_vertical" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titlu mesaj"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continut mesaj:"
android:id="@+id/textView3"
android:layout_gravity="center_vertical"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="480dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editText2"
android:layout_gravity="center_vertical"
android:layout_below="@+id/linearLayout2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/textView3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Destinatie mesaj"
android:id="@+id/textView2" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinnertipdest"
android:entries="@array/tipuridestinatie"
android:prompt="@string/tipdest_prompt"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alege reprezentant"
android:id="@+id/lbl_destinatie" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinneruzer"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
您的代码
final Button btn_rev = (Button) findViewById(R.id.btn_reveniremsg);
一定是
final Button btn_rev = (Button) findViewById(R.id.button);
你的代码
final Button btn_rev = (Button) findViewById(R.id.btn_reveniremsg);
一定是
final Button btn_rev = (Button) findViewById(R.id.button);
postactivity\u send\u msg.xml
确保布局中的btn\u reveniremsg
是正确的id。同时尝试添加SendMsgActivity.this.finish()
。我将XML添加到问题正文Postactivity\u send\u msg.XML
确保btn\u reveniremsg
是布局中正确的id。同时尝试添加SendMsgActivity.this.finish()
。我将XML添加到问题正文+1您更快;)说真的,我应该学习如何更快地编写代码。太好了。我不知道我怎么会错过这个机会。我确信这就是我要找的ID。但我在另一个活动中有这个ID。感谢大家的贡献+1你更快了;)说真的,我应该学习如何更快地编写代码。太好了。我不知道我怎么会错过。我肯定那就是我要找的身份证。但我在另一项活动中有这个身份证。谢谢大家的贡献哇。。。我真是太蠢了。我怎么会错过呢?谢谢你哇。。。我真是太蠢了。我怎么会错过呢?非常感谢。