【问题标题】:NULLPOINT EXCEPTION ERROR during Simple Android app development [duplicate]简单的Android应用程序开发期间的NULLPOINT EXCEPTION ERROR [重复]
【发布时间】:2014-05-02 14:21:03
【问题描述】:

我正在尝试使用 Android ADT 开发这个简单的应用程序,但出现空点异常错误。它在第 32 行返回空点异常错误,即

add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("your total is" + counter);


        }
    });

我的整个java代码是

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.*;

public class MainActivity extends ActionBarActivity {

int counter;
Button add , subtract;
TextView display;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;
    add = (Button) findViewById(R.id.badd1);
    subtract = (Button) findViewById(R.id.bsub1);
    display = (TextView) findViewById(R.id.textView1);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("your total is" + counter);


        }
    });

    subtract.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("your total is" + counter);

        }
    });
}
@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;
}

}

我的 xml 代码是

<RelativeLayout 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:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.newboston.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="your total is 0"
    android:textSize="45dp" />

    <Button
        android:id="@+id/badd1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="34dp"
        android:layout_centerHorizontal="true"
        android:text="add one"
        android:textSize="20dp" />

    <Button
        android:id="@+id/bsub1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/badd1"
        android:layout_below="@+id/badd1"
        android:layout_marginTop="34dp"
        android:text="subtract one"
        android:textSize="20dp" />

</RelativeLayout>

我的 logCat 文件是

03-25 23:10:29.219: D/AndroidRuntime(1211): Shutting down VM
03-25 23:10:29.229: W/dalvikvm(1211): threadid=1: thread exiting with uncaught exception (group=0xb2b10ba8)
03-25 23:10:29.279: E/AndroidRuntime(1211): FATAL EXCEPTION: main
03-25 23:10:29.279: E/AndroidRuntime(1211): Process: com.example.newboston, PID: 1211
03-25 23:10:29.279: E/AndroidRuntime(1211): java.lang.RuntimeException: Unable to start activity            ComponentInfo{com.example.newboston/com.example.newboston.MainActivity}: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at      android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at android.os.Looper.loop(Looper.java:136)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invoke(Method.java:515)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at dalvik.system.NativeStart.main(Native Method)
 03-25 23:10:29.279: E/AndroidRuntime(1211): Caused by: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211):     at com.example.newboston.MainActivity.onCreate(MainActivity.java:32)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at android.app.Activity.performCreate(Activity.java:5231)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 23:10:29.279: E/AndroidRuntime(1211):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-25 23:10:29.279: E/AndroidRuntime(1211):     ... 11 more
03-25 23:10:36.449: I/Process(1211): Sending signal. PID: 1211 SIG: 9

【问题讨论】:

  • @Brian Roach 不,不是。
  • 您的代码在复制到新创建的 Android 项目时无需更改即可在真实设备上运行。您可以在运行之前尝试清理和重建项目。
  • @user3457567 您的代码有效。我刚测试过。发布你的 logcat。
  • @Onik 当有人在这里转储他们的代码并说“我有一个 NPE”时,是的,它是。至少这是更好的做法,而不是仅仅因为“不清楚你在问什么”而关闭。 SO 并不是免费的调试服务。
  • 您的代码可以正常工作。您这边还有其他问题

标签: java android android-emulator nullpointerexception adt


【解决方案1】:

如果您使用多个布局文件(例如 layout-land、layout-sw360dp),请确保您引用的所有元素都存在于所有这些文件中。

【讨论】:

  • 我发布了我的 Logcat。可以看看吗?
猜你喜欢
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 2016-10-21
  • 2012-04-06
  • 2013-04-26
  • 1970-01-01
相关资源
最近更新 更多