如何使用android导航微调器

时间:2014-07-14 01:43:27

标签: 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;
        }
    }
}

logcat的:

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)

清单:          

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

1 个答案:

答案 0 :(得分:1)

  

android.content.ActivityNotFoundException:无法找到显式   活动课   {com.zarwanhashem.sequences / com.zarwanhashem.sequences.Fibonacci};   您是否在AndroidManifest.xml中声明了此活动

您打算在应用程序中调用的每个活动都必须在AndroidManifest文件中声明。必要的最低要求是:

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