-1

我曾经看到消息说“不幸的是 appname 已停止”,即使我没有收到任何错误。我可以向您展示代码,以便您完全理解。

这是 ColorMixings.java

package com.example.colormixings;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;

public class ColorMixings extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_mixings);
final CheckBox chkRed = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox chkBlue = (CheckBox) findViewById(R.id.checkBox2);
final CheckBox chkYellow = (CheckBox) findViewById(R.id.checkBox3);
ImageButton imgMove = (ImageButton) findViewById(R.id.imageButton1);

imgMove.setOnClickListener(new onClickListener() {

@Override
public void onClick(View arg0) {
if(chkRed.isChecked())
{
startActivity(new Intent(ColorMixings.this,Red.class));
}
else if(chkBlue.isChecked())
{
startActivity(new Intent(ColorMixings.this, Blue.class));
}
else if(chkYellow.isChecked())
{
startActivity(new Intent(ColorMixings.this, Yellow.class));
}
else if(chkRed.isChecked() && chkYellow.isChecked())
{
startActivity(new Intent(ColorMixings.this, Orange.class));
}
else if(chkRed.isChecked() && chkBlue.isChecked())
{
startActivity(new Intent(ColorMixings.this, Violet.class));
}
else if(chkBlue.isChecked() && chkYellow.isChecked())
{
startActivity(new Intent(ColorMixings.this, Green.class));
}
else
{
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.color_mixings, menu);
return true;
}
}

这是 Red.java 的代码。所有其他类都与 Red.java 相同,所以我没有包含它

package com.example.colormixings;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class Red extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageButton imgBack = (ImageButton) findviewById(R.id.imageButton1);
imgBack.setOnClickListener(new onClickListener() {

@Override
public void onClick(View arg0) {
startActivity(new Intent(Red.this, ColorMixings.class)
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.red, menu);
return true;
}
}

这是 AndroidManifest.xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xlmns:android="http://schemas.android.com/apk/res/android"
package="com.example.colormixings"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersin="8"
android:targetSdkVersion="17"/>

<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@string/AppTheme"
android:icon="@drawable/painticon">
<activity
android:name="com.example.colormixings.ColorMixings"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity>
android:name="com.example.colormixings.Red"
android:label="@string/title_activity_red">
</activity>
<activity>
android:name="com.example.colormixings.Blue"
android:label="@string/title_activity_blue">
</activity>
<activity>
android:name="com.example.colormixings.Yellow"
android:label="@string/title_activity_yellow">
</activity>
<activity>
android:name="com.example.colormixings.Green"
android:label="@string/title_activity_green">
</activity>
<activity>
android:name="com.example.colormixings.Orange"
android:label="@string/title_activity_orange">
</activity>
<activity>
android:name="com.example.colormixings.Violet"
android:label="@string/title_activity_violet">
</activity>
</application>

</manifest>
4

1 回答 1

2

如果没有 logcat,我不知道这是否是您当前的问题,但如果不是,很快就会出现。在Red你有

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageButton imgBack = (ImageButton) findviewById(R.id.imageButton1);  // problem line
        imgBack.setOnClickListener(new onClickListener() {

您正在尝试初始化您的ImageButton而不膨胀layout包含它的内容。因此NPE,当您尝试设置您的onClickListener. 在初始化之前,ImageButton您应该调用setContentView(). 就像是

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layoutWithTheButton);   // you are forgetting this line
      ImageButton imgBack = (ImageButton) findviewById(R.id.imageButton1);
      imgBack.setOnClickListener(new onClickListener() {
于 2013-09-06T16:05:37.480 回答