2016-02-07 34 views
0

認爲問題在於onClick類,但我不確定。每當我檢查我的遊戲回答時,它只是突然崩潰當我按下檢查哪個是「chckAns」按鈕我的應用程序突然不斷崩潰

package com.example.machineproblemsix;

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.content.Context; 
import android.content.DialogInterface; 


import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 


public class SecondActivity extends Activity { 

    static Context context; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 
    } 
    public void doBack(View v){ 
     finish(); 
    } 
    public static class LoginDialogFragment extends DialogFragment{ 
     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      // Use the Builder class for convenient dialog construction 
      AlertDialog.Builder builder = new 
       AlertDialog.Builder(getActivity()); 
      LayoutInflater inflater = getActivity().getLayoutInflater(); 
      View v = inflater.inflate(R.layout.jake_layout, null); 
      final EditText etAnswer = (EditText)  
        v.findViewById(R.id.answer); 
      final EditText etAnswerTwo = (EditText)  
        v.findViewById(R.id.answerTwo); 
      builder.setView(v) 
        .setPositiveButton(R.string.login, new 
          DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int 
          id) { 
          String answer, answertwo; 
          answer = etAnswer.getText().toString(); 
          answertwo = etAnswerTwo.getText().toString(); 
          String msg; 
          if(answer.equalsIgnoreCase("jake")){ 
           msg = "CORRECT!"; 
          }else if(answertwo.equalsIgnoreCase("beemo")|| 
          (answertwo.equalsIgnoreCase("bmo"))){ 
           msg = "CORRECT!"; 
          } else { 
           msg = "TRY AGAIN!"; 
          } 
          Toast.makeText(context, msg, 
           Toast.LENGTH_SHORT).show(); 
         } 
        }) 
        .setNegativeButton(R.string.cancel, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
          int id) { 
          // User cancelled the dialog 
         } 
        }); 
      // Create the AlertDialog object and return it 
      return builder.create(); 
     } 
} 
    public void showLogin(View v){ 
     DialogFragment loginFragment = new LoginDialogFragment(); 
     loginFragment.show(getFragmentManager(), "login"); 
} 
} 

我認爲問題在於onClick類,但我不確定。每當我檢查我的遊戲回答時,它只是突然崩潰

================================= ===============================================

日誌:

02-07 23:30:29.070: E/AndroidRuntime(924): FATAL EXCEPTION: main 
02-07 23:30:29.070: E/AndroidRuntime(924): Process: com.example.machineproblemsix, PID: 924 
02-07 23:30:29.070: E/AndroidRuntime(924): java.lang.NullPointerException 
02-07 23:30:29.070: E/AndroidRuntime(924): at com.example.machineproblemsix.SecondActivity$LoginDialogFragment$1.onClick(SecondActivity.java:52) 
02-07 23:30:29.070: E/AndroidRuntime(924): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 
02-07 23:30:29.070: E/AndroidRuntime(924): at android.os.Handler.dispatchMessage(Handler.java:102) 
02-07 23:30:29.070: E/AndroidRuntime(924): at android.os.Looper.loop(Looper.java:136) 
02-07 23:30:29.070: E/AndroidRuntime(924): at android.app.ActivityThread.main(ActivityThread.java:5001) 
02-07 23:30:29.070: E/AndroidRuntime(924): at java.lang.reflect.Method.invokeNative(Native Method) 
02-07 23:30:29.070: E/AndroidRuntime(924): at java.lang.reflect.Method.invoke(Method.java:515) 
02-07 23:30:29.070: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
02-07 23:30:29.070: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
02-07 23:30:29.070: E/AndroidRuntime(924): at dalvik.system.NativeStart.main(Native Method) 

====================================== ==================================================================================

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:background="#000" 
     android:contentDescription="@string/app_name" 
     android:scaleType="center" 
     android:src="@drawable/jake" /> 

    <EditText 
     android:id="@+id/answer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="16dp" 
     android:hint="@string/hint_answer" 
     android:inputType="textEmailAddress" /> 

</LinearLayout> 
+0

沒有崩潰log(logcat)很難預測這個問題,請粘貼你的logcat –

+0

請注意你的佈局(jake_layout) –

+0

對不起,因爲我的代碼沒有組織,我在其他一些活動中只是爲了加快速度up – kdz

回答

0

根據到Logcat,出現錯誤第52行。我無法確定(因爲沒有行號。包括在代碼中)但我相信該行是:

Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); 

上面只有兩個變量:context和msg。 由於您以msg爲單位指定值,這意味着「上下文」是一個問題。您忘記在上下文中分配值,這是發生「NullPointerException」原因。

您應該使用 「getActivity()」 而不是使用 「靜態背景」,這將讓52行,像這樣:

Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show(); 
0

組織你的代碼是這樣的:

LayoutInflater inflater = getActivity().getLayoutInflater(); 
     View v = inflater.inflate(R.layout.jake_layout, null); 
     final EditText etAnswer = (EditText)  
       v.findViewById(R.id.answer); 
     final EditText etAnswerTwo = (EditText)  
       v.findViewById(R.id.answerTwo); 

AlertDialog.Builder builder = new 
      AlertDialog.Builder(getActivity()); 
builder.setView(v) 
.setPositiveButton(R.string.login, new 
DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int id) { 
String answer, answertwo; 
answer = etAnswer.getText().toString(); 
answertwo = etAnswerTwo.getText().toString(); 
String msg; 
if(answer.equalsIgnoreCase("jake")){ 
    msg = "CORRECT!"; 
    }else if(answertwo.equalsIgnoreCase("beemo")|| 
         (answertwo.equalsIgnoreCase("bmo"))){ 
          msg = "CORRECT!"; 
         } else { 
          msg = "TRY AGAIN!"; 
         } 
         Toast.makeText(getActivity(), msg, 
          Toast.LENGTH_SHORT).show(); 
        } 
       }) 
       .setNegativeButton(R.string.cancel, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, 
         int id) { 
         // User cancelled the dialog 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
相關問題