遗憾的是<appname>已停止</appname>

时间:2014-06-04 19:54:42

标签: android android-emulator

我在eclipse.at中编写了以下代码,首先它一直在工作,直到我在MainActivity.java文件中做了一些更改,就像让按钮做一些事情一样。当我运行应用程序时,出现以下错误消息:不幸的是已停止。请提前帮助我。

MainActivity.java

package com.example.jony;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
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.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {
int counter;
    Button add;
    Button sub;
    TextView tvdisplay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        counter = 0;
        add =(Button) findViewById(R.id.badd);

        tvdisplay = (TextView) findViewById(R.id.tvdisplay);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter = counter + 1;
                tvdisplay.setText("Your total is" + counter);
                        }
        });
        sub.setOnClickListener(new View.OnClickListener() {

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

fragment_main.xml

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     tools:context="com.example.jony.MainActivity$PlaceholderFragment"
     android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:gravity="center"
        android:text="@string/calci" />
    <Button
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:text="@string/add"
        android:textSize="35sp"
        android:id="@+id/badd" />
   <Button
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:text="@string/sub"
        android:textSize="35sp"
        android:id="@+id/bsub" />

</LinearLayout>

activity_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="fill_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" />

LogCat输出:

06-05 00:42:13.452: E/Trace(1924): error opening trace file: No such file or directory (2)
06-05 00:42:14.492: D/AndroidRuntime(1924): Shutting down VM
06-05 00:42:14.492: W/dalvikvm(1924): threadid=1: thread exiting with uncaught       exception      (group=0xb5e2d288)
06-05 00:42:14.531: E/AndroidRuntime(1924): FATAL EXCEPTION: main
06-05 00:42:14.531: E/AndroidRuntime(1924): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.example.jony/com.example.jony.MainActivity}: java.lang.NullPointerException
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.os.Looper.loop(Looper.java:137)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.main(ActivityThread.java:4745)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at java.lang.reflect.Method.invoke(Method.java:511)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at                    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at dalvik.system.NativeStart.main(Native Method)
06-05 00:42:14.531: E/AndroidRuntime(1924): Caused by: java.lang.NullPointerException
06-05 00:42:14.531: E/AndroidRuntime(1924):     at com.example.jony.MainActivity.onCreate(MainActivity.java:40)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.Activity.performCreate(Activity.java:5008)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-05 00:42:14.531: E/AndroidRuntime(1924):     ... 11 more

1 个答案:

答案 0 :(得分:2)

您的xml中没有ID为tvdisplay的TextView,这是您尝试使用

设置的内容
tvdisplay = (TextView) findViewById(R.id.tvdisplay);

因此,当您尝试在onClick中设置文本时,它将为您提供一个用于tvdisplay的NPE。将TextView xml更改为

<TextView
    android:id="@+id/tvdisplay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:gravity="center"
    android:text="@string/calci" />