我该如何删除nullPointerException

时间:2014-03-02 07:07:33

标签: java android nullpointerexception

我在我的代码中点击方法得到Null指针异常。请告诉我如何删除它。

package co.sds.iitr.bullsandcows;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener 
{
EditText Num;
Button done;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Num = (EditText) findViewById(R.id.etNum);
    done = (Button) findViewById(R.id.btDone);

    done.setOnClickListener(this);

}

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

@Override
public void onClick(View v) 
{
    // TODO Auto-generated method stub
    if(v.getId()== R.id.btDone)
    {
        String num = Num.getText().toString();
        int n = Integer.getInteger(num);
        Toast.makeText(getApplicationContext(),"Your number is saved",
                Toast.LENGTH_LONG).show();
        Intent i = new Intent(getApplicationContext(),GuessActivtiy.class);     
        i.putExtra("num", n);
        startActivity(i);

    }
    else
        Toast.makeText(getApplicationContext(),"Not Found",
                Toast.LENGTH_LONG).show();


}

 }

GussActivity是我的另一个活动类,我试图通过意图传递我的值。 我的日志猫看起来像这样。

03-02 02:01:22.080: D/gralloc_goldfish(901): Emulator without GPU emulation detected.
03-02 02:01:27.570: D/AndroidRuntime(901): Shutting down VM
03-02 02:01:27.570: W/dalvikvm(901): threadid=1: thread exiting with uncaught exception      (group=0xb3aaeba8)
03-02 02:01:27.650: E/AndroidRuntime(901): FATAL EXCEPTION: main
03-02 02:01:27.650: E/AndroidRuntime(901): Process: co.sds.iitr.bullsandcows, PID: 901
03-02 02:01:27.650: E/AndroidRuntime(901): java.lang.NullPointerException
03-02 02:01:27.650: E/AndroidRuntime(901):  at     co.sds.iitr.bullsandcows.MainActivity.onClick(MainActivity.java:45)
03-02 02:01:27.650: E/AndroidRuntime(901):  at  android.view.View.performClick(View.java:4438)
03-02 02:01:27.650: E/AndroidRuntime(901):  at android.view.View$PerformClick.run(View.java:18422)
03-02 02:01:27.650: E/AndroidRuntime(901):  at android.os.Handler.handleCallback(Handler.java:733)
03-02 02:01:27.650: E/AndroidRuntime(901):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-02 02:01:27.650: E/AndroidRuntime(901):  at android.os.Looper.loop(Looper.java:136)
03-02 02:01:27.650: E/AndroidRuntime(901):  at android.app.ActivityThread.main(ActivityThread.java:5017)
03-02 02:01:27.650: E/AndroidRuntime(901):  at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:01:27.650: E/AndroidRuntime(901):  at java.lang.reflect.Method.invoke(Method.java:515)
03-02 02:01:27.650: E/AndroidRuntime(901):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-02 02:01:27.650: E/AndroidRuntime(901):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-02 02:01:27.650: E/AndroidRuntime(901):  at dalvik.system.NativeStart.main(Native Method)

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

getInteger方法替换为parseInt,将String转换为Integer使用parseInt方法

根据java doc

getInteger(String nm)

确定具有指定名称的系统属性的整数值。如果没有具有指定名称的属性,如果指定的名称为空或null,或者属性没有正确的数字格式,则{{ 1}}被返回。

答案 1 :(得分:0)

你只需在MainActivity中尝试这样....

done.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent (MainActivity.this , GuessActivtiy.class);
            intent.putExtra("num", n);
            startActivity(intent);

        }
    });