我正在学习Android开发。我被困在应该很容易的事情上。
我正在创建一个具有一个 Activity、2 个片段和 1 个界面的应用程序。
android:minSdkVersion="11"
android:targetSdkVersion="19
因此,在主要活动中,我尝试使用管理器创建对片段 B 的引用。我被困在这里,因为 Eclispse 告诉我要改变一些事情(见下文):
我的意图:`
@Override
public void respond(int i) {
// TODO Auto-generated method stub
FragmentManager manager =getFragmentManager();
FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);
}`
如果我这样做,我会收到错误消息并需要执行一些更改。更改后的代码如下所示(我仍然无法到达 FragmentB):
@Override
public void respond(int i) {
// TODO Auto-generated method stub
android.app.FragmentManager manager =getFragmentManager();
android.app.Fragment f2= manager.findFragmentById(R.id.fragment2);
}
有关更多详细信息,我还将在此处放置 Activity 的导入标头:
package com.example.modular_ui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity implements Communicator{....
我在这里想念什么?整个 support.v4 /support.v7 对新手来说有点混乱。
编辑:更改为:
import android.app.Fragment;
import android.app.FragmentManager;
并扩展 FragmentActivity 我仍然无法创建对 FragmentB 的引用:
@Override
public void respond(int i) {
// TODO Auto-generated method stub
FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);
}
根据要求,我发布了 FragmentB 代码:
package com.example.modular_ui;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentB extends Fragment {
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_b, container);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
text = (TextView) getActivity().findViewById(R.id.textView1);
}
主要 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.modular_ui.MainActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.modular_ui.FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.modular_ui.FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/fragment1"
android:layout_marginTop="54dp" />
</RelativeLayout>