Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

android - No view found for id for ShooksFragment

I'm new to all this fragments stuff, and I get an error that couldn't be solved by any of the StackOverflow's questions.

My problem is that I'm getting the error of

No view found for id 0x7f0c0078 (com.shook.android.shook:id/shooksLayout) for fragment ShooksFragment{1a77ec43 #0 id=0x7f0c0078

when trying to add a fragment dinamically.

My code:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This line:

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

is incorrect because R.id.shooksLayout is defined in the layout of your fragment. The id you specify in add() should be the id of a ViewGroup you specify in your setContentView() fot the activity. For example, for my fragments I usually keep an empty <FrameLayout> in my activity and then provide the id of this container in both fragment add() and replace() calls.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...