简体   繁体   中英

NullPointerException when I try to run my Android Program on Android Studio

For some reason I keep getting a NullPointerException whenever I run my app. I think the issue is in the newProblem() method but I don't know what the issue is. Please help me on this issue guys, this is literally my second Android app.

Java:

package com.anuraagy.quiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.content.*;
import android.view.MenuItem;
import android.widget.*;
import android.view.*;
import java.util.*;

import java.text.AttributedCharacterIterator;


public class Quiz extends Activity {

    //Create all the private button fields
    private Button submitButton;
    private EditText inputText;
    private TextView viewText;

    private int number1;
    private int number2;
    private int inputValue;
    private int totalValue;
    private String numberString;
    private ArrayList runningTotal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        Button submit = (Button)findViewById(R.id.button);

        TextView viewText = (TextView)findViewById(R.id.textView4);

        EditText inputText = (EditText)findViewById(R.id.numberInputed);
//        boolean hello = mathString();
        newProblem();
        submit.setOnClickListener(new View.OnClickListener() {
            //Onclick button function
            public void onClick(View view) {
                if(runningTotal.size() < 20 ) {
                    if (checkAnswer()) {
                        newProblem();
                    }
//                    } else {
//                        wrongAnswer();
//                    }
                }
            }
        } );

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.quiz, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void newProblem()
    {
        int number1 = (int)(Math.random()*12 + 1);
        int number2 = (int)(Math.random()*12 + 1);
        int totalValue = number1 * number2;

        String numberString = number1 + " * " + number2;
        viewText.setText(numberString);


    }

    //Check the answer submitted
    private boolean checkAnswer()
    {
        if(number1*number2 == Integer.parseInt(inputText.getText().toString()))
        {
            runningTotal.add("true");
            return true;
        }
        else
        {
            runningTotal.add("false");
            return false;
        }
    }
//    private boolean mathString()
//    {
//
//
//        int number1 = (int)(Math.random()*12 + 1);
//        int number2 = (int)(Math.random()*12 + 1);
//        int value = number1 * number2;
//
//        String totalText = number1 + " * " + number2;
//
//        TextView viewText = (TextView)findViewById(R.id.textView4);
//        viewText.setText(totalText);
//
//        EditText inputText = (EditText)findViewById(R.id.numberInputed);
//        String s = inputText.getText().toString();
//        int value2 = Integer.parseInt(s);
//
//        if(value == value2)
//            return true;
//       else
//            return false;
//    }
}

Log:

09-18 17:40:39.338      740-740/com.anuraagy.quiz E/Trace﹕ error opening trace file: No such file or directory (2)
09-18 17:40:40.298      740-740/com.anuraagy.quiz D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
09-18 17:40:42.618      740-740/com.anuraagy.quiz D/AndroidRuntime﹕ Shutting down VM
09-18 17:40:42.618      740-740/com.anuraagy.quiz W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-18 17:40:42.637      740-740/com.anuraagy.quiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anuraagy.quiz/com.anuraagy.quiz.Quiz}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.anuraagy.quiz.Quiz.newProblem(Quiz.java:84)
            at com.anuraagy.quiz.Quiz.onCreate(Quiz.java:40)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
09-18 17:45:42.698      740-740/com.anuraagy.quiz I/Process﹕ Sending signal. PID: 740 SIG: 9

acitivty_quiz.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.anuraagy.quiz.Quiz">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit Answer"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="57dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Question 1"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="My numbers to display"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/numberInputed"
        android:layout_below="@+id/textView4"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignStart="@+id/textView4" />
</RelativeLayout>

You define viewText twice. Once on class level, and once in onCreate() .

In onCreate() , replace

TextView viewText = (TextView)findViewById(R.id.textView4);

with

viewText = (TextView)findViewById(R.id.textView4);

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