我使用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 ComponentInfo{org.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_width="match_parent"
android:layout_height="match_parent"
tools:context=".PostPrayerCommentActivity"
xmlns:app1="http://schemas.android.com/apk/res/org.ufiministries.app">
<LinearLayout
android:id="@+id/linear"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:layout_below="@+id/linear"
android:orientation="vertical">
<TextView
android:id="@+id/prayer_request_article_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/testimonialText"
android:lines="3"
android:layout_height="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="@+id/phoneLayout"
android:layout_below="@+id/headerTxtPrayerRequest"
android:layout_width="match_parent"
android:layout_marginTop="@dimen/text_large_padding"
app:counterEnabled="true"
app:counterMaxLength="140"
app:hintAnimationEnabled="true"
android:layout_height="wrap_content">
<EditText
android:id="@+id/prayerTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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>
答案 0 :(得分:0)
更改此
btnComment = (Button) findViewById(R.id.btnSendPrayer);
要
btnComment = (Button) findViewById(R.id.prayer_comment_btn);