0

我不断收到错误“无法实例化活动 ComponentInfo{com.game.yugioh/com.game.yugioh.MainActivity}:java.lang.NullPointerException”,我不知道如何解决它。

日志猫:

10-19 16:03:39.889  17612-17612/com.game.yugioh E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.game.yugioh/com.game.yugioh.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
        at android.app.ActivityThread.access$600(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:4987)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java:1851)
        at com.game.yugioh.MainActivity.<init>(MainActivity.java:12)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1319)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
        at android.app.ActivityThread.access$600(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:4987)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

MainActivity.java:

package com.game.yugioh;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {

Bitmap[] hand;
ImageView handCard1 = (ImageView) findViewById(R.id.handCard1);
ImageView handCard2 = (ImageView) findViewById(R.id.handCard2);
ImageView handCard3 = (ImageView) findViewById(R.id.handCard3);
ImageView handCard4 = (ImageView) findViewById(R.id.handCard4);
ImageView handCard5 = (ImageView) findViewById(R.id.handCard5);
ImageView handCard6 = (ImageView) findViewById(R.id.handCard6);
Deck synchronWarriorsDeck = new Deck("Synchron Warriors");

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

    start();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private void start() {

    for (int i = 0; i < 5; i++) {
        Field.draw();
        addHandCard();
    }
}

private void addHandCard() {
    int location = Field.getHandCounter();
    String next = String.valueOf(Field.getNext());

    if (location == 1) {
        handCard1.setImageResource(getResources().getIdentifier(next, "drawable", getPackageName()));
    } else if (location == 2) {
        handCard2.setImageResource(getResources().getIdentifier(next, "drawable", getPackageName()));
    } else if (location == 3) {
        handCard3.setImageResource(getResources().getIdentifier(next, "drawable", getPackageName()));
    } else if (location == 4) {
        handCard4.setImageResource(getResources().getIdentifier(next, "drawable", getPackageName()));
    } else if (location == 5) {
        handCard5.setImageResource(getResources().getIdentifier(next, "drawable", getPackageName()));
    } else if (location == 6) {
        handCard6.setImageResource(getResources().getIdentifier(next, "drawable", getPackageName()));
    }
 }

}

字段.java:

package com.game.yugioh;

import java.util.Arrays;
import java.util.List;

public class Field {

private static String name = Deck.getName();
private static int originalSize;
private static int currentSize;
private static int handCounter = 0;
private static int deckCounter = 0;
private static List<String[]> deck = Arrays.asList(Deck.synchronWarriors());

private static int check = 0;
private static int handLocation;

private Field() {
    originalSize = 0;
    currentSize = 0;
}

public static int getOriginalSize() {
    if (name.equals("Synchron Warriors")) {
        originalSize = Deck.synchronWarriors().length;
    }
    return originalSize;
}

public static int getCurrentSize() {
    if (name.equals("Synchron Warriors")) {
        currentSize = originalSize - deckCounter;
    }

    return currentSize;
}

public static void draw() {
    getNext();
    handCounter++;
    deckCounter++;
}

public static int getHandCounter() {
    return handCounter;
}

public int getDeckCounter() {
    return deckCounter;
}

public static String[] getNext() {
    int idx = deck.indexOf(deckCounter);
    if (idx < 0 || idx == deck.size())
        return null;
    return deck.get(idx);
}
}

甲板.java:

package com.game.yugioh;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Deck {

private static String name;
private static String firstCard;
private static String[][] synchronWarriors;
private static List<String[]> deck = Arrays.asList(synchronWarriors);

public Deck(String n) {

    name = n;
}

public static String getName() {
    if (name.equals("Synchron Warriors")) {
        firstCard = synchronWarriors[0][0];
        if (firstCard.equals("Changer Synchron")) {
            name = "Synchron Warriors";
        }
    }
    return name;
}

public void shuffle() {
    Collections.shuffle(Arrays.asList(synchronWarriors));
}

public static String[][] synchronWarriors() {

    String[][] synchronWarriors = {{"changer_synchron", "changer_synchron", "dandylion", "junk_synchron", "junk_synchron",
            "junk_synchron", "level_eater", "level_eater", "level_eater", "quickdraw_synchron", "quickdraw_synchron",
            "quickdraw_synchron", "quillbolt_hedgehog", "quillbolt_hedgehog", "quillbolt_hedgehog", "synchron_explorer",
            "synchron_explorer", "synchron_explorer", "tuningware", "tuningware", "tuningware", "turbo_synchron",
            "turbo_synchron", "unknown_synchron"},
            {"dark_hole", "mind_control", "mst", "mst", "scapegoat", "tuning", "tuning", "tuning"},
            {"bottomless_trap_hole", "call_of_the_haunted", "call_of_the_haunted", "call_of_the_haunted", "limit_reverse",
                    "limit_reverse", "limit_reverse", "mirror_force", "mirror_force", "solen_warning", "starlight_road",
                    "starlight_road"}};

    return synchronWarriors;
}
}

