抱歉!!应用程序XXXX意外停止..请再试一次

时间:2014-05-07 07:27:55

标签: android

我是android developmnt的初学者。我按照一系列教程编写了一段代码。每当我在模拟器中运行应用程序时,它就会显示出来#34;抱歉!应用程序XXXX意外停止了......请再试一次。"请帮我解决这个错误。 这是我用过的代码......

package newapp.com;

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.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity 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.tdisplay);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
        }
    });

}}

这是我使用的xml代码....

<LinearLayout 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:orientation="vertical"
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="newapp.com.MainActivity$PlaceholderFragment" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="The sum is" 
    android:textSize="45dp"
    android:gravity="center"
    android:id="@+id/tdisplay"
    />
<Button 
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="add"
    android:id="@+id/badd"
    />

<Button
    android:id="@+id/bsub"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="subtract" />

</LinearLayout>

这是清单......

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="newapp.com"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="newapp.com.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

这是我的logcat ...以及logcat如何解决此问题。

05-07 13:22:31.058: I/dalvikvm(609): Could not find method 
android.content.pm.PackageManager.getActivityLogo, referenced from method 
android.support.v7.internal.widget.ActionBarView.<init>
05-07 13:22:31.058: W/dalvikvm(609): VFY: unable to resolve virtual method 318:           
Landroid/content/pm/PackageManager;.getActivityLogo   
(Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
05-07 13:22:31.058: D/dalvikvm(609): VFY: replacing opcode 0x6e at 0x008b
05-07 13:22:31.058: I/dalvikvm(609): Could not find method     
android.content.pm.ApplicationInfo.loadLogo, referenced from method    
android.support.v7.internal.widget.ActionBarView.<init>
05-07 13:22:31.068: W/dalvikvm(609): VFY: unable to resolve virtual method 314:     
Landroid/content/pm/ApplicationInfo;.loadLogo     
(Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
05-07 13:22:31.088: D/dalvikvm(609): VFY: replacing opcode 0x6e at 0x0099
05-07 13:22:31.108: D/dalvikvm(609): VFY: dead code 0x008e-0092 in     
Landroid/support/v7/internal/widget/ActionBarView;.<init> 
(Landroid/content/Context;Landroid/util/AttributeSet;)V
05-07 13:22:31.108: D/dalvikvm(609): VFY: dead code 0x009c-00a0 in     
Landroid/support/v7/internal/widget/ActionBarView;.<init> 
(Landroid/content/Context;Landroid/util/AttributeSet;)V
05-07 13:22:31.458: D/AndroidRuntime(609): Shutting down VM
05-07 13:22:31.458: W/dalvikvm(609): threadid=1: thread exiting with uncaught exception     
(group=0x4001d800)
05-07 13:22:31.478: E/AndroidRuntime(609): FATAL EXCEPTION: main
05-07 13:22:31.478: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start     
activity ComponentInfo{newapp.com/newapp.com.MainActivity}:     
java.lang.NullPointerException
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
android.os.Handler.dispatchMessage(Handler.java:99)
05-07 13:22:31.478: E/AndroidRuntime(609):  at android.os.Looper.loop(Looper.java:123)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
android.app.ActivityThread.main(ActivityThread.java:4627)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
java.lang.reflect.Method.invokeNative(Native Method)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
java.lang.reflect.Method.invoke(Method.java:521)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-07 13:22:31.478: E/AndroidRuntime(609):  at dalvik.system.NativeStart.main(Native     
Method)
05-07 13:22:31.478: E/AndroidRuntime(609): Caused by: java.lang.NullPointerException
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
newapp.com.MainActivity.onCreate(MainActivity.java:32)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-07 13:22:31.478: E/AndroidRuntime(609):  ... 11 more
05-07 13:22:41.409: I/Process(609): Sending signal. PID: 609 SIG: 9

2 个答案:

答案 0 :(得分:0)

在清单文件中的活动中应用主题

 android:theme="@style/Theme.AppCompat.Light"

在styles.xml中

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->

    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->


    </style>

答案 1 :(得分:0)

您发布的包含您使用的按钮和TextView的布局不是R.layout.activity_main

确保R.layout.activity_main是包含R.id.baddR.id.bsubR.id.tdisplay

的布局