2016-05-05 12 views
0

Ich erstellte eine Aktivität mit einem Widget und EditText-Widget und machte es als Dialogfeld angezeigt. Seine zeigt ziemlich gut, wenn seine offene, aber wenn ich die OnClickListener innerhalb dieser Tätigkeit auf die Schaltfläche eingestellt (der Dialog ein), die Aktivität wird nicht geöffnet an allen und und die App zermalmt mit dem Fehler unter:Android-Schaltfläche nicht im Aktivitätsdialog

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)  

Bitte helfen . Wenn ich tht entfernen button.setOnClickListener wird der Dialog öffnen

Aktivität Code:

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-Code:

<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> 

+1

Zeigen Sie Ihren gesamten Code – Amy

+0

Überprüfen Sie, ob Sie dieses Stück Code für Schaltfläche geschrieben. Schaltfläche button = (Schaltfläche) findViewById (R.id.button); Wenn Sie dies getan haben, zeigen Sie Ihren Code. – techniqez

+0

@techniqez es enthalten, aber immer noch nicht funktioniert – thehacker401

Antwort

0

Ändern Sie diese

btnComment = (Button) findViewById(R.id.btnSendPrayer); 

Um

btnComment = (Button) findViewById(R.id.prayer_comment_btn); 
+0

oooh weiß nicht, wie ich das verpasst habe. Danke für die Entdeckung, die mich bilden. – thehacker401

+0

Sie sollten diese Antwort akzeptieren, wenn es für Sie nützlich ist :) – Amy