2014-07-14 21 views
0

我是新來的android,我很糟糕。所以請耐心等待我的回答,並且要非常清楚和基本。如何使用android導航微調器

我想在android中進行導航微調。我使用教程做了它,但我無法弄清楚如何修改它們引導用戶訪問的頁面。現在我試圖讓他們都導致只有一個基本的「你好」的相同頁面。該應用程序不斷崩潰。

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

請從logcat發佈崩潰堆棧跟蹤。 – matiash

+0

什麼是崩潰堆棧跟蹤?我如何得到它? – Zarwan

+0

檢查http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this – matiash

回答

1

android.content.ActivityNotFoundException:無法找到顯式 活動類 {com.zarwanhashem.sequences/com.zarwanhashem.sequences.Fibonacci}; 你是否在你的AndroidManifest.xml中聲明瞭這個活動

您打算在應用程序中調用的每個活動都必須在AndroidManifest文件中聲明。最低必要的將是:

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

是的,我在閱讀哈哈後發現它。謝謝。我應該先自己檢查一下。 – Zarwan