java - 如何使用 Android 导航微调器

标签 java android android-activity navigation spinner

我是 Android 新手,而且不太擅长。所以请耐心听我说,请非常清楚和基本地回答您的问题。

我正在尝试在 android 中制作一个导航微调器。我使用教程制作了它,但我不知道如何修改他们引导用户访问的页面。现在,我试图让它们全部进入同一个页面,其中只有一个基本的“Hello”。但该应用程序不断崩溃。

public class MainActivity extends ActionBarActivity implements
        ActionBar.OnNavigationListener {

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current dropdown position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar to show a dropdown list.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
        // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter<String>(actionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1, new String[] {
                                getString(R.string.title_section1),
                                getString(R.string.title_section2),
                                getString(R.string.title_section3), }), this);
    }


    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getSupportActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }


    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getSupportActionBar()
                .getSelectedNavigationIndex());
    }


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


    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public boolean onNavigationItemSelected(int position, long id) {
        // When the given dropdown item is selected, show its contents in the
        // container view.
        Intent intent = new Intent (this, Fibonacci.class);
        startActivity(intent);
        return true;
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            TextView textView = (TextView) rootView
                    .findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

}

还有:

public class Fibonacci extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         // Get the message from the intent
        Intent intent = getIntent();
        String message = "Hello";

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }


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

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            TextView textView = (TextView) rootView
                    .findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static int fibonacciNum (int position) {
        int num = 1;

        if (position <= 2) {
            return 1;
        }
        else {
            num = fibonacciNum(position-1) + fibonacciNum(position-2);
            return num;
        }
    }
}

日志猫:

07-15 10:08:13.850: E/AndroidRuntime(921): FATAL EXCEPTION: main
07-15 10:08:13.850: E/AndroidRuntime(921): Process: com.zarwanhashem.sequences, PID: 921
07-15 10:08:13.850: E/AndroidRuntime(921): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.zarwanhashem.sequences/com.zarwanhashem.sequences.Fibonacci}; have you declared this activity in your AndroidManifest.xml?
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.app.Activity.startActivityForResult(Activity.java:3424)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.app.Activity.startActivityForResult(Activity.java:3385)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.app.Activity.startActivity(Activity.java:3627)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.app.Activity.startActivity(Activity.java:3595)
07-15 10:08:13.850: E/AndroidRuntime(921):  at com.zarwanhashem.sequences.MainActivity.onNavigationItemSelected(MainActivity.java:91)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.support.v7.app.ActionBarImplICS$OnNavigationListenerWrapper.onNavigationItemSelected(ActionBarImplICS.java:355)
07-15 10:08:13.850: E/AndroidRuntime(921):  at com.android.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:145)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.widget.AdapterView.access$200(AdapterView.java:48)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.os.Handler.handleCallback(Handler.java:733)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.os.Handler.dispatchMessage(Handler.java:95)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.os.Looper.loop(Looper.java:136)
07-15 10:08:13.850: E/AndroidRuntime(921):  at android.app.ActivityThread.main(ActivityThread.java:5017)
07-15 10:08:13.850: E/AndroidRuntime(921):  at java.lang.reflect.Method.invokeNative(Native Method)
07-15 10:08:13.850: E/AndroidRuntime(921):  at java.lang.reflect.Method.invoke(Method.java:515)
07-15 10:08:13.850: E/AndroidRuntime(921):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-15 10:08:13.850: E/AndroidRuntime(921):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-15 10:08:13.850: E/AndroidRuntime(921):  at dalvik.system.NativeStart.main(Native Method)

list :

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zarwanhashem.sequences.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最佳答案

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.zarwanhashem.sequences/com.zarwanhashem.sequences.Fibonacci}; have you declared this activity in your AndroidManifest.xml?

您打算在应用程序中调用的每个 Activity 都必须在 AndroidManifest 文件中声明。最少需要的是:

<activity android:name=".Fibonacci" />

关于java - 如何使用 Android 导航微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24728541/

相关文章:

java - MyBatis 中的一对多关系

java - 构建时 Gradle 7 StackOverflowError

android - Android 应用签名是什么意思?

android - 将自定义对象数组保存到共享首选项

android - 使用 "@style/Theme"后我的主题发生了变化

java - 谷歌地方 API : How to get photos and place id from latitude and longitude?

Java/JSP - 永久计数器

Android:在按钮上使用图像而不是文本

Android 完全强制退出应用程序

android - 在 Android 中传递 ArrayLists