0

由于空指针异常,在活动设置期间强制关闭。

错误日志:

08-02 12:50:33.913: E/main(383): Birds clicked on - new activity starting....id
08-02 12:55:22.977: D/AndroidRuntime(383): Shutting down VM
08-02 12:55:22.977: W/dalvikvm(383): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-02 12:55:23.104: E/AndroidRuntime(383): FATAL EXCEPTION: main
08-02 12:55:23.104: E/AndroidRuntime(383): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kissta.safari/com.kissta.safari.viewscreen}: java.lang.NullPointerException
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.os.Looper.loop(Looper.java:130)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-02 12:55:23.104: E/AndroidRuntime(383):  at java.lang.reflect.Method.invokeNative(Native Method)
08-02 12:55:23.104: E/AndroidRuntime(383):  at java.lang.reflect.Method.invoke(Method.java:507)
08-02 12:55:23.104: E/AndroidRuntime(383):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-02 12:55:23.104: E/AndroidRuntime(383):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-02 12:55:23.104: E/AndroidRuntime(383):  at dalvik.system.NativeStart.main(Native Method)
08-02 12:55:23.104: E/AndroidRuntime(383): Caused by: java.lang.NullPointerException
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.Activity.findViewById(Activity.java:1647)
08-02 12:55:23.104: E/AndroidRuntime(383):  at com.kissta.safari.viewscreen.<init>(viewscreen.java:125)
08-02 12:55:23.104: E/AndroidRuntime(383):  at java.lang.Class.newInstanceImpl(Native Method)
08-02 12:55:23.104: E/AndroidRuntime(383):  at java.lang.Class.newInstance(Class.java:1409)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-02 12:55:23.104: E/AndroidRuntime(383):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-02 12:55:23.104: E/AndroidRuntime(383):  ... 11 more

根据日志命令,我的应用程序在打开 viewscreen 或 viewxml 时似乎失败了:

// ...abridged...import android.widget.ImageButton; 

public class main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainxml);

// ... abridged
ImageButton ibone = (ImageButton) findViewById(R.id.imageButton1);
ibone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent explicitIntent1 = new Intent(main.this,viewscreen.class);
                explicitIntent1.putExtra("id", 1);
                startActivity(explicitIntent1);
                Log.e("main", "Birds clicked on - new activity starting...." + "id");
            }
        });


....further abridged...

显现:

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

<application
    android:icon="@drawable/iconanimal"
    android:label="@string/app_name"
    android:screenOrientation="landscape"
    android:theme="@style/Theme.Transparent" >
    <activity
        android:name=".splash"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".main"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
    </activity>
    <activity
        android:name=".viewscreen"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
</activity>

</application>

</manifest>

在视屏活动中:

package com.kissta.safari;

