我正在创建一个安卓纸牌游戏。我创建了一个扑克牌类和一个纸牌类。当我运行示例代码时,它无法构建甲板。这是我的代码和 logcat
扑克牌.java
package com.michaelpeerman.hi_low;
public class PlayingCard {
public String[] CardSuit = {"SPADE","HEART","CLUB","DIAMOND"};
public String[] CardRank= {"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE","JACK","QUEEN","KING"};
public String theSuit;
public int theRank;
public boolean theState;
public PlayingCard(){
theSuit = "SPADE";
theRank = 1;
theState = false;
}
public PlayingCard(String mySuit, int myRank){
theSuit = mySuit;
theRank = myRank;
}
public int getRank(){
return theRank;
}
public String getSuit(){
return theSuit;
}
public boolean getState(){
return theState;
}
public void setState(boolean myState){
theState = myState;
}
public void setRank(int myRank){
theRank = myRank;
}
public void setSuit(String mySuit){
theSuit = mySuit;
}
public String getCardString(){
String cardString = null;
String theCardRanks[] = {"A","2","3","4","5","6","7","8","9","T","J","Q","K"};
if (theSuit == "CLUB")
cardString = "C"+theCardRanks[theRank-1];
else if (theSuit == "SPADE")
cardString = "S"+theCardRanks[theRank-1];
else if (theSuit == "HEART")
cardString = "H"+theCardRanks[theRank-1];
else if (theSuit == "DIAMOND")
cardString = "D"+theCardRanks[theRank-1];
return cardString;
}
}
CardDeck.java
package com.michaelpeerman.hi_low;
import java.util.Random;
import com.michaelpeerman.hi_low.PlayingCard;
public class CardDeck {
public PlayingCard[] cardArray;
public int num_decks;
public int cards_left;
public CardDeck(int my_decks)
{
num_decks = my_decks;
cardArray = new PlayingCard[52*num_decks];
cards_left = 52*num_decks;
for (int i=0; i< num_decks; i++){
for (int j = 0; j < 13; j++)
{
cardArray[(j+(52*i))].setSuit("SPADE");
cardArray[j+13+(52*i)].setSuit("HEART");
cardArray[j+26+(52*i)].setSuit("CLUB");
cardArray[j+39+(52*i)].setSuit("DIAMOND");
if (j==0){
cardArray[j+(52*i)].setRank(1);
cardArray[j+13+(52*i)].setRank(1);
cardArray[j+26+(52*i)].setRank(1);
cardArray[j+39+(52*i)].setRank(1);
}
else if (j==1){
cardArray[j+(52*i)].setRank(2);
cardArray[j+13+(52*i)].setRank(2);
cardArray[j+26+(52*i)].setRank(2);
cardArray[j+39+(52*i)].setRank(2);
}
else if (j==2){
cardArray[j+(52*i)].setRank(3);
cardArray[j+13+(52*i)].setRank(3);
cardArray[j+26+(52*i)].setRank(3);
cardArray[j+39+(52*i)].setRank(3);
}
else if (j==3){
cardArray[j+(52*i)].setRank(4);
cardArray[j+13+(52*i)].setRank(4);
cardArray[j+26+(52*i)].setRank(4);
cardArray[j+39+(52*i)].setRank(4);
}
else if (j==4){
cardArray[j+(52*i)].setRank(5);
cardArray[j+13+(52*i)].setRank(5);
cardArray[j+26+(52*i)].setRank(5);
cardArray[j+39+(52*i)].setRank(5);
}
else if (j==5){
cardArray[j+(52*i)].setRank(6);
cardArray[j+13+(52*i)].setRank(6);
cardArray[j+26+(52*i)].setRank(6);
cardArray[j+39+(52*i)].setRank(6);
}
else if (j==6){
cardArray[j+(52*i)].setRank(7);
cardArray[j+13+(52*i)].setRank(7);
cardArray[j+26+(52*i)].setRank(7);
cardArray[j+39+(52*i)].setRank(7);
}
else if (j==7){
cardArray[j+(52*i)].setRank(8);
cardArray[j+13+(52*i)].setRank(8);
cardArray[j+26+(52*i)].setRank(8);
cardArray[j+39+(52*i)].setRank(8);
}
else if (j==8){
cardArray[j+(52*i)].setRank(9);
cardArray[j+13+(52*i)].setRank(9);
cardArray[j+26+(52*i)].setRank(9);
cardArray[j+39+(52*i)].setRank(9);
}
else if (j==9){
cardArray[j+(52*i)].setRank(10);
cardArray[j+13+(52*i)].setRank(10);
cardArray[j+26+(52*i)].setRank(10);
cardArray[j+39+(52*i)].setRank(10);
}
else if (j==10){
cardArray[j+(52*i)].setRank(11);
cardArray[j+13+(52*i)].setRank(11);
cardArray[j+26+(52*i)].setRank(11);
cardArray[j+39+(52*i)].setRank(11);
}
else if (j==11){
cardArray[j+(52*i)].setRank(12);
cardArray[j+13+(52*i)].setRank(12);
cardArray[j+26+(52*i)].setRank(12);
cardArray[j+39+(52*i)].setRank(12);
}
else if (j==12){
cardArray[j+(52*i)].setRank(13);
cardArray[j+13+(52*i)].setRank(13);
cardArray[j+26+(52*i)].setRank(13);
cardArray[j+39+(52*i)].setRank(13);
}
}
}
}
public void useCard(){
cards_left = cards_left - 1;
}
public int get_cards_left(){
return cards_left;
}
public void setCard(int card, PlayingCard myCard){
cardArray[card]=myCard;
}
public PlayingCard getCard(int card){
return cardArray[card];
}
public void printDeck(){
for (int j = 0; j < (52*num_decks); j++){
// Print out to textview
//String cardString = cardArray[j].getCardString();
}
}
public void suffleDeck(){
for (int i = 0; i < 100*num_decks; i++)
{
int rand1 = 1 + new Random().nextInt(52*num_decks);
int rand2 = 1 + new Random().nextInt(52*num_decks);
PlayingCard tmpCard = cardArray[rand1];
cardArray[rand1] = cardArray[rand2];
cardArray[rand2] = tmpCard;
}
}
public boolean isDeckFinished(){
boolean isFinished =true;
for (int i = 0; i<52*num_decks; i++){
PlayingCard myCard = cardArray[i];
if (myCard.getState() == false){
isFinished = false;
}
}
return isFinished;
}
}
启动屏幕.java
package com.michaelpeerman.hi_low;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.michaelpeerman.hi_low.R;
import com.michaelpeerman.hi_low.CardDeck;
import com.michaelpeerman.hi_low.PlayingCard;
public class StartScreen extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CardDeck myDeck = new CardDeck(1);
int cards = myDeck.cards_left;
String text = Integer.toString(cards);
TextView t=new TextView(this);
t=(TextView)findViewById(R.id.main_text);
t.append(text);
}
}
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/main_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.michaelpeerman.hi_low"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="13" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".StartScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LogCat 输出
05-03 18:03:55.067: E/AndroidRuntime(9805): FATAL EXCEPTION: main
05-03 18:03:55.067: E/AndroidRuntime(9805): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michaelpeerman.hi_low/com.michaelpeerman.hi_low.StartScreen}: java.lang.NullPointerException
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.os.Looper.loop(Looper.java:137)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-03 18:03:55.067: E/AndroidRuntime(9805): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 18:03:55.067: E/AndroidRuntime(9805): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 18:03:55.067: E/AndroidRuntime(9805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-03 18:03:55.067: E/AndroidRuntime(9805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-03 18:03:55.067: E/AndroidRuntime(9805): at dalvik.system.NativeStart.main(Native Method)
05-03 18:03:55.067: E/AndroidRuntime(9805): Caused by: java.lang.NullPointerException
05-03 18:03:55.067: E/AndroidRuntime(9805): at com.michaelpeerman.hi_low.CardDeck.<init>(CardDeck.java:18)
05-03 18:03:55.067: E/AndroidRuntime(9805): at com.michaelpeerman.hi_low.StartScreen.onCreate(StartScreen.java:16)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.Activity.performCreate(Activity.java:4465)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 18:03:55.067: E/AndroidRuntime(9805): ... 11 more