2013-05-21 2 views
0

Привет, я новичок в андроиде, и я пытался сделать простое приложение sudoku. Теперь перед лицом проблемы, когда я нажимаю кнопку «about», аппликация имеет тенденцию останавливаться и возвращает меня на экран моего меню. Может ли кто-нибудь предложить, в чем проблема при аварии?Судоку падает, когда нажата кнопка «около»

Ниже вот мой код:

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> 

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> 
+1

Сообщение полной трассировки стека ваши аварии –

ответ

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; 

Update выше, как:

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

Это должно решить вашу проблему.

+0

жаль man..this вратаря решить мою проблему ... все тот же ... –

0

Попробуйте этот код ...

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

вы положили getApplicationContext() или Sudoku.this вместо только. , потому что иногда это не относится к контексту.

Вы также используете нижеследующий код против переключатель() ... случай заявление.

public void onClick(View v){ 

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

}

Смежные вопросы