Java Android应用程序在主活动启动时强制崩溃
我刚开始使用这个应用程序,我觉得我做的一切都很好(这不是我第一次使用这个应用程序),但当我尝试运行它时,它崩溃了??它没有给我错误 下面是来自主要活动、manufest和xml布局的代码。 ` Manufest:Java Android应用程序在主活动启动时强制崩溃,java,android,eclipse,Java,Android,Eclipse,我刚开始使用这个应用程序,我觉得我做的一切都很好(这不是我第一次使用这个应用程序),但当我尝试运行它时,它崩溃了??它没有给我错误 下面是来自主要活动、manufest和xml布局的代码。 ` Manufest: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.theory.ga
<?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>
`
我还有一个叫做百万航空的班,这只是一个普通班,我想没什么错。请检查这是怎么回事,并让我知道为什么我的应用程序不会开始说它“已停止工作”。。谢谢我猜你得到了一个空点异常,因为
播放
和设置
对象都是空的
将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);
}
在onCreate()方法中尝试使用then时,播放和设置都将为空: 请确保像这样启动它们:
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings)
play.setOnClickListener(this);
settings.setOnClickListener(this);
您可能会得到一个
NullPointerException
,因为您忘记将按钮分配给字段(按钮播放,设置;
)
下面是一个关于如何分配它们的示例:
/** 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);
}
它必须给你一个错误。检查日志并在这里张贴stacktrace。啊,是的,这就是原因!我是怎么忘记这一步的:D谢谢男人:)
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings)
play.setOnClickListener(this);
settings.setOnClickListener(this);
/** 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);
}