无法修复Java空指针错误 [英] Cannot fix Java null pointer error

查看:49
本文介绍了无法修复Java空指针错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,当我运行它会返回一个空指针错误.这是logcat的代码和错误:

I have some code that when I run it returns a null pointer error. This is the code and error from the logcat:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import java.lang.String;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

    private static final Random r_generator = new Random();
    String textViewString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Resources res = getResources();
        String[] myString = res.getStringArray(R.array.colorArray);
        String q = myString[r_generator.nextInt(myString.length)];
        TextView tv = (TextView) findViewById(R.id.color_text);
        tv.setText(q);

    Button green= (Button) findViewById(R.id.green_button);
    green.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Green")) return;
            Resources res = getResources();
            String[] myString = res.getStringArray(R.array.colorArray);
            String q = myString[r_generator.nextInt(myString.length)];
            TextView tv = (TextView) findViewById(R.id.color_text);
            tv.setText(q);
        }
    });

    Button blue = (Button) findViewById(R.id.blue_button);
    blue.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Blue")) return;
            Resources res = getResources();
            String[] myString = res.getStringArray(R.array.colorArray);
            String q = myString[r_generator.nextInt(myString.length)];
            TextView tv = (TextView) findViewById(R.id.color_text);
            tv.setText(q);
        }
    });

    Button red = (Button) findViewById(R.id.red_button);
    red.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Red")) return;
            Resources res = getResources();
            String[] myString = res.getStringArray(R.array.colorArray);
            String q = myString[r_generator.nextInt(myString.length)];
            TextView tv = (TextView) findViewById(R.id.color_text);
            tv.setText(q);

        }
    });

        }
    }

我曾尝试查看其他非法的国家错误问题,但我仍然不知道我的个人情况出了什么问题.

I have tried looking at other illegal state error questions but I am still lost on what is going wrong with my individual case.

这是错误代码:

 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       java.lang.NullPointerException
                                                                                           at com.example.cedric.learnthecolors.MainActivity$1.onClick(MainActivity.java:31)
                                                                                           at android.view.View.performClick(View.java:4476)
                                                                                           at android.view.View$PerformClick.run(View.java:18795)
                                                                                           at android.os.Handler.handleCallback(Handler.java:730)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                           at android.os.Looper.loop(Looper.java:177)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:5496)
                                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                           at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
                                                                                           at dalvik.system.NativeStart.main(Native Method)

这是我的布局文件:

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@android:style/Theme.Holo.Light"
    tools:context="com.example.cedric.learnthecolors.MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/color_text"
            android:text="@string/Green"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="68sp"
            android:gravity="center"
            android:textAllCaps="true"/>

    </LinearLayout>

    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/green_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/green_button"/>

        <Button
            android:id="@+id/blue_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/blue_button"/>

        <Button
            android:id="@+id/red_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/the_red_button"/>

    </LinearLayout>

    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/yellow_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/yellow_button"/>

        <Button
            android:id="@+id/white_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/white_button"/>

        <Button
            android:id="@+id/orange_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/orange_button"/>

    </LinearLayout>

    <LinearLayout android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/brown_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/brown_button"/>

        <Button
            android:id="@+id/pink_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/pink_button"/>

        <Button
            android:id="@+id/purple_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick"
            android:background="@drawable/purple_button"/>

    </LinearLayout>

</LinearLayout>

非常感谢!

推荐答案

问题是此行:

!textViewString.equals()

因为在使用它之前,您没有在任何地方将 textViewString 的值提供给它.至少将其分配给一个空字符串,或者只是删除该行,因为此刻对您没有多大帮助,我真的不明白这一点.由于您无处更改它的值,因此您的所有按钮都不会起作用(即使没有例外).

Because you've not given the value to the textViewString anywhere before using it. Assign it to an empty string at least, or simply delete that line since at the moment it's not doing you much good, I don't really get it's point. Since you're nowhere changing it's value, none of your buttons will work (even without the exception).

这篇关于无法修复Java空指针错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