2016-09-05 18 views
-1

私はこの断片のものを初めて知りました。StackOverflowの質問では解決できないエラーが発生しました。フラグメントShooksFragment {1a77ec43#0 ID = 0x7f0c0078のために:(ID/shooksLayout com.shook.android.shook)ShooksFragmentのIDが見つかりません

私の問題は、私は、ID 0x7f0c0078見つかり

ノービューのエラーを取得していますということです

フラグメントをダイナミックに追加しようとするとき。

マイコード:

MainActivity

... 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_friends) { 
     Toast.makeText(getApplicationContext(), "Find friends!", Toast.LENGTH_LONG).show(); 

    } else if (id == R.id.nav_shooks) { 

     FragmentManager fragmentManager = getFragmentManager(); 

     FragmentTransaction transaction = fragmentManager.beginTransaction(); 

     ShooksFragment fragment = new ShooksFragment(); 
     transaction.add(R.id.shooksLayout, fragment); 

     transaction.commit(); 

    } else if (id == R.id.nav_prizes) { 
     Toast.makeText(getApplicationContext(), "Get prizes!", Toast.LENGTH_LONG).show(); 
    } else if (id == R.id.nav_share) { 
     Toast.makeText(getApplicationContext(), "Share your shooks!", Toast.LENGTH_LONG).show(); 
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
... 

ShooksFragment

public class ShooksFragment extends Fragment { 

public ShooksFragment() { 
    // Required empty public constructor 
} 




@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragment_shooks, container, false); 
} 

// TODO: Rename and change types and number of parameters 
public static ShooksFragment newInstance() { 

    ShooksFragment fragment = new ShooksFragment(); 

    return fragment; 
} 

} 

fragment_shooks.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.shook.android.shook.ShooksFragment" 
android:id="@+id/shooksLayout"> 

+0

、あなたは 'ShooksFragmentフラグメント= ShooksFragment.newInstance()を行う必要があります;' – Shaishav

答えて

0

この行は:R.id.shooksLayoutはあなたfragmentのレイアウトで定義されているので

transaction.add(R.id.shooksLayout, fragment); 

間違っています。 add()に指定したidは、setContentView()で指定したViewGroupidである必要があります。たとえば、私の断片の場合、私は通常自分の活動に空きを入れてをこの断片のadd()replace()という断片で提供しています。

0

これを試してみてください。まず

ShooksFragment fragment = ShooksFragment.newInstance(); 
transaction.add(R.id.shooksLayout, fragment); 
transaction.commit(); 
+0

これは実際には、あまりにも助けました!どうもありがとうございましたが、他の答えがもっと役に立つと思います! ありがとうとにかく – user6463207

+0

あなたはこの回答を投票することができます –

+0

十分な評判を持っていない:| – user6463207

関連する問題