活动主.xml:

<RelativeLayout 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: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=".MainActivity" >

<ImageView
    android:id="@+id/opponentHandCard1"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignParentEnd="false"
    android:contentDescription="opponent_card1_in_hand"
    android:src="@drawable/card_back" />

<ImageView
    android:id="@+id/opponentHandCard2"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentHandCard1"
    android:layout_toRightOf="@+id/opponentHandCard1"
    android:contentDescription="opponent_card2_in_hand"
    android:src="@drawable/card_back" />

<ImageView
    android:id="@+id/opponentHandCard3"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentHandCard2"
    android:layout_toRightOf="@+id/opponentHandCard2"
    android:contentDescription="opponent_card3_in_hand"
    android:src="@drawable/card_back" />

<ImageView
    android:id="@+id/opponentHandCard4"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentHandCard3"
    android:layout_toRightOf="@+id/opponentHandCard3"
    android:contentDescription="opponent_card4_in_hand"
    android:src="@drawable/card_back" />

<ImageView
    android:id="@+id/opponentHandCard5"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentHandCard4"
    android:layout_toRightOf="@+id/opponentHandCard4"
    android:contentDescription="opponent_card5_in_hand"
    android:src="@drawable/card_back" />

<ImageView
    android:id="@+id/opponentHandCard6"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentHandCard5"
    android:layout_toRightOf="@+id/opponentHandCard5"
    android:contentDescription="opponent_card6_in_hand"
    android:src="@drawable/card_back" />

<ImageView
    android:id="@+id/handCard1"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignLeft="@+id/opponentHandCard1"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="11dp"
    android:src="@drawable/card_back"
    android:onClick="onClickHandCard1" />

<ImageView
    android:id="@+id/handCard2"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/handCard1"
    android:layout_toRightOf="@+id/handCard1"
    android:src="@drawable/card_back"
    android:onClick="onClickHandCard2" />

<ImageView
    android:id="@+id/handCard3"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/handCard2"
    android:layout_toRightOf="@+id/handCard2"
    android:src="@drawable/card_back"
    android:onClick="onClickHandCard3" />

<ImageView
    android:id="@+id/handCard4"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/handCard3"
    android:layout_toRightOf="@+id/handCard3"
    android:src="@drawable/card_back"
    android:onClick="onClickHandCard4" />

<ImageView
    android:id="@+id/handCard5"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignBottom="@+id/handCard4"
    android:layout_toRightOf="@+id/handCard4"
    android:src="@drawable/card_back"
    android:onClick="onClickHandCard5" />

<ImageView
    android:id="@+id/handCard6"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/handCard5"
    android:layout_toRightOf="@+id/handCard5"
    android:src="@drawable/card_back"
    android:onClick="onClickHandCard6" />

<ImageView
    android:id="@+id/opponentSpellTrapZone1"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignLeft="@+id/opponentHandCard1"
    android:layout_below="@+id/opponentHandCard1"
    android:layout_marginTop="19dp" />

<ImageView
    android:id="@+id/opponentSpellTrapZone2"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignBottom="@+id/opponentSpellTrapZone1"
    android:layout_toRightOf="@+id/opponentSpellTrapZone1" />

<ImageView
    android:id="@+id/opponentSpellTrapZone3"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentSpellTrapZone2"
    android:layout_toRightOf="@+id/opponentSpellTrapZone2" />

<ImageView
    android:id="@+id/opponentSpellTrapZone4"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentSpellTrapZone3"
    android:layout_toRightOf="@+id/opponentSpellTrapZone3" />

