我正在使用eclipse为Android开发游戏。游戏已经完成,但它无法在我选择的模拟器(BlueStacks)上正确启动。 在撞击之前它只显示白色屏幕和信息栏约0.5秒。在崩溃后,应用程序仍然在后台打开,但无法执行或显示任何内容。
我可以编译和安装应用程序,语法是正确的...我认为我的错误在清单中的某处。
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.ralfkraemer.starsandstrikes"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="de.ralfkraemer.starsandstrikes.MainMenu"
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>
以下是我的主要活动类
package de.ralfkraemer.starsandstrikes;
import java.io.IOException;
import java.util.Random;
import de.ralfkraemer.starsandstrikes.R;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymainmenu);
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
randomizeFact();
}
// RANDOM FACT
String[] factresource = getResources().getStringArray(R.array.factres);
protected void randomizeFact(){
TextView randomFact = (TextView)findViewById(R.id.text1);
Random random = new Random();
int maxIndex = factresource.length;
int generatedIndex = random.nextInt(maxIndex);
randomFact.setText(factresource[generatedIndex]);
}
// START GAME
public void startGame(){
Game currentGame = new Game();
currentGame.setScore(0);
setContentView(R.layout.activitygame);
currentGame.running = true;
}
// PLAY STORE (RATE)
public void openPlayStore(){
Uri uri = Uri.parse("url");
Intent intent = new Intent (Intent.ACTION_VIEW, uri);
startActivity(intent);
}
public void goToAbout(){
setContentView(R.layout.activityabout);
}
public void goToHowToPlay(){
setContentView(R.layout.activityhowtoplay);
}
public void goToHighscores() throws IOException{
Highscores highscores = new Highscores();
setContentView(R.layout.activityhighscores);
}
}
这是主要活动xml
<LinearLayout 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:orientation="vertical"
android:background="@drawable/background"
tools:context="${linearPackage}.${activityClass}" >
<ImageView
android:id="@+id/header"
android:contentDescription="@string/header_content"
android:src="@drawable/logo_mainmenu"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button_play"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="65dp"
android:text="@string/mm_button_play"
android:textSize="22sp"
android:onClick="startGame"
android:background="@drawable/button_default" />
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="@string/mm_randomfact_placeholder"
android:textSize="16sp"
android:gravity="center"/>
<Button
android:id="@+id/button_randomfact"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="65dp"
android:text="@string/mm_button_randomfact"
android:textSize="18sp"
android:onClick="randomizeFact"
android:background="@drawable/button_default" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/button_highscores"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="@string/mm_button_highscores"
style="android:attr/buttonBarButtonStyle"
android:background="@drawable/button_default"
android:onClick="goToHighscores" />
<Button
android:id="@+id/button_howtoplay"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="@string/mm_button_howtoplay"
style="android:attr/buttonBarButtonStyle"
android:background="@drawable/button_default"
android:onClick="goToHowToPlay" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/button_about"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="@string/mm_button_about"
style="android:attr/buttonBarButtonStyle"
android:background="@drawable/button_default"
android:onClick="goToAbout" />
<Button
android:id="@+id/button_rate"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="@string/mm_button_rate"
style="android:attr/buttonBarButtonStyle"
android:background="@drawable/button_default"
android:onClick="openPlayStore" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
当活动在此行实例化时,您正在调用getResources
:
// RANDOM FACT
String[] factresource = getResources().getStringArray(R.array.factres);
并且隐含的Activity.this
对象没有有效的上下文,因此崩溃了。您需要在onCreate
或在对象实例化后调用的另一个方法中调用它。