java - 无法启动 Activity - ComponentInfo 错误

标签 java android runtimeexception

这是我尝试启动应用程序时遇到的错误。 有人能看出我做错了什么吗?

LogCat

11-19 14:01:39.993: E/AndroidRuntime(4046): FATAL EXCEPTION: main

11-19 14:01:39.993: E/AndroidRuntime(4046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zimzux.soundapprhk/com.zimzux.soundapprhk.Start}: java.lang.NullPointerException

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.ActivityThread.access$600(ActivityThread.java:123)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.os.Handler.dispatchMessage(Handler.java:99)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.os.Looper.loop(Looper.java:137)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.ActivityThread.main(ActivityThread.java:4424)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at java.lang.reflect.Method.invokeNative(Native Method)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at java.lang.reflect.Method.invoke(Method.java:511)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at dalvik.system.NativeStart.main(Native Method)

11-19 14:01:39.993: E/AndroidRuntime(4046): Caused by: java.lang.NullPointerException

11-19 14:01:39.993: E/AndroidRuntime(4046):     at com.zimzux.soundapprhk.Start.onCreate(Start.java:269)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.Activity.performCreate(Activity.java:4492)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

11-19 14:01:39.993: E/AndroidRuntime(4046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

11-19 14:01:39.993: E/AndroidRuntime(4046):     ... 11 more

11-19 14:01:40.213: I/dalvikvm(4046): threadid=3: reacting to signal 3

11-19 14:01:40.213: I/dalvikvm(4046): Wrote stack traces to '/data/anr/traces.txt'

11-19 14:01:40.503: I/dalvikvm(4046): threadid=3: reacting to signal 3

11-19 14:01:40.513: I/dalvikvm(4046): Wrote stack traces to '/data/anr/traces.txt'

11-19 14:02:38.883: I/Process(4046): Sending signal. PID: 4046 SIG: 9

这是我的 list

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zimzux.soundapprhk"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >

        <activity
            android:name=".Opstart"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.zimzux.soundapprhk.OPSTART" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Start"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.zimzux.soundapprhk.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


    </application>
</manifest>

这是我的 XML

我主要使用 include,这对我来说更容易有一个概述。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00A2E8"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:scrollbars="vertical|horizontal"
    tools:context=".MainActivity"
    tools:ignore="SpUsage" >

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="20"
        android:layout_gravity="top"
        android:background="#00A2E8"
        android:gravity="center_horizontal"
        android:layout_marginLeft="100dp"
        android:orientation="vertical" >

        <include
            android:id="@+id/topbanner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/topbanner_xml" />
        <include
            android:id="@+id/topbannerindstil"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/topbanner_indstil_xml" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="100"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >  

        <include
            android:id="@+id/start"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/mainstart_xml" />

        <include
            android:id="@+id/afspilmain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/afspil_main_xml" />        

        <include
            android:id="@+id/spilstopmain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/afspil_spilstop_xml" />
        <include
            android:id="@+id/indstil"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/indstil_xml" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bund"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="25"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <include
            android:id="@+id/bundbanner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/bundbanner_xml" />
        <include
            android:id="@+id/bundbannerindstil"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/bundbanner_indstil_xml" />

    </LinearLayout>

</LinearLayout>

这是我的 Java

对于所有其他 XML,有很多 findViewById。

package com.zimzux.soundapprhk;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Start extends Activity {
    MediaPlayer mp1;
    MediaPlayer mp2;

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


        final LinearLayout top = (LinearLayout) findViewById(R.id.top);
        final LinearLayout bund = (LinearLayout) findViewById(R.id.bund);
        final LinearLayout topbannerafspil = (LinearLayout) findViewById(R.id.topbanner);
        final LinearLayout bundbannerafspil = (LinearLayout) findViewById(R.id.bundbanner);
        final LinearLayout start = (LinearLayout) findViewById(R.id.start);
        final LinearLayout afspil = (LinearLayout) findViewById(R.id.afspilmain);
        final LinearLayout spilstop = (LinearLayout) findViewById(R.id.spilstopmain);
        final LinearLayout indstil = (LinearLayout) findViewById(R.id.indstil);
        final LinearLayout topbannerindstil = (LinearLayout) findViewById(R.id.topbannerindstil);
        final LinearLayout bundbannerindstil = (LinearLayout) findViewById(R.id.bundbannerindstil);

        ImageView btnstart = (ImageView) findViewById(R.id.btnstart);
        ImageView btnindstil = (ImageView) findViewById(R.id.btnindstil);
        @SuppressWarnings("unused")
        TextView textnowpl = (TextView) findViewById(R.id.textnowpl);

        Button btnscor = (Button) findViewById(R.id.btnscor);
        Button btntimeout = (Button) findViewById(R.id.btntimeout);
        Button btnudvisning = (Button) findViewById(R.id.btnudvisning);
        Button btnstraffe = (Button) findViewById(R.id.btnstraffe);
        Button btnindlob = (Button) findViewById(R.id.btnindlob);
        Button btnopvarmning = (Button) findViewById(R.id.btnopvarmning);
        Button btnhalvleg = (Button) findViewById(R.id.btnhalvleg);
        Button btnnextsong = (Button) findViewById(R.id.btnnextsong);
        Button btnspilstop = (Button) findViewById(R.id.btnspilstop);

        Button btnbasic = (Button) findViewById(R.id.btnbasic);
        Button btnspilswitchstop = (Button) findViewById(R.id.btnspilswitchstop);
        Button btnfadeud = (Button) findViewById(R.id.btnfadeud);
        Button btnstop = (Button) findViewById(R.id.btnstop);

        Button spilstop1 = (Button) findViewById(R.id.spilstop1);
        Button spilstop2 = (Button) findViewById(R.id.spilstop2);
        Button spilstop3 = (Button) findViewById(R.id.spilstop3);
        Button spilstop4 = (Button) findViewById(R.id.spilstop4);
        Button spilstop5 = (Button) findViewById(R.id.spilstop5);
        Button spilstop6 = (Button) findViewById(R.id.spilstop6);
        Button spilstop7 = (Button) findViewById(R.id.spilstop7);
        Button spilstop8 = (Button) findViewById(R.id.spilstop8);
        Button spilstop9 = (Button) findViewById(R.id.spilstop9);
        Button spilstop10 = (Button) findViewById(R.id.spilstop10);
        Button spilstop11 = (Button) findViewById(R.id.spilstop11);
        Button spilstop12 = (Button) findViewById(R.id.spilstop12);
        Button spilstop13 = (Button) findViewById(R.id.spilstop13);
        Button spilstop14 = (Button) findViewById(R.id.spilstop14);
        Button spilstop15 = (Button) findViewById(R.id.spilstop15);
        Button spilstop16 = (Button) findViewById(R.id.spilstop16);




        top.setVisibility(View.GONE);
        topbannerafspil.setVisibility(View.GONE);
        topbannerindstil.setVisibility(View.GONE);
        bund.setVisibility(View.GONE);
        bundbannerafspil.setVisibility(View.GONE);
        bundbannerindstil.setVisibility(View.GONE);

        start.setVisibility(View.VISIBLE);
        afspil.setVisibility(View.GONE);
        spilstop.setVisibility(View.GONE);
        indstil.setVisibility(View.GONE);


        btnstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){

                topbannerafspil.setVisibility(View.VISIBLE);
                bundbannerafspil.setVisibility(View.VISIBLE);
                start.setVisibility(View.GONE);
                afspil.setVisibility(View.VISIBLE);
                spilstop.setVisibility(View.GONE);
                indstil.setVisibility(View.GONE);
                topbannerindstil.setVisibility(View.GONE);
                bundbannerindstil.setVisibility(View.GONE);
                top.setVisibility(View.VISIBLE);
                bund.setVisibility(View.VISIBLE);
            }});

        btnindstil.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){

                topbannerafspil.setVisibility(View.GONE);
                bundbannerafspil.setVisibility(View.GONE);
                start.setVisibility(View.GONE);
                afspil.setVisibility(View.GONE);
                spilstop.setVisibility(View.GONE);
                indstil.setVisibility(View.VISIBLE);
                topbannerindstil.setVisibility(View.VISIBLE);
                bundbannerindstil.setVisibility(View.VISIBLE);
                top.setVisibility(View.VISIBLE);
                bund.setVisibility(View.VISIBLE);
            }});

        btnbasic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){


                afspil.setVisibility(View.VISIBLE);
                spilstop.setVisibility(View.GONE);
            }});

        btnspilswitchstop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){


                afspil.setVisibility(View.GONE);
                spilstop.setVisibility(View.VISIBLE);
            }});
}}

最佳答案

错误发生在第269行? 我无法确定是哪一行,因为您在开始 Activity 中提供了 135 行。

但是,我的猜测是您的 findViewById 方法正在查找不存在的资源。检查您的 xml 文件是否包含您在 Activity 中引用的所有按钮/线性布局/ ImageView 等。

如果不是,那么您需要将按钮等声明为类变量,然后在 on create 方法中初始化它们,从而允许在其他方法和内部类中访问。

关于java - 无法启动 Activity - ComponentInfo 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20074152/

相关文章:

java - 严重 : Unable to create initial connections of pool - tomcat 7 with context. xml 文件

android - findViewById 抛出 RuntimeException

java - 创建具有不同目标对象的方法引用列表?

java - 当 spring bean 定义文件多次加载到应用程序中时会发生什么?

java - JPA 对映射 @ManyToMany 的怀疑

java - 如何在安卓上使用文件锁?

javascript - 使用hibernate spring mvc检测数据库中的行

java - 'android.annotation.NonNull' 在 'android.annotation' 中不公开。无法从外部包访问

android - 以编程方式触发 Android 中的操作栏菜单项

java - 尽管我得到了结果,为什么我会收到此错误?