2017-10-10 35 views
0

我是新的Android開發,所以這可能是一個菜鳥的錯誤,但是,我卡住了。無法啓動活動,沒有發現id爲

我要開始菜單上點擊時的活動,但以下情況除外顯示出來:

了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.mygymroutine.mygymroutine/COM。 mygymroutine.mygymroutine.activities.ActivitySchedule}: java.lang.IllegalArgumentException異常:(:ID/fragment_schedule com.mygymroutine.mygymroutine),用於片段 ScheduleFragment {39f1839#0的id = 0x7f0d008b ...

未發現ID 0x7f0d008b 視圖

這是相關的代碼:

public class basic extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_basic); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     displaySelectedScreen(R.id.nav_schedule); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.basic, menu); 
     return true; 
    } 

    @Override 
    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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
public boolean onNavigationItemSelected(MenuItem item) { 
     displaySelectedScreen(item.getItemId()); 
     return true; 
    } 
    private void displaySelectedScreen(int itemId) { 
     Intent intent=null; 
     switch (itemId) { 
      case R.id.nav_schedule: 
       intent=new Intent(this, ActivitySchedule.class); 
       startActivity(intent); 
       break; 
     } 

活動代碼

package com.mygymroutine.mygymroutine.activities; 


import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 


import com.mygymroutine.mygymroutine.Fragments.ScheduleFragment; 
import com.mygymroutine.mygymroutine.R; 


public class ActivitySchedule extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.acivity_schedule); 
     if(savedInstanceState==null){ 
      ScheduleFragment fg = new ScheduleFragment(); 
      getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg) 
        .commit(); 
     } 
    } 
} 

片段

package com.mygymroutine.mygymroutine.Fragments; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import com.mygymroutine.mygymroutine.R; 

public class ScheduleFragment extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     //returning our layout file 
     //change R.layout.yourlayoutfilename for each of your fragments 
     View rootView=inflater.inflate(R.layout.fragment_schedule, container, false); 
     FloatingActionButton fab=(FloatingActionButton)rootView.findViewById(R.id.fab_schedule); 
     return inflater.inflate(R.layout.fragment_schedule, container, false); 
    } 


    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     getActivity().setTitle("Schedule"); 
    } 
} 

片段附表XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/fragment_schedule" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="17dp" 
     android:text="Schedule" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_schedule" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginBottom="41dp" 
     android:layout_marginEnd="32dp" 
     android:layout_marginRight="32dp" 
     android:clickable="true" 
     android:tint="@android:color/background_light" 
     app:backgroundTint="@android:color/holo_red_light" 
     app:fabSize="mini" 
     app:rippleColor="@android:color/transparent" 
     app:srcCompat="@android:drawable/ic_menu_add" /> 

</RelativeLayout> 

活動時間表的xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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" 
    tools:context="com.mygymroutine.mygymroutine.activities.ActivitySchedule"> 

</android.support.constraint.ConstraintLayout> 
+2

你的片斷應該'返回rootView',順便說一句 –

+0

變化'返回inflater.inflate(R.layout.fragment_schedule,容器,false);'返回'rootView;' – UltimateDevil

+0

在XM L,在哪裏設置了R.id.fragment_schedule?哪個檔案? –

回答

0

這裏:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg) 
       .commit(); 

你傳入R.id.fragment_schedule的片段的容器。片段將被插入的位置。但是,R.id.fragment_scheduleFragment的一部分。所以,它還沒有膨脹。您不能將其用作容器,因爲它尚未創建。

我想你想的是:

<android.support.constraint.ConstraintLayout 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" 
    tools:context="com.mygymroutine.mygymroutine.activities.ActivitySchedule" 

    <!-- ADD LINE BELOW --> 
    android:id="@+id/fragment_container" > 

</android.support.constraint.ConstraintLayout> 

並改變這一點:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg) 
       .commit(); 

這樣:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fg) 
       .commit(); 
+0

現在它的工作,非常感謝! –

0

In。該onOptionsItemSelected方法添加以下 (這種方法帶來了一個參數 - 菜單項項)

switch (item.getItemId()) 
    case R.id.myFirst 
     startActivity(new Intent(this, MyFirstActivity.class)); 
    case R.id.mySecond 
     startActivity (newIntent(this, MySecondActivity.class)); 

等。您可以使用必要的視圖爲活動定義佈局。您也可以在Manifest文件中聲明該活動。

This Works。我是這樣開始從主體開始我的活動的。