单击“关于”按钮时,Sudoku崩溃

时间:2013-05-21 05:49:37

标签: android

您好我是Android的新手,我一直在尝试做一个简单的数独应用程序。 我现在面临一个问题,每当我点击“关于”按钮时,applocation会停止并将我带回菜单屏幕。 任何人都可以建议崩溃的问题是什么?

下面是我的代码:

Sudoku.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;
           }

 }

About.java

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

about.xml:

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

string.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>

Sudoku Manifest.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>

2 个答案:

答案 0 :(得分:0)

您应该尝试将intent-filter添加到manifest.xml。在启动Android编程时我经常遇到这个问题。添加如下:

<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;

这可以解决您的问题。

答案 1 :(得分: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();

}