我刚刚开始研究这个应用程序我觉得我已经做好了一切(这不是我一直在做的第一个应用程序)但是当我尝试运行它崩溃了吗?它没有给我任何错误。
这是主要活动的代码,然后是manufest,然后是xml布局。 `
package com.theory.game;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GameTheoryActivity extends Activity implements View.OnClickListener{
Button play, settings;
int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play.setOnClickListener(this);
settings.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bPlay:
Random irand = new Random();
i = irand.nextInt(4);
switch(i){
case 0:
Intent GoMillionair = new Intent(GameTheoryActivity.this, millionair.class);
startActivity(GoMillionair);
return;
case 1:
return;
case 2:
return;
case 3:
return;
}
return;
case R.id.bSettings:
return;
}
}
}
Manufest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theory.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application >
<activity
android:name=".GameTheoryActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".millionair" >
<intent-filter>
<action android:name="com.theory.game.MILLIONAIR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundimage"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="390dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:background="@drawable/play" />
<Button
android:id="@+id/bSettings"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_weight="0.36"
android:background="@drawable/settings" />
</LinearLayout>
</LinearLayout>
`
我还有另一个叫做百万航空的班级,这个班级只是一个普通的班级,我猜也没错。请查看最新情况,让我知道为什么我的申请不会开始说它“已经停止工作”..谢谢
答案 0 :(得分:1)
我的猜测是你得到一个NullPointerException,因为play
和settings
个对象都是null。
将onCreate()更改为如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings);
play.setOnClickListener(this);
settings.setOnClickListener(this);
}
答案 1 :(得分:1)
您可能会收到NullPointerException
,因为您忘记将按钮分配到字段(Button play, settings;
)。
以下是如何分配它们的示例:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.bPlay);
settings = (Button) findViewById(R.id.bSettings);
}
答案 2 :(得分:0)
当您尝试在onCreate()方法中使用时,看起来播放和设置都将为null:
play.setOnClickListener(this);
settings.setOnClickListener(this);
确保像这样启动它们:
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings)
play.setOnClickListener(this);
settings.setOnClickListener(this);