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

时间:2014-04-06 16:22:06

标签: java android eclipse nullpointerexception

我已经尝试构建我的第一个Android应用程序。每次我在模拟器中打开应用程序时,我都会收到一条消息,指出测试已停止工作。它必须是简单的东西。希望你能帮帮我。

  

04-06 15:43:47.806:W / dalvikvm(4275):threadid = 1:线程退出   未捕获的异常(组= 0xa614d908)

     

04-06 15:43:47.826:E / AndroidRuntime(4275):致命异常:主

     

04-06 15:43:47.826:E / AndroidRuntime(4275):   java.lang.RuntimeException:无法启动活动   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(原生方法)

     

04-06 15:43:47.826:E / AndroidRuntime(4275):引起:   显示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更多

以上是日志文件

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" />

ABove是activity_main.xml

3 个答案:

答案 0 :(得分:3)

而不是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;
}

答案 1 :(得分:2)

我看到了你的问题。 activity_main.xml包含一个框架布局,其中fragment_main.xml会膨胀。问题是,addsub都是空的,因为你在错误的布局中寻找它们。因此,首先调用FragmentTransaction并将所有按钮代码移动到PlaceHolderFragment的onCreateView,然后修复您的问题。我提供了固定代码,您只需复制它并从onCreate (...)中删除按钮代码

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

答案 2 :(得分:0)

最有可能

add = (Button) findViewById (R.id.bAdd);

返回null,因为您的activity_main视图中未定义该元素。