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

时间:2015-02-08 00:36:36

标签: android

我是android的初学者,我不知道我的代码中有什么问题!我需要你的帮助,这是我的代码: 我正在尝试做一个计算器= - / * .....并且我删除了if view == 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");


    }
}

我的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>

例外:

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

有什么帮助吗?

2 个答案:

答案 0 :(得分:4)

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

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

但是在onClick中,您正在尝试访问在theresult之外声明的变量onCreate的另一个实例。这个尚未初始化。这就是为什么当你尝试setText时给予它null的原因。

要解决,请替换以下行:

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

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

答案 1 :(得分:0)

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

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

setContentView(R.layour.login_screen);