java - Android应用程序已停止工作-eclipse

标签 java android eclipse nullpointerexception

我一直在尝试构建我的第一个 Android 应用程序。每次我在模拟器中打开应用程序时,我都会收到一条消息,提示测试已停止工作。这一定是简单的事情。希望你能帮助我。

04-06 15:43:47.806: W/dalvikvm(4275): threadid=1: thread exiting with uncaught exception (group=0xa614d908)

04-06 15:43:47.826: E/AndroidRuntime(4275): FATAL EXCEPTION: main

04-06 15:43:47.826: E/AndroidRuntime(4275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: java.lang.NullPointerException

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.access$600(ActivityThread.java:141)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.os.Handler.dispatchMessage(Handler.java:99)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.os.Looper.loop(Looper.java:137)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.main(ActivityThread.java:5041)

04-06 15:43:47.826: E/AndroidRuntime(4275): at java.lang.reflect.Method.invokeNative(Native Method)

04-06 15:43:47.826: E/AndroidRuntime(4275): at java.lang.reflect.Method.invoke(Method.java:511)

04-06 15:43:47.826: E/AndroidRuntime(4275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

04-06 15:43:47.826: E/AndroidRuntime(4275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

04-06 15:43:47.826: E/AndroidRuntime(4275): at dalvik.system.NativeStart.main(Native Method)

04-06 15:43:47.826: E/AndroidRuntime(4275): Caused by: java.lang.NullPointerException

04-06 15:43:47.826: E/AndroidRuntime(4275): at com.test.MainActivity.onCreate(MainActivity.java:30)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.Activity.performCreate(Activity.java:5104)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

04-06 15:43:47.826: E/AndroidRuntime(4275): ... 11 more

以上是日志文件

public class Main extends ActionBarActivity {

    int counter ;
        Button add, sub ;
        TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      counter = 0;
          add = (Button) findViewById (R.id.bAdd);
          sub = (Button) findViewById (R.id.bsub);
          display = (TextView) findViewById(R.id.tvDisplay);
          add.setOnClickListener(new  View.OnClickListener() {

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

            }
        });

          sub.setOnClickListener(new  View.OnClickListener() {

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


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
               .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

        @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 boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

/**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

以上是主类

<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: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.test.MainActivity$PlaceholderFragment" >

   <TextView
        android:id="@+id/tvDisplay"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/bsub"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/bsub"
        android:gravity="center"
        android:text="Your total is 0"
        android:textSize="45dp" />

    <Button
        android:id="@+id/badd"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="78dp"
        android:gravity="center"
        android:text="Add one"
        android:textSize="20dp" />

    <Button
        android:id="@+id/bsub"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/badd"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Subtract one"
        android:textSize="20dp" />



</RelativeLayout>

上面是fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.Hello.firstapp.Main"
    tools:ignore="MergeRootFrame" />

上面是activity_main.xml

最佳答案

而不是 onCreate()将所有初始化移至 onCreateView()如那些buttonstextview在你 fragment_main.xml这就是为什么你得到 NPE如那些buttonstextview不是 activity_main.xml 的一部分。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
counter = 0;
add = (Button) rootView.findViewById (R.id.bAdd);
sub = (Button) rootView.findViewById (R.id.bsub);
display = (TextView) rootView.findViewById(R.id.tvDisplay);
add.setOnClickListener(new  View.OnClickListener() {

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

    }
   });

  sub.setOnClickListener(new  View.OnClickListener() {

   @Override
   public void onClick(View v) {
   // TODO Auto-generated method stub
   counter --;
   display.setText("Your total is " + counter);
   }
  });
 return rootView;
}

关于java - Android应用程序已停止工作-eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22896682/

相关文章:

java - Android Studio 预览版未显示 ListView 示例

eclipse - eclipse 中自动发布

java - 自定义异常类未编译到 .class 文件中

java - 玻璃面板控制台式文本显示组件

java - Java 类中扩展集合的只读迭代器

android - 保持房间连接

Android:数据绑定(bind)文本未更新

java - 如何检测 SWT 对话框已打开并且可见?

java - 在 Java 中交换字符串中的字符

java - JDBI 如何在防止 SQL 注入(inject)的同时动态创建 WHERE 子句?