Android按钮在活动对话框中不起作用
Posted
技术标签:
【中文标题】Android按钮在活动对话框中不起作用【英文标题】:Android Button not working in activity dialog 【发布时间】:2016-08-31 03:06:03 【问题描述】:我使用 EditText 小部件和 Button 创建了一个活动,并将其显示为一个对话框。它在打开时显示得很好,但是每当我在该活动(对话框之一)内的按钮上设置 onClickListener 时,该活动根本不会打开,并且应用程序崩溃并出现以下错误:
05-05 12:33:26.717 2998-2998/org.ufiministries.app E/androidRuntime: FATAL EXCEPTION: main
Process: org.ufiministries.app, PID: 2998
java.lang.RuntimeException: Unable to start activity ComponentInfoorg.ufiministries.app/org.ufiministries.app.PostPrayerCommentActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at org.ufiministries.app.PostPrayerCommentActivity.onCreate(PostPrayerCommentActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
请帮忙。如果我删除 button.setOnClickListener
对话框将打开
活动代码:
package org.ufiministries.app;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.support.v7.app.AppCompatDialog;
public class PostPrayerCommentActivity extends AppCompatActivity implements View.OnClickListener
private Toolbar toolbar;
private Button btnComment;
private EditText txtPrayer;
private String objectId, requestor, userId;
private String sendPrayerText;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_comment);
Intent prayerComment = getIntent();
objectId = prayerComment.getStringExtra("objectId");
requestor = prayerComment.getStringExtra("requestor");
//userId = prayerComment.getStringExtra("userId");
btnComment = (Button) findViewById(R.id.btnSendPrayer);
txtPrayer = (EditText) findViewById(R.id.prayerTxt);
this.btnComment.setOnClickListener(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("Prayer for: " + requestor);
@Override
public void onClick(View v)
switch (v.getId())
case R.id.btnSendPrayer:
sendPrayerText = txtPrayer.getText().toString();
break;
XML 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
tools:context=".PostPrayerCommentActivity"
xmlns:app1="http://schemas.android.com/apk/res/org.ufiministries.app">
<LinearLayout
android:id="@+id/linear"
android:layout_
android:layout_
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:padding="@dimen/activity_horizontal_margin"
android:layout_below="@+id/linear"
android:orientation="vertical">
<TextView
android:id="@+id/prayer_request_article_content"
android:layout_
android:layout_centerHorizontal="true"
android:text="@string/testimonialText"
android:lines="3"
android:layout_ />
<android.support.design.widget.TextInputLayout
android:id="@+id/phoneLayout"
android:layout_below="@+id/headerTxtPrayerRequest"
android:layout_
android:layout_marginTop="@dimen/text_large_padding"
app:counterEnabled="true"
app:counterMaxLength="140"
app:hintAnimationEnabled="true"
android:layout_>
<EditText
android:id="@+id/prayerTxt"
android:layout_
android:layout_
android:layout_weight="1"
android:hint="Enter prayer"
android:minLines="5"
style="@style/Widget.AppCompat.EditText"
android:inputType="textMultiLine">
<requestFocus />
</EditText>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/prayer_comment_btn"
android:layout_
android:layout_
android:layout_below="@+id/prayer_request_article_date"
android:layout_gravity="center_horizontal"
android:paddingRight="48dp"
android:paddingLeft="48dp"
android:layout_marginTop="@dimen/text_large_padding"
android:background="@color/colorPrimary"
style="@style/ParseLoginUI.Button"
android:text="Send Prayer" />
</LinearLayout>
【问题讨论】:
显示你的整个代码 检查你是否为按钮编写了这段代码。按钮按钮 = (Button) findViewById(R.id.button);如果您这样做了,请显示您的代码。 @techniqez 包含但仍无法正常工作 【参考方案1】:改变这个
btnComment = (Button) findViewById(R.id.btnSendPrayer);
到
btnComment = (Button) findViewById(R.id.prayer_comment_btn);
【讨论】:
哦不知道我怎么错过了。感谢您发现我。 如果它对你有用,你应该接受这个答案:)以上是关于Android按钮在活动对话框中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
MvvmCross Android Button Click 绑定在 Release 版本中不起作用