<ImageView
    android:id="@+id/opponentSpellTrapZone5"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignTop="@+id/opponentSpellTrapZone4"
    android:layout_toRightOf="@+id/opponentSpellTrapZone4" />

<ImageView
    android:id="@+id/opponentMonsterZone1"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignLeft="@+id/opponentSpellTrapZone1"
    android:layout_below="@+id/opponentSpellTrapZone1" />

<ImageView
    android:id="@+id/opponentMonsterZone3"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/opponentSpellTrapZone2"
    android:layout_toRightOf="@+id/opponentSpellTrapZone2" />

<ImageView
    android:id="@+id/opponentMonsterZone4"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/opponentSpellTrapZone3"
    android:layout_toRightOf="@+id/opponentSpellTrapZone3" />

<ImageView
    android:id="@+id/opponentMonsterZone5"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/opponentSpellTrapZone4"
    android:layout_toRightOf="@+id/opponentSpellTrapZone4" />

<ImageView
    android:id="@+id/opponentMonsterZone2"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/opponentSpellTrapZone2"
    android:layout_toRightOf="@+id/opponentSpellTrapZone1" />

<ImageView
    android:id="@+id/monsterZone1"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignLeft="@+id/opponentMonsterZone1"
    android:layout_below="@+id/opponentMonsterZone1" />

<ImageView
    android:id="@+id/spellTrapZone1"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignLeft="@+id/monsterZone1"
    android:layout_below="@+id/monsterZone1" />

<ImageView
    android:id="@+id/monsterZone2"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_above="@+id/spellTrapZone1"
    android:layout_toRightOf="@+id/monsterZone1" />

<ImageView
    android:id="@+id/monsterZone3"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/opponentMonsterZone3"
    android:layout_toRightOf="@+id/opponentMonsterZone2" />

<ImageView
    android:id="@+id/monsterZone4"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/opponentMonsterZone3"
    android:layout_toRightOf="@+id/opponentMonsterZone3" />

<ImageView
    android:id="@+id/monsterZone5"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/opponentMonsterZone5"
    android:layout_toRightOf="@+id/opponentMonsterZone4" />

<ImageView
    android:id="@+id/spellTrapZone2"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/monsterZone1"
    android:layout_toRightOf="@+id/monsterZone1" />

<ImageView
    android:id="@+id/spellTrapZone3"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/monsterZone2"
    android:layout_toRightOf="@+id/monsterZone2" />

<ImageView
    android:id="@+id/spellTrapZone4"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/monsterZone3"
    android:layout_toRightOf="@+id/monsterZone3" />

<ImageView
    android:id="@+id/spellTrapZone5"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_below="@+id/monsterZone4"
    android:layout_toRightOf="@+id/monsterZone4" />

<ImageButton
    android:id="@+id/deckZone"
    android:layout_width="45dp"
    android:layout_height="75dp"
    android:layout_alignBottom="@+id/monsterZone5"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="14dp"
    android:onClick="onClickDeckZone" />
    --android:src="@drawable/deck" />

<TextView
    android:id="@+id/txtDeckNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="44"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_below="@+id/deckZone"
    android:layout_alignLeft="@+id/deckZone"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/opponentSpellTrapZone2"
    android:layout_marginTop="20dp"
    android:layout_toLeftOf="@+id/opponentSpellTrapZone4"
    android:text="TextView" />

</RelativeLayout>
4

1 回答 1

0

您需要膨胀布局,然后检索您的组件,否则findViewById将返回null.

ImageView handCard1;
ImageView handCard2;
ImageView handCard3;
ImageView handCard4;
ImageView handCard5;
ImageView handCard6;
Deck synchronWarriorsDeck = new Deck("Synchron Warriors");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this line inflates the layout
    setContentView(R.layout.activity_main);
    //now you can retrieve your components
    handCard1 = (ImageView) findViewById(R.id.handCard1);
    handCard2 = (ImageView) findViewById(R.id.handCard2);
    handCard3 = (ImageView) findViewById(R.id.handCard3);
    handCard4 = (ImageView) findViewById(R.id.handCard4);
    handCard5 = (ImageView) findViewById(R.id.handCard5);
    handCard6 = (ImageView) findViewById(R.id.handCard6);
    start();
}
于 2013-10-19T20:36:24.243 回答