繁体   English   中英

当我按下“ chckAns”按钮时,我的应用突然崩溃

[英]when I'm pressing Check which is“chckAns” button my app is suddenly kept crashing

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

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

根据Logcat的说法,第52行存在错误。我不确定(因为代码中没有包含行号),但我不确定这行是:

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

上一行只有两个变量:context和msg。 由于您在msg中分配值,因此意味着“上下文”是一个问题。 您忘记在上下文中分配值,这是发生“ NullPointerException”的原因。

您应该使用“ getActivity()”代替“静态上下文”,这将使第52行看起来像这样:

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

像这样组织代码:

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM