0

嗨,我是 android 新手,我一直在尝试做一个简单的数独应用程序。我现在面临一个问题,每当我单击“关于”按钮时,应用程序往往会停止并带我回到我的菜单屏幕。谁能建议崩溃的问题是什么?

下面是我的代码:

数独.java

public class Sudoku extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sudoku);
    View continueButton=findViewById(R.id.button1);
    continueButton.setOnClickListener(this);
    View newButton=findViewById(R.id.button2);
    newButton.setOnClickListener(this);
    View aboutButton=findViewById(R.id.button3);
    aboutButton.setOnClickListener(this);
    View exitButton=findViewById(R.id.button4);
    exitButton.setOnClickListener(this);
}

        public void onClick(View v){
        switch(v.getId()){
        case R.id.button3:
            Intent i = new Intent(this, About.class);
            startActivity(i);
            break;
        case R.id.button4:
            finish();
            break;

            }
          }
        @Override
           public boolean onCreateOptionsMenu(Menu menu) {
              super.onCreateOptionsMenu(menu);
              MenuInflater inflater = getMenuInflater();
              inflater.inflate(R.menu.menu, menu);
              return true;
           }

 }

关于.java

public class About extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
}
}

关于.xml:

public class About extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
}
}

字符串.xml:

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Sudoku</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Application Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\Sudoku is a logic-based number placement puzzle.

从部分完成的 9x9 网格开始,目标是填充网格,以便每一行、每一列和每个 3x3 框(也称为)中的数字 1 到 9 恰好包含一次。

activity_sudoku.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/background"
tools:context=".Sudoku" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textStyle="italic" />

<TableLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:stretchColumns="*">
    <TableRow >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="22dp"
    android:text="@string/continue_label"
    android:textStyle="italic"
     />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="21dp"
    android:text="@string/new_game_label" 
    android:textStyle="italic"
    />


</TableRow>
<TableRow >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/about_label"
         />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/exit_label"
         />

</TableRow>
</TableLayout>

</RelativeLayout>

数独清单.xml:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="org.example.sudoku.Sudoku"
        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="org.example.sudoku.About"
        android:label="@string/about_title"
        android:theme="@android:style/Theme.Dialog">
    </activity>
</application>

</manifest>
4

2 回答 2

0

您应该尝试将意图过滤器添加到 manifest.xml。在启动 Android prog 时,我经常遇到这个问题。添加它,例如:

<activity
    android:name="org.example.sudoku.Sudoku"
    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="org.example.sudoku.About"
    android:label="@string/about_title"
    android:theme="@android:style/Theme.Dialog">
    <intent-filter>
        <action android:name="org.example.sudoku.About" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

稍后在 Sudoku.java 更新这部分:

case R.id.button3:
        Intent i = new Intent(this, About.class);
        startActivity(i);
        break;

将以上内容更新为:

case R.id.button3:
        startActivity(new Intent("org.example.sudoku.About"));
        break;

这应该可以解决您的问题。

于 2013-05-21T06:20:24.030 回答
0

试试这个代码...

case R.id.button3:
        Intent i = new Intent(getApplicationContext(), About.class);
        startActivity(i);
        break;

你放 getApplicationContext() 或 Sudoku.this 而不是只有这个。因为有时这不是上下文。

您还可以针对 switch()...case 语句使用以下代码。

public void onClick(View v){

    if(v == aboutButton)
    {
        Intent i = new Intent(this, About.class);
        startActivity(i);
    }
    else
        finish();

}

于 2013-05-21T06:35:48.243 回答