gpt4 book ai didi

android - 将 View 动态添加到特定的 fragment 布局中

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:48:04 26 4
gpt4 key购买 nike

我正在尝试将 TextView 动态添加到作为 fragment 一部分的布局中,但它不起作用。应用程序启动后立即关闭。我认为这与TextView Paper = new TextView(this); 部分。我不知道如何获得正确的 Context 因为我正在使用 Fragments。也许我的假设是错误的,但我真的不知道错误在哪里。

public class PageFragment extends Fragment {

private int fragmentNR;

public PageFragment(int nr) {
this.fragmentNR = nr;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = new View(getActivity());
if (fragmentNR == 0)
v = inflater.inflate(R.layout.page1, container, false);
else if (fragmentNR == 1)
v = inflater.inflate(R.layout.page2, container, false);
else if (fragmentNR == 2)
v = inflater.inflate(R.layout.page3, container, false);

return v;
}

}
public class MainActivity extends FragmentActivity  {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PageAdapter mPageAdapter = new PageAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mPageAdapter);

//This is where i try to add my TextView into my Layout
LinearLayout PaperLayout = (LinearLayout) findViewById(R.id.PaperLayout);//PaperLayout is a LinearLayout within the Page2-Fragment(R.layout.page2)
TextView Paper = new TextView(this);
Paper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Paper.setText("Paper2");
PaperLayout.addView(Paper);
}
}

日志

07-18 16:32:58.330: E/AndroidRuntime(614): FATAL EXCEPTION: main
07-18 16:32:58.330: E/AndroidRuntime(614): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loni.math_prime/com.loni.math_prime.MainActivity}: java.lang.NullPointerException
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.os.Looper.loop(Looper.java:137)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-18 16:32:58.330: E/AndroidRuntime(614): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:32:58.330: E/AndroidRuntime(614): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 16:32:58.330: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 16:32:58.330: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 16:32:58.330: E/AndroidRuntime(614): at dalvik.system.NativeStart.main(Native Method)
07-18 16:32:58.330: E/AndroidRuntime(614): Caused by: java.lang.NullPointerException
07-18 16:32:58.330: E/AndroidRuntime(614): at com.loni.math_prime.MainActivity.onCreate(MainActivity.java:30)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.Activity.performCreate(Activity.java:5008)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-18 16:32:58.330: E/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-18 16:32:58.330: E/AndroidRuntime(614): ... 11 more

XML

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

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<android.support.v4.view.PagerTitleStrip
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32dp"
android:textStyle="bold"
android:textColor="#fff5ee"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>

</LinearLayout>

最佳答案

已解决:

public class PageFragment extends Fragment {

private int fragmentNR;


public PageFragment(int nr) {
this.fragmentNR = nr;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v = new View(getActivity());

if (fragmentNR == 0)
v = inflater.inflate(R.layout.page1, container, false);
else if (fragmentNR == 1){
v = inflater.inflate(R.layout.page2, container, false);
View tv = v.findViewById(R.id.Ausgabe);
((TextView)tv).setText("TestText");
View pl = v.findViewById(R.id.PageLayout);
TextView Paper = new TextView(pl.getContext());
Paper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Paper.setText("Inserted TestText");
((LinearLayout)pl).addView(Paper);
}
else if (fragmentNR == 2)
v = inflater.inflate(R.layout.page3, container, false);

return v;
}
}

关于android - 将 View 动态添加到特定的 fragment 布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11545443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com