import java.util.ArrayList;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class viewscreen extends Activity implements OnClickListener,
    OnGestureListener {

// references to our image
private animals animalclass = new animals();

int TotalAnimals; // starting from 0 of course (so an 11=12 fruit)
int currentAnimal = 0;

boolean active = true;
long starttime = System.currentTimeMillis();
long timedelay = 50;

ArrayList<Integer> imagearray = new ArrayList<Integer>();
ArrayList<Integer> soundarray = new ArrayList<Integer>();
// ArrayList<String> printarray = new ArrayList<String>();

private ViewSwitcher switcher;
private GestureDetector gesturedetector;
public boolean viewone = true;
public int menuoption;
Handler myHandler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("viewscreen1", "xml loading"); // does not reach here
    setContentView(R.layout.viewxml);

永远不会触发上面的日志消息,这意味着永远不会到达 viewxml.xml(???)。

这是 viewxml.xml:

`<?xml version="1.0" encoding="utf-8"?>
<!-- Note:  Can't add more than 2 views to a ViewSwitcher -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fullscreenrelative"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
>

<ViewSwitcher
    android:id="@+id/viewSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/bottomtablerow"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal|top"
    android:inAnimation="@anim/in_animation"
    android:outAnimation="@anim/out_animation" >

    <RelativeLayout
        android:id="@+id/view1relative"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/swan"
        android:gravity="center_horizontal" >

        <ImageView
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/clear32" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/view2relative"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/kangaroo" >

        <ImageView
            android:id="@+id/view2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:src="@drawable/clear32" />
    </RelativeLayout>
</ViewSwitcher>

<TableRow
    android:id="@+id/bottomtablerow"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:layout_alignParentBottom="true"
    android:background="#303030"
    android:gravity="center_horizontal" >

    <ImageButton
        android:id="@+id/btnPrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_weight=".2"
        android:background="@drawable/whiteleft"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".1"
        android:src="@drawable/clear" />

    <TextView
        android:id="@+id/textll"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_gravity="center_horizontal|top"
        android:layout_weight="1"
        android:gravity="center"
        android:includeFontPadding="false"
        android:padding="10dip"
        android:text="@string/app_name"
        android:textColor="#FFFFFF"
        android:textSize="25sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".1"
        android:src="@drawable/clear" />

    <ImageButton
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_weight=".2"
        android:background="@drawable/whiteright"
        android:textStyle="bold" />
</TableRow>

</RelativeLayout>`

PS 我现在也收到一个可能无关的 AVD 错误:“模拟器:错误:无法加载 OpenGLES 仿真库:无法加载 DLL!” 不过,我根本不相信我在使用 OpenGL。请参阅http://code.google.com/p/android/issues/detail?id=33336上的常见错误报告

PSS 我的应用程序也拒绝使用非谷歌 api 虚拟机

PSS 这里是所要求的完整主文件:package com.kissta.safari;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

public class main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainxml);
    // android:tileMode="repeat"

    Log.e("main", "main all good");

    SoundManager.getInstance();
    SoundManager.initSounds(main.this);
    SoundManager.loadSounds();
    Log.e("main", "sm LoadSounds worked, all done.");

        // SECTION FOR GAME CHOICE BUTTONS!!!!!!!!!
        // ***
        ImageButton ibone = (ImageButton) findViewById(R.id.imageButton1);
        ibone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent explicitIntent1 = new Intent(main.this,viewscreen.class);
                explicitIntent1.putExtra("id", 1);
                startActivity(explicitIntent1);
                Log.e("main", "Birds clicked on - new activity starting...." + "id"); // it gets this far perfectly fine
            }
        });

        // ***

        ImageButton ibtwo = (ImageButton) findViewById(R.id.imageButton2);
        ibtwo.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Playsound2.stop();
                Intent explicitIntent2 = new Intent(main.this,
                        viewscreen.class);
                explicitIntent2.putExtra("id", 2);
                startActivity(explicitIntent2);
            }
        });

        // ***

        ImageButton ibthree = (ImageButton) findViewById(R.id.imageButton3);
        ibthree.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Playsound2.stop();
                Intent explicitIntent3 = new Intent(main.this,
                        viewscreen.class);
                explicitIntent3.putExtra("id", 3);
                startActivity(explicitIntent3);
            }
        });

        // ***

        ImageButton ibfour = (ImageButton) findViewById(R.id.imageButton4);
        ibfour.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Playsound2.stop();
                Intent explicitIntent4 = new Intent(main.this,
                        viewscreen.class);
                explicitIntent4.putExtra("id", 4);
                startActivity(explicitIntent4);
            }
        });

        ImageButton ibfive = (ImageButton) findViewById(R.id.imageButton5);
        ibfive.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Playsound2.stop();
                Intent explicitIntent5 = new Intent(main.this,
                        viewscreen.class);
                explicitIntent5.putExtra("id", 5);
                startActivity(explicitIntent5);
            }
        });

        ImageButton ibsix = (ImageButton) findViewById(R.id.imageButton6); // settings - adjust
        ibsix.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Playsound2.stop();
                Intent explicitIntent6 = new Intent(main.this,
                        viewscreen.class);
                explicitIntent6.putExtra("id", 6);
                startActivity(explicitIntent6);
            }
        });

        ImageButton ibsev = (ImageButton) findViewById(R.id.imageButtonau); // settings - adjust
        ibsev.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Playsound2.stop();
                Intent explicitIntent6 = new Intent(main.this,
                        viewscreen.class);
                explicitIntent6.putExtra("id", 7);
                startActivity(explicitIntent6);
            }
        });




        // / *************** END OF GAME CHOICE BUTTONS


    }



}
4

1 回答 1

0
Intent explicitIntent1 = new Intent(main.this,viewscreen.class);
explicitIntent1.putExtra("id", 1);
startActivity(explicitIntent1);
Log.e("main", "this finished ok"); // app gets to here fine

在这段代码之后,出现了类似于

findViewById(..

检查您的 xml 中是否存在此 ID

于 2012-08-02T09:31:33.303 回答