我无法从 Homefragment.java
设置文字目前,我有 xml 文件:
activity_main.xml中
<LinearLayout
android:id="@+id/tab_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<include
android:id="@+id/ftr"
layout="@layout/tab" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
现在 tab.xml
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
现在是类文件:
activity_main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
initUI();
}
private void initUI() {
fragment = new HomeFragment();
}
HomeFragment.java
public class HomeFragment extends Fragment {
private TextView txt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
context = getActivity();
initUI(rootView);
return rootView;
}
private void initUI(View view) {
txt = (TextView)view.findViewbyid(R.id.txt1);
txt.setText("hello. test");
}
fragment_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
....
</RelativeLayout
错误日志:
Caused by: java.lang.NullPointerException
at com.xx.ew.fragment.HomeFragment.initUI(HomeFragment.java:112)
at com.xx.ew.fragment.HomeFragment.onCreateView(HomeFragment.java:99)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5356)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
现在问题是当我尝试在 homefragment 类中设置 tab.xml 的 Textview 文本时,它未设置为文本和强制关闭应用程序。
如何从 homefragment.java 设置 tab.xml 的 textview(txt1)的文字?
答案 0 :(得分:0)
更改了我的回答
在initUI()活动方法中按如下方式调用片段: -
private void initUI() {
fragment = new HomeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.tab_bar, fragment);
fragmentTransaction.commit();
}
在Fragment中,只更改 createView 方法
中的一行View rootView = inflater.inflate(R.layout.tab, container,
false);
答案 1 :(得分:0)
MainActivity.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = new HomeFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
HomeFragment.java
public class HomeFragment extends Fragment {
private Context context;
private TextView txt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
context = getActivity();
initUI(rootView);
return rootView;
}
private void initUI(View view) {
txt = (TextView) view.findViewById(R.id.txt1);
txt.setText("hello. test");
}
}
fragment_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="@+id/ftr"
layout="@layout/tab" />
</RelativeLayout>
答案 2 :(得分:-1)
尝试: 使rootView成为一个全局变量。然后重写onStart方法并从那里调用initUI。