1

在我的項目中,我有一個位於MainActivity.java的導航抽屜。該抽屜中的每個項目都會啓動佔用整個屏幕空間的不同片段。其中一個片段具有需要啓動一個Activity的按鈕(此活動擴展了MainActivity.java--其原理背後是保持抽屜式導航器可用)。當我按下任何按鈕,在一個片段啓動另一個活動,我得到這個錯誤java.lang.IllegalArgumentException:沒有找到ID爲fragment的視圖NewLogFragment

java.lang.RuntimeException: Unable to start activity ComponentInfo{.AddWorkoutActivity}: java.lang.IllegalArgumentException: 
No view found for id 0x7f0a0041 (id/contentFrame) for fragment NewLogFragment{529f3328 #0 id=0x7f0a0041} 

這裏,NewLogFragment是一種嘗試推出新的活動片段。

基本上,我的項目的結構是這樣的: MainActivity:java->包含NavDrawer並啓動一個片段 - >然後這個片段啓動另一個活動,這會導致錯誤。

下面是我的一些我的代碼:

public class MainActivity extends ActionBarActivity { 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // rest of the code. Note, DrawerList is the list of items in the drawer. 

     DrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       int editedPosition = position + 1; 
       Toast.makeText(MainActivity.this, "You selected item " + editedPosition, Toast.LENGTH_SHORT).show(); 

       FragmentManager fragmentManager = getSupportFragmentManager(); 
       FragmentTransaction ft = fragmentManager.beginTransaction(); 

       switch(position){ 
        case 0: 

         ft.replace(R.id.contentFrame, new NewLogFragment()).commit(); 

         break; 

} 
       mDrawerLayout.closeDrawer(mDrawerList); 
      } 
     }); // ... rest of the code 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/primary" 
     app:popupTheme="@style/Theme.AppCompat" 
     app:theme="@style/ToolbarTheme" /> 



    <!-- Main layout --> 
    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <FrameLayout 
      android:id="@+id/contentFrame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"></FrameLayout> 


     <!-- Nav drawer --> 
     <ListView 
      android:id="@android:id/list" 
      android:layout_width="305dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:background="@android:color/white" /> 
    </android.support.v4.widget.DrawerLayout>  
</LinearLayout> 

NewLogFragment.java

public class NewLogFragment extends Fragment { 


    private Button bt_button; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.newlog_layout, container, false); 
     bt_button = (Button) rootView.findViewById(R.id.bt_button); 

     bt_button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getActivity(), AddWorkoutActivity.class); 
       intent.putExtra("type", 1); 
       startActivity(intent); 
      } 
     }); 

    return rootView; 
} 

@Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
    } 

newlog_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/fragmentOne"> 

    <LinearLayout 
     android:id="@+id/ll_buttons" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/bt_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/bt_button" /> 


    </LinearLayout> 

</RelativeLayout> 

也許我沒有正確地構建我的項目......但是我已經在網上查找,這應該仍然有效。不知道什麼是錯的。任何關於構建我的項目的最佳方式的建議都會受到讚賞(關於是以這種方式保留它還是將所有內容都作爲片段或活動)。但我想先解決這個問題。

編輯加入AddWorkoutActivity.java

public class AddWorkoutActivity extends MainActivity implements View.OnClickListener { 

    TextView tv_warmupex, tv_mainex, tv_workout_type, tv_cycle, tv_week, tv_one_rm; 
    EditText ev_datepicker; 
    // more layout stuff 

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

     dateFormatter = new SimpleDateFormat("MM-dd-yyyy", Locale.US); 

     Intent logFragmentIntent = getIntent(); 

     workout_type = logFragmentIntent.getStringExtra("workout_type"); 
     setUpLayout(workout_type); 
    } 

    // and a bunch of helper methods that do all the calculations 

    public void completeWorkout(MenuItem item) { 
    if (cb_wu_1.isChecked() && cb_wu_2.isChecked() && cb_wu_3.isChecked() && 
      cb_me_1.isChecked() && cb_me_2.isChecked() && cb_me_3.isChecked()) { 
     // add workout to database 
     HashMap<String, String> queryValuesMap = new HashMap<String, String>(); 

     queryValuesMap.put("table", MAIN_E); 
     queryValuesMap.put(WEEK, String.valueOf(e_info[1])); // get latest cycle/week for this exercise and +1 to add to the table 
     queryValuesMap.put(CYCLE, String.valueOf(e_info[0])); 
     queryValuesMap.put(WEIGHT, getWeightString()); 
     queryValuesMap.put(DATE_CREATED, changeDateFormat(ev_datepicker.getText().toString())); 
     queryValuesMap.put(NOTES, "notes"); 
     queryValuesMap.put(MAIN_EXERCISE, workout_type); 

     dbTools.insertExercise(queryValuesMap); 

     // Update 1RM table if a new rep max is achieved during current workout 
     if (Double.parseDouble(getWeightString())>oneRepMax){ 

      HashMap<String, String> queryValuesMapOneRM = new HashMap<String, String>(); 

      queryValuesMapOneRM.put(MAIN_EXERCISE, workout_type); // get latest cycle/week for this exercise and +1 to add to the table 
      queryValuesMapOneRM.put(WEIGHT, getWeightString()); 
      queryValuesMapOneRM.put(DATE_CREATED, changeDateFormat(ev_datepicker.getText().toString())); 
      dbTools.updateExerciseRepMax(queryValuesMapOneRM); 

     } 

     Intent intent = new Intent(this, MainActivity.class); 
     startActivity(intent); 
    } else { 
     /*if not all buttons are clicked, show a toast 
      telling user to click all buttons to complete workout 
     */ 
    } 
} 

} 
+0

檢查您在NewLogFragment類中的導入片段應從spport.v4導入 – Shiva

+0

它確實...我已經檢查過所有java類 – naja

+0

您是否在manifest中註冊了AddWorkoutActivity?你可以發佈AddWorkoutActivity類的相關代碼嗎? – Shiva

回答

2

的,因爲你試圖使用不當前活動包含ID替換片段的崩潰發生的歷史。我的建議是改變項目結構,例如爲NavDrawer使用一個活動,併爲每個菜單使用片段。否則,它會更復雜。請參考以下鏈接:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

+0

好的,但如果我的菜單片段需要打開另一個屏幕(這不是菜單片段)會怎麼樣。你的建議是讓該屏幕成爲一個片段?所以在我的應用程序中,我只會有一個MainActivity和一堆碎片? – naja

+0

Menu代碼是什麼意思? – Krish

+0

如果我需要發送消息到非菜單片段,我該怎麼辦?活動很容易。在我的非菜單AddWorkoutActivity中,我只是在意圖上調用getStringExtra()。 – naja

相關問題