无法启动活动 - ComponentInfo错误

时间:2013-11-19 14:27:33

标签: 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

这是我的清单

<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

我主要使用包含,我更容易理解。

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

2 个答案:

答案 0 :(得分:1)

错误发生在第269行? 我无法确定您在开始活动中提供135行的行。

然而,我的猜测是你的findViewById方法正在寻找一个不存在的资源。检查您的xml是否包含您在活动中引用的所有按钮/线性布局/图像视图等。

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

答案 1 :(得分:0)

我已经检查了代码,但是行数不正确,这个错误的原因肯定是清楚的,findViewById找不到id的视图,请重新检查你的代码

相关问题