无法实例化活动组件信息.....无法转换为android.app.Activity

时间:2012-05-18 13:04:01

标签: android

我无法弄清楚为什么我收到此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); 
        }

    }

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

的strings.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:

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

2 个答案:

答案 0 :(得分:0)

你不能把你的班级Prefs转换为活动

答案 1 :(得分:0)

我遇到了同样的问题,你从头开始的评论提示答案 - 你只需要做一个干净的构建。

Eclipse开发人员应该运行Project » Clean…,命令行开发人员需要运行ant clean