第一款Android App中的空指针异常

时间:2012-10-28 04:27:38

标签: android nullpointerexception

我正在尝试编写我的第一个Android应用,但我遇到了麻烦。我写了一个简单的Java程序来查找用户提供的数字的因子,我想将它移植到Java。我有一个接口的XML文件,它似乎运行正常,直到我添加了第一个Java类。现在它不会运行,log.cat说有一个空指针异常。到目前为止,我只有存根,但似乎它应该运行正常我已经做到这一点。我包含了log.cat文本,但是我太新了,因为它很有意义。

可能存在多个问题。添加Java文件后,我立即开始获得运行时错误,但我不认为它们是空指针异常。我认为,当我做出修改以解决已导致问题的问题时,就开始了。

感谢任何帮助。

这是Java文件:

import android.app.Activity;  <br>
import android.os.Bundle;  <br>
import android.widget.Button;   <br>
import android.widget.EditText;  <br>
import android.widget.TextView;  <br>

public class AndroidFactoringActivity extends Activity {

    // Instance Variables
    EditText userNumber;
    Button factorButton;
    TextView resultsField;
    int factorResults;

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    factorButton = (Button) findViewById(R.id.factorButton);
    userNumber = (EditText) findViewById(R.id.userNumber);
    factorResults = 1;
    resultsField.setText(String.valueOf(factorResults));
}   
}

这是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/askField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ask"
        android:textSize="24dp" />

    <EditText
        android:id="@+id/userNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/factorButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click" />

    <TextView
        android:id="@+id/resultsField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/tell"
        android:textSize="24dp" />
</LinearLayout>

以下是log.cat结果:

 03-31 23:58:53.579: D/AndroidRuntime(2804): Shutting down VM

    03-31 23:58:53.589: W/dalvikvm(2804): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

    03-31 23:58:53.589: E/AndroidRuntime(2804): Uncaught handler: thread main exiting due to uncaught exception

    03-31 23:58:53.650: E/AndroidRuntime(2804): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.dave_b.factoring/net.dave_b.factoring.AndroidFactoringActivity}: java.lang.NullPointerException

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.os.Handler.dispatchMessage(Handler.java:99)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.os.Looper.loop(Looper.java:123)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.main(ActivityThread.java:4363)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at java.lang.reflect.Method.invokeNative(Native Method)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at java.lang.reflect.Method.invoke(Method.java:521)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at dalvik.system.NativeStart.main(Native Method)

    03-31 23:58:53.650: E/AndroidRuntime(2804): Caused by: java.lang.NullPointerException

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at net.dave_b.factoring.AndroidFactoringActivity.onCreate(AndroidFactoringActivity.java:26)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     ... 11 more

    03-31 23:58:53.679: I/dalvikvm(2804): threadid=7: reacting to signal 3

    03-31 23:58:53.679: E/dalvikvm(2804): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

    03-31 23:59:57.629: I/Process(2804): Sending signal. PID: 2804 SIG: 9

    04-01 00:07:36.129: D/AndroidRuntime(3040): Shutting down VM

    04-01 00:07:36.129: W/dalvikvm(3040): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

    04-01 00:07:36.139: E/AndroidRuntime(3040): Uncaught handler: thread main exiting due to uncaught exception

    04-01 00:07:36.159: E/AndroidRuntime(3040): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.dave_b.factoring/net.dave_b.factoring.AndroidFactoringActivity}: java.lang.NullPointerException

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.os.Handler.dispatchMessage(Handler.java:99)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.os.Looper.loop(Looper.java:123)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.main(ActivityThread.java:4363)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at java.lang.reflect.Method.invokeNative(Native Method)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at java.lang.reflect.Method.invoke(Method.java:521)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at dalvik.system.NativeStart.main(Native Method)

    04-01 00:07:36.159: E/AndroidRuntime(3040): Caused by: java.lang.NullPointerException

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at net.dave_b.factoring.AndroidFactoringActivity.onCreate(AndroidFactoringActivity.java:26)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     ... 11 more

    04-01 00:07:36.199: I/dalvikvm(3040): threadid=7: reacting to signal 3

    04-01 00:07:36.199: E/dalvikvm(3040): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

    04-01 00:07:40.329: I/Process(3040): Sending signal. PID: 3040 SIG: 9

3 个答案:

答案 0 :(得分:2)

resultsField.setText(String.valueOf(factorResults));

resultsFieldnull,您正在尝试将值设置为null。

resultsField= (TextView ) findViewById(R.id.userNumber);

在设置值之前获取textview实例,否则resultsField将为null,并且null上的所有操作都会在NullPointerException中生成。

答案 1 :(得分:0)

你有一个空指针,因为你没有初始化变量resultsField(你刚刚声明它)。

使用时

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

您甚至必须初始化参考resultsField。 这可以使用

完成

resultsField= (TextView ) findViewById(R.id.resultsField);

语句onCreate()

之后的setContentView()中的

答案 2 :(得分:0)

您需要初始化userNumer和resultsFiled,如下所示

userNumber =(EditText)findViewById(R.id.userNumber); resultsField =(TextView)findViewById(R.id.resultsField);