无法使用“R.id.myFirstFragment”引用“片段”
Unable to reference `fragment` using `R.id.myFirstFragment`
这里是片段class
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>
问题是当我在 AppCompatActivity
的 onCreate 中使用 this.getFragmentManager().findFragmentById(R.id.myFirstFragment)
我得到 Fragment
虽然我应该得到 WorkoutDetailFragment
正如在类似的例子中所述 here .
更新:
我也无法将 Fragment
转换为 WorkDetailFragment
获取片段有什么问题? getFragmentManager().findFragmentById()
方法 returns a Fragment
并且你必须转换它。由于您正在将 Fragment
转换为 WorkoutDetailFragment
,因此您应该能够用它做您想做的事。
我认为它必须是 <fragment android:name="com.hfad.chapter7.WorkoutDetailFragment"...
而不是 class
。
=> http://developer.android.com/training/basics/fragments/creating.html#AddInLayout
@user4913383,
更改此行:
workoutDetailFragment = (WorkoutDetailFragment)this.getFragmentManager().findFragmentById(R.id.myFirstFragment);
到
workoutDetailFragment = (WorkoutDetailFragment)this.getSupportFragmentManager().findFragmentById(R.id.myFirstFragment);
这里是片段class
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>
问题是当我在 AppCompatActivity
的 onCreate 中使用 this.getFragmentManager().findFragmentById(R.id.myFirstFragment)
我得到 Fragment
虽然我应该得到 WorkoutDetailFragment
正如在类似的例子中所述 here .
更新:
我也无法将 Fragment
转换为 WorkDetailFragment
获取片段有什么问题? getFragmentManager().findFragmentById()
方法 returns a Fragment
并且你必须转换它。由于您正在将 Fragment
转换为 WorkoutDetailFragment
,因此您应该能够用它做您想做的事。
我认为它必须是 <fragment android:name="com.hfad.chapter7.WorkoutDetailFragment"...
而不是 class
。
=> http://developer.android.com/training/basics/fragments/creating.html#AddInLayout
@user4913383,
更改此行:
workoutDetailFragment = (WorkoutDetailFragment)this.getFragmentManager().findFragmentById(R.id.myFirstFragment);
到
workoutDetailFragment = (WorkoutDetailFragment)this.getSupportFragmentManager().findFragmentById(R.id.myFirstFragment);