2013-05-21 78 views
0

嗨我是新来的android和我一直在尝试做一个简单的数独应用程序。 我现在面临一个问题,每当我点击“关于”按钮,应用程序往往会停下来,并将我带回我的菜单屏幕。 任何人都可以提出什么是崩溃的问题?单击“约”按钮时Sudoku崩溃

下面这里是我的代码:

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的栅格起, 目的是填充网格,以便每个 行,每列,并且每个的3×3盒 (也称为)包含数字 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> 

数独的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> 
+1

安置自己的崩溃 –

回答

0

你应该尝试加入意向过滤到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: 
     startActivity(new Intent("org.example.sudoku.About")); 
     break; 

这应该解决您的问题:

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

更新前。

+0

对不起man..this的全堆栈跟踪didnt解决我的问题...还是一样... –

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

}