简体   繁体   English

Android应用程式已停止运作-eclipse

[英]Android application has stopped working -eclipse

I have been tried to build my first Android Application. 我已经尝试构建我的第一个Android应用程序。 Every time I open the application in the emulator, I just get a message that says test has stopped working. 每次我在模拟器中打开应用程序时,我都会收到一条消息,指出测试已停止工作。 It must be something simple. 它一定很简单。 Hope you can help me. 希望您能够帮助我。

04-06 15:43:47.806: W/dalvikvm(4275): threadid=1: thread exiting with uncaught exception (group=0xa614d908) 04-06 15:43:47.806:W / dalvikvm(4275):threadid = 1:线程以未捕获的异常退出(group = 0xa614d908)

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

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):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):位于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):位于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):位于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):位于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):位于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):位于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):位于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.invokeNative(本机方法)

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 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):在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):位于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):在dalvik.system.NativeStart.main(本机方法)

04-06 15:43:47.826: E/AndroidRuntime(4275): Caused by: java.lang.NullPointerException 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):在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):位于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):位于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):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

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

Above is the log file 上面是日志文件

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

}

Above is the Main class 以上是主班

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

Above is the fragment_main.xml 上面是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 is the activity_main.xml 上面是activity_main.xml

Instead of onCreate() move all ur initialization into onCreateView() as those buttons and textview is in ur fragment_main.xml thats why u got NPE as those buttons and textview is not the part of 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;
}

I see your issue. 我看到你的问题。 activity_main.xml contains a frame layout, in which your fragment_main.xml is inflated. activity_main.xml包含一个框架布局,其中fragment_main.xml被夸大了。 The thing is that, both add and sub are null, as you're looking for them in the wrong layout. 事实是, addsub都为空,因为您在错误的布局中查找它们。 Thus, call the FragmentTransaction first and move all your button code to the onCreateView of the PlaceHolderFragment and your issue should be fixed. 因此,首先调用FragmentTransaction,然后将所有按钮代码移至PlaceHolderFragment的onCreateView上,您的问题应已修复。 I have provided the fixed code, all you have to do is copy it and remove the button code from onCreate (...) 我已经提供了固定代码,您所要做的就是复制它并从onCreate (...)删除按钮代码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;
}

Most likely 最有可能的

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

returns null , because the element is not defined on your activity_main view. 返回null ,因为未在activity_main视图上定义该元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM