2015-08-14 70 views
0

这是代码:程序崩溃的切换从项目菜单活动时

SudokuMain.java

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int itemId = item.getItemId(); 
    switch (itemId) { 
     case R.id.action_newGame: 
      ((SudokuBoardView) findViewById(R.id.vsudoku_board)).newGame(); 
      return true; 
     case R.id.action_test: 
      Intent intent = new Intent(SudokuMain.this, PrimoRisultato.class); 
      startActivityForResult(intent,0); 
      finish(); 
     /* if (LinkMatrix.Test()) { 
       Toast.makeText(this, "Test Successfully", Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(this, "Test Failed", Toast.LENGTH_LONG).show(); 
      }*/ 
      return true; 
    } 
    return true; 
} 

AndroidManifest.xml中

<activity 
     android:name=".SudokuMain" 
     android:configChanges="orientation|keyboardHidden" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape" 
     android:theme="@style/AppThemeActionBar" > 
    </activity> 
    <activity 
     android:name=".PrimoRisultato" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape" 
     android:configChanges="orientation|keyboardHidden" > 
    </activity> 

当我点击按钮来切换活动的应用程式崩溃。 我看到了几个指导/代码,但它不起作用。

好吧,所以我认为问题是在SudokiMain我使用主题与操作栏,但在PrimoRisultato我使用主题没有操作栏。 这是堆栈跟踪(红色文本):

08-18 14:41:27.602 7471-7471/com.example.face_offbrains E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.face_offbrains, PID: 7471 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.face_offbrains/com.example.face_offbrains.PrimoRisultato}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

有没有解决“您需要使用Theme.AppCompat主题(或后代)与本次活动”没有动作条的方法吗?

即使把Theme.AppCompact这样的应用程序崩溃。

<activity 
     android:name=".PrimoRisultato" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape" 
     android:configChanges="orientation|keyboardHidden" 
     android:theme="@style/AppThemeActionBar"> 
    </activity> 

和menu.xlm

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

<item 
    android:id="@+id/action_newGame" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:title="@string/action_newGame"/> 
<item 
    android:id="@+id/action_test" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:title="@string/action_test"/> 

+3

您可以发布您的堆栈跟踪? – Prudhvi

+0

如果没有你发布你的堆栈跟踪,我们无法知道,但我的猜测是你没有声明'PrimoRisultato.class'是你的清单中的一个活动。 –

+0

我没有在manifest中发布PrimoRisultato的声明 – shawk

回答

0

如果你不需要通过活动结果使用此代码:

Intent intent = new Intent(SudokuMain.this, PrimoRisultato.class); 
      startActivity(intent); 
      finish(); 
+0

再次崩溃.... – shawk

+0

PrimoRisultato.class不是和清单中的活动。 – Santiago

+0

甚至将PrimoRisultato.class放入应用程序崩溃中 – shawk