当我按下检查哪个是“chckAns”按钮时,我的应用程序突然崩溃了

时间:2016-02-07 13:15:50

标签: java android

认为问题出在onClick上,但我不确定。每当我在游戏中检查我的答案时,它就会突然崩溃

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>

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();