很遗憾'应用名称'已停止工作

时间:2016-10-14 15:28:59

标签: android eclipse nullpointerexception

我是eclipse的初学者,我正在尝试显示我的按钮,但我的应用程序已停止工作,我不知道为什么。希望我的疑虑得到澄清。提前谢谢。

我的Log cat:

  10-14 14:56:15.527: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:15.857: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:56:16.037: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:16.107: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:56:16.296: D/AndroidRuntime(1283): Shutting down VM
10-14 14:56:16.296: W/dalvikvm(1283): threadid=1: thread exiting with uncaught exception (group=0x409bf1f8)
10-14 14:56:16.316: E/AndroidRuntime(1283): FATAL EXCEPTION: main
10-14 14:56:16.316: E/AndroidRuntime(1283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lpu/com.example.lpu.MainActivity}: java.lang.NullPointerException
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.os.Looper.loop(Looper.java:137)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at java.lang.reflect.Method.invoke(Method.java:511)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at dalvik.system.NativeStart.main(Native Method)
10-14 14:56:16.316: E/AndroidRuntime(1283): Caused by: java.lang.NullPointerException
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.example.lpu.MainActivity.onCreate(MainActivity.java:29)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.Activity.performCreate(Activity.java:4466)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-14 14:56:16.316: E/AndroidRuntime(1283):     ... 11 more
10-14 14:56:16.547: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:16.577: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:56:16.866: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:16.886: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:06.806: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:07.136: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:07.277: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:07.407: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:07.736: D/dalvikvm(1330): GC_FOR_ALLOC freed 65K, 3% free 9119K/9347K, paused 78ms
10-14 14:57:07.806: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:08.026: I/dalvikvm-heap(1330): Grow heap (frag case) to 16.883MB for 8294416-byte allocation
10-14 14:57:08.087: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:08.207: D/dalvikvm(1330): GC_CONCURRENT freed 1K, 2% free 17217K/17479K, paused 6ms+19ms
10-14 14:57:08.307: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:08.337: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:08.826: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:08.866: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:09.301: D/gralloc_goldfish(1330): Emulator without GPU emulation detected.
10-14 14:57:09.316: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:09.357: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:12.797: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:12.849: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:13.306: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:13.326: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:18.666: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:18.697: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:11.557: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:11.807: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:12.046: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:12.097: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:12.137: D/AndroidRuntime(1371): Shutting down VM
10-14 15:01:12.147: W/dalvikvm(1371): threadid=1: thread exiting with uncaught exception (group=0x409bf1f8)
10-14 15:01:12.157: E/AndroidRuntime(1371): FATAL EXCEPTION: main
10-14 15:01:12.157: E/AndroidRuntime(1371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lpu/com.example.lpu.MainActivity}: java.lang.NullPointerException
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.os.Looper.loop(Looper.java:137)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at java.lang.reflect.Method.invoke(Method.java:511)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at dalvik.system.NativeStart.main(Native Method)
10-14 15:01:12.157: E/AndroidRuntime(1371): Caused by: java.lang.NullPointerException
10-14 15:01:12.157: E/AndroidRuntime(1371):     at com.example.lpu.MainActivity.onCreate(MainActivity.java:29)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.Activity.performCreate(Activity.java:4466)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-14 15:01:12.157: E/AndroidRuntime(1371):     ... 11 more
10-14 15:01:12.547: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:12.586: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:12.816: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:12.826: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:04:31.236: I/Process(1371): Sending signal. PID: 1371 SIG: 9

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/game" >

     <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="• Game Development"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="21dp"
        android:text="BS Information Technology major in"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FDFEFE"
        android:textSize="15dp"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:text="PACUCOA Level II Reaccredited Status)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="18dp"
        android:text="COMPUTER STUDIES"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FDFEFE"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="16dp"
        android:text="BS Computer Science major in"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FDFEFE"
        android:textSize="15dp"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="14dp"
        android:text="• Mobile Application Development"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView9"
        android:layout_below="@+id/textView9"
        android:layout_marginTop="16dp"
        android:text="Associate in Computer Technology"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FDFEFE"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="(Bs Computer Science - PACUCOA level III Reacrideted Status)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13sp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_below="@+id/textView8"
        android:text="• Digital Animation"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView7"
        android:layout_centerVertical="true"
        android:text="• Multimedia Technologies"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

</RelativeLayout>

主要活动:

package com.example.lpu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener {

    Button button1;
    Button button2;
    ImageButton imageButton1;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        imageButton1 = (ImageButton)findViewById(R.id.imageButton1);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        imageButton1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        Intent intent = new Intent(MainActivity.this,BUTTON.class);
        this.startActivity(intent);
        break;

    case R.id.button2:
        Intent intent2 = new Intent(MainActivity.this,BUTOON.class);
        this.startActivity(intent2);
        break;

    case R.id.imageButton1:
        Intent intent3 = new Intent(MainActivity.this,CCS.class);
        this.startActivity(intent3);
        break;
    }

    }
}

这是Android Manifest

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
        <activity android:name=".BUTTON"></activity>
        <activity android:name=".BUTOON"></activity>
        <activity android:name=".CCS"></activity>

    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

您的日志文件指出了问题:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lpu/com.example.lpu.MainActivity}: java.lang.NullPointerException
...
10-14 14:56:16.316: E/AndroidRuntime(1283): Caused by: java.lang.NullPointerException
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.example.lpu.MainActivity.onCreate(MainActivity.java:29)

从最后一行可以看出,MainActivity类的第29行有一个空指针异常。看起来你正试图错误地设置onClickListener。而不是

imageButton1.setOnClickListener(this);
你可以这样做:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
    }
});

根据Android文档:https://developer.android.com/reference/android/widget/Button.html

答案 1 :(得分:0)

您的xml缺少要将OnClickListener添加到的元素。

您需要将一个标识为R.id.button1的按钮,另一个标识为R.id.button2的按钮和一个标识为R.id.imageButton1的imageButton添加到您的activity_main xml文件中。

在relativeLayout中添加类似的内容:

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"
        android:id="@+id/button1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2"
        android:id="@+id/button2"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="imageButton1"
        android:id="@+id/imageButton1"
        />