No view found for id 0x7f090005

2019-02-28 12:44发布

问题:

I'm starting to build a simple Android app so when I press my video button it calls a fragment which contains the video player, but I'm always getting: No view found for id 0x7f090005...

I wonder if you give me a hand by figuring out what is wrong in the code that I'm writing.

Here is my MainActivity.java file:

public class MainActivity extends Activity {

    private Button mVideoButton;
    Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity); 

        mVideoButton = (Button)findViewById(R.id.videoButton);

        // Video Button
        mVideoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                FragmentVideo sf = new FragmentVideo();

                ft.add(R.id.fragmentVideo, sf);
                ft.commit();
            }
        });
    }
}

Then I have my main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activityStart" >

    <ImageView 
        android:src="@drawable/armstrong_on_moon"
        android:contentDescription="@string/description"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:scaleType="centerInside"
        android:layout_weight="1" />

    <TableRow 
        android:gravity="center|bottom"
        android:layout_weight="0">

        <Button 
            android:id="@+id/videoButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/video" />

    </TableRow>

Next, my FragmentVideo.java class:

public class FragmentVideo extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState ){
        View v = inflater.inflate(R.layout.fragment_video, parent, false);

        return v;
    }   
}

And at last, the fragment_video.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragmentVideo" >

        <VideoView
            android:id="@+id/video"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center" />

        <ProgressBar
            android:id="@+id/prog"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center" />

</LinearLayout>

Please let me know if I'm doing something wrong or if I'm missing something as I can see where the issue lays.

Thanks very much for any help in advance.

EGMWEB

回答1:

On ft.add() the int parameter refers to the container to which you want to add the Fragment; you are trying to add the Fragment to the Fragment's own layout (Android doesn't find the View because it is not inflated yet, if it did it will throw and Exception in this case; you cannot add a Fragment to its own layout). You want to replace R.id.fragmentVideo with R.id.activityStart to add the Fragment to the current TableLayout that is part of the already inflated main_activity layout



回答2:

Could you check whats the equivalent view for the id '0x7f090005' in the R.java file. It should be under gen folder of your project. The framework is not able to find this under the resource file that you have in this java class.