. lang。NullPointerException:尝试调用虚拟方法android

时间:2021-09-18 20:50:40

i am a beginner in the android, and i dont know what is the problem in my code ! and i need your help, this is my code: i am trying to do a calculator = - / * ..... and i removed the if view==pbtn for example...just to try the onclick function...and i cant find what is my problem

我是android的初学者,我不知道我的代码有什么问题!我需要你的帮助,这是我的代码:我正在尝试做一个计算器……我去掉了if视图=pbtn…试试onclick函数…我找不到我的问题

package com.michael.mikematta.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static com.michael.mikematta.calculator.R.id.FirstNum;
import static com.michael.mikematta.calculator.R.id.HelokBtn;
import static com.michael.mikematta.calculator.R.id.KafolBtn;
import static com.michael.mikematta.calculator.R.id.MinusBtn;
import static com.michael.mikematta.calculator.R.id.PlusBtn;
import static com.michael.mikematta.calculator.R.id.SecondNum;
import static com.michael.mikematta.calculator.R.id.TheResult;


public class MainActivity extends Activity implements View.OnClickListener {

    EditText firstnumx;
    EditText secondnum;
    Button pbtn;
    Button minusbtn;
    Button kafolbtn;
    Button helokbtn;
    TextView theresult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button pbtn = (Button) findViewById(R.id.PlusBtn);
        Button minusbtn = (Button) findViewById(R.id.MinusBtn);
        Button kafolbtn = (Button) findViewById(R.id.KafolBtn);
        Button helokbtn = (Button) findViewById(R.id.HelokBtn);
        EditText firstnum = (EditText) findViewById(R.id.FirstNum);
        EditText secondnum = (EditText) findViewById(R.id.SecondNum);
        TextView theresult = (TextView) findViewById(R.id.TheResult);

        minusbtn.setOnClickListener(this);
        pbtn.setOnClickListener(this);
        kafolbtn.setOnClickListener(this);
        helokbtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {


        theresult.setText("hi");


    }
}

my xml:

我的xml:

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/ic_launcher"
        android:orientation="vertical"

        tools:context=".MainActivity">

        <TextView
            android:text="Hello Calculator"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:textSize="20dp"
            android:textColor="#000"
            android:textStyle="bold"
            />
        <EditText
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:hint="Enter First Num"
            android:background="#fff"
            android:id="@+id/FirstNum"
            android:layout_margin="10dp"
            />
        <EditText
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:hint="Enter Second Num"
            android:background="#fff"
            android:id="@+id/SecondNum"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="+"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/PlusBtn"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="-"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/MinusBtn"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="*"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/KafolBtn"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="/"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/HelokBtn"
            android:layout_margin="10dp"
            />
        <TextView
            android:text="Here will be your result"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:textSize="20dp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:background="#ffff714f"
            android:id="@+id/TheResult"
            />

    </LinearLayout>

Exception:

例外:

02-08 03:09:50.468    1648-1666/com.michael.mikematta.calcu2 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-08 03:09:50.468    1648-1666/com.michael.mikematta.calcu2 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeeff540, error=EGL_SUCCESS
02-08 03:09:50.538    1648-1648/com.michael.mikematta.calcu2 I/Choreographer﹕ Skipped 33 frames!  The application may be doing too much work on its main thread.
02-08 03:09:51.177    1648-1648/com.michael.mikematta.calcu2 I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
02-08 03:09:53.984    1648-1648/com.michael.mikematta.calcu2 D/AndroidRuntime﹕ Shutting down VM
02-08 03:09:54.000    1648-1648/com.michael.mikematta.calcu2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.michael.mikematta.calcu2, PID: 1648
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
            at com.michael.mikematta.calcu2.MainActivity.onClick(MainActivity.java:47)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-08 03:09:57.267    1648-1648/com.michael.mikematta.calcu2 I/Process﹕ Sending signal. PID: 1648 SIG: 9

any help please?

任何帮助吗?

2 个解决方案

#1


4  

You are initializing a local copy of variable theresult in onCreate:

在onCreate中初始化变量theresult的本地副本:

TextView theresult = (TextView) findViewById(R.id.TheResult);

But in onClick you are trying to access another instance of the variable theresult which is declared outside onCreate. This one has not been initialized yet. That's why giving its giving null when you are trying to do setText.

但在onClick中,你试图访问变量theresult的另一个实例,它在onCreate之外声明。这个还没有初始化。这就是为什么在尝试执行setText时赋予它null的原因。

To solve, replace the below line:

若要解决,请替换以下行:

TextView theresult = (TextView) findViewById(R.id.TheResult);

with

theresult = (TextView) findViewById(R.id.TheResult);

#2


0  

Either check you have initialized the view or may be layout not has been attached to activity.

检查您是否初始化了视图,或者可能是布局没有附加到活动。

    password = (EditText) findViewById(R.id.password);

or

setContentView(R.layour.login_screen);

#1


4  

You are initializing a local copy of variable theresult in onCreate:

在onCreate中初始化变量theresult的本地副本:

TextView theresult = (TextView) findViewById(R.id.TheResult);

But in onClick you are trying to access another instance of the variable theresult which is declared outside onCreate. This one has not been initialized yet. That's why giving its giving null when you are trying to do setText.

但在onClick中,你试图访问变量theresult的另一个实例,它在onCreate之外声明。这个还没有初始化。这就是为什么在尝试执行setText时赋予它null的原因。

To solve, replace the below line:

若要解决,请替换以下行:

TextView theresult = (TextView) findViewById(R.id.TheResult);

with

theresult = (TextView) findViewById(R.id.TheResult);

#2


0  

Either check you have initialized the view or may be layout not has been attached to activity.

检查您是否初始化了视图,或者可能是布局没有附加到活动。

    password = (EditText) findViewById(R.id.password);

or

setContentView(R.layour.login_screen);