无法使用`R.id.myFirstFragment`引用`fragment`

时间:2016-05-10 11:31:07

标签: android android-fragments r.java-file

这是片段类

package com.hfad.chapter7;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class WorkoutDetailFragment extends Fragment {

    private long workoutID;


    public WorkoutDetailFragment() {
        // 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_workout_detail, container, false);
    }


    public void setWorkoutID(long workoutID) {
        this.workoutID = workoutID;
    }
}

它使用的布局是:

<FrameLayout 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"
    tools:context="com.hfad.chapter7.WorkoutDetailFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

我的MainActivity是:

package com.hfad.chapter7;

import android.app.Activity;
import android.app.Fragment;
import android.provider.UserDictionary;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        WokoutDetailFragment workoutDetailFragment;
        workoutDetailFragment = (WorkoutDetailFragment)this.getFragmentManager().findFragmentById(R.id.myFirstFragment);


    }
}

MainActivity的布局是:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hfad.chapter7.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

    <fragment
        class="com.hfad.chapter7.WorkoutDetailFragment"
        android:id="@+id/myFirstFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



</LinearLayout>

问题是当我在this.getFragmentManager().findFragmentById(R.id.myFirstFragment)的onCreate中使用AppCompatActivity时我得到Fragment但是我应该在类似的示例中按照here获得WorkoutDetailFragment。< / p>

更新:

此外,我无法将Fragment投射到WorkDetailFragment enter image description here

3 个答案:

答案 0 :(得分:1)

@ user4913383,

更改此行:

workoutDetailFragment = (WorkoutDetailFragment)this.getFragmentManager().findFragmentById(R.id.myFirstFragment);

workoutDetailFragment = (WorkoutDetailFragment)this.getSupportFragmentManager().findFragmentById(R.id.myFirstFragment);

答案 1 :(得分:0)

获取片段的问题是什么? getFragmentManager().findFragmentById()方法返回Fragment,您必须将其强制转换。由于您将Fragment投射到WorkoutDetailFragment,因此您应该可以随心所欲地使用它。

答案 2 :(得分:0)

我认为必须是<fragment android:name="com.hfad.chapter7.WorkoutDetailFragment"...而不是class。 =&GT; http://developer.android.com/training/basics/fragments/creating.html#AddInLayout