org.example.sudoku.Prefs cannot be cast to android.app.Activity
このエラーが発生する理由がわかりません。Ed Burnette のこの本Hello, Androidを使用して、簡単な設定メニューを作成しようとしています。私はどこでも答えを探しましたが、それを理解するのに十分な理解が得られていないか、私はばかです。XML 設定とマニフェストにすべてを入れていますが、オプション メニューから設定をクリックするたびにクラッシュします。ここに私のコードとエラーメッセージがあります:
Prefs.java:
package org.example.sudoku;
import android.os.Bundle;
import android.preference.PreferenceFragment;
//import android.preference.PreferenceActivity;
public class Prefs extends PreferenceFragment {
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
数独.java:
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class Sudoku extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.settings:
//startActivity(new Intent(this, Prefs.class));
Intent intent = new Intent(this, Prefs.class);
startActivity(intent);
return true;
}
return false;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings"
android:title="@string/settings_label"
android:alphabeticShortcut="@string/settings_shortcut" />
</menu>
<?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="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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=".About"
android:label="@string/about_title"
android:theme="@android:style/Theme.Dialog">
</activity>
<activity android:name=".Prefs"
android:label="@string/settings_title">
</activity>
</application>
</manifest>
文字列.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Sudoku!</string>
<string name="app_name">Sudoku Game</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_text">Sudoku is a logic-based number placement puzzle. Starting with a partially completed 9x9 grid so that each row, each column, and each of the 3x3 boxes(also called <i>block</i>) contains the digits 1 to 9 exactly once</string>
<string name="about_title">About Android Sudoku</string>
<string name="settings_label">Settings
</string>
<string name="settings_title">Sudoku Settings</string>
<string name="settings_shortcut">s</string>
<string name="music_title">Music</string>
<string name="music_summary">Play Background Music</string>
<string name="hints_title">Hints</string>
<string name="hints_summary">Show hints during play</string>
</resources>
settings.xml:
<code>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/settings_title">
<CheckboxPreference
android:key="music"
android:title="@string/music_title"
android:summary="@string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="hints"
android:title="@string/hints_title"
android:summary="@string/hints_summary"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
main.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:gravity="center"
android:layout_marginBottom="20dip"
android:textSize="24.5sp"
/>
<TableLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*">
<TableRow>
<Button
android:id="@+id/continue_button"
android:text="@string/continue_label"
/>
<Button
android:id="@+id/new_button"
android:text="@string/new_game_label"
/>
</TableRow>
<TableRow >
<Button
android:id="@+id/about_button"
android:text="@string/about_label"
/>
<Button
android:id="@+id/exit_button"
android:text="@string/exit_label"
/>
</TableRow>
</TableLayout>
</LinearLayout>
Here is all the error messages I am recieving:
05-18 12:37:16.781: E/AndroidRuntime(623): FATAL EXCEPTION: main
05-18 12:37:16.781: E/AndroidRuntime(623): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.sudoku/org.example.sudoku.Prefs}: java.lang.ClassCastException: org.example.sudoku.Prefs cannot be cast to android.app.Activity
05-18 12:37:16.781: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
05-18 12:37:16.781: E/AndroidRuntime(623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-18 12:37:16.781: E/AndroidRuntime(623): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-18 12:37:16.781: E/AndroidRuntime(623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-18 12:37:16.781: E/AndroidRuntime(623): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 12:37:16.781: E/AndroidRuntime(623): at android.os.Looper.loop(Looper.java:137)
05-18 12:37:16.781: E/AndroidRuntime(623): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-18 12:37:16.781: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 12:37:16.781: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 12:37:16.781: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-18 12:37:16.781: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-18 12:37:16.781: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
05-18 12:37:16.781: E/AndroidRuntime(623): Caused by: java.lang.ClassCastException: org.example.sudoku.Prefs cannot be cast to android.app.Activity
05-18 12:37:16.781: E/AndroidRuntime(623): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-18 12:37:16.781: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
05-18 12:37:16.781: E/AndroidRuntime(623): ... 11 more