简体   繁体   中英

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

think the problem lies on the class onClick but I'm not sure though. Whenever I check my answer on the game, it just suddenly crash

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");
}   
}

I think the problem lies on the class onClick but I'm not sure though. Whenever I check my answer on the game, it just suddenly crash

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

Log:

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>

According to Logcat, there is an error at Line No. 52. I can't know for sure (because there is no line no. included in code) but I believe that line is:

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

There is only two variable in above line: context and msg. Since you assign value in msg, which mean "context" is a problem. You forget to assign value in context which is the reason "NullPointerException" occur.

You should use "getActivity()" instead of using "static context" which will make line 52, look like this :

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

Organize your code like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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