gpt4 book ai didi

android - 更改 fragment 中 LinearLayout 的权重 - Android Studio

转载 作者:行者123 更新时间:2023-11-29 16:50:07 24 4
gpt4 key购买 nike

(首先我必须说我有一个普通的英语)。大家好,我正在尝试更改 FragmentLinearLayout 的权重。这是我的代码,但是在单击 fragment 时它崩溃了。这个想法是,在点击 fragment 的那一刻,它会填满移动屏幕,其他 fragment 也是如此。

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

View v=inflater.inflate(R.layout.fragment_info1, container, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
XX.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT,0));
}
});
return v;
}

这里是错误:

10-10 20:06:54.222 30286-30286/com.example.jhon.myapplication E/AndroidRuntime:
FATAL EXCEPTION: main: com.example.jhon.myapplication, PID: 30286
java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.example.jhon.myapplication.Info1$1.onClick(Info1.java:79)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

在 xml 代码中,我尝试将每个 fragment 放在线性布局中,以便能够更轻松地修改它们的权重。

<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"
android:orientation="vertical"
tools:context="com.example.jhon.myapplication.Informacion">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/LinearInfo1">
<fragment
android:id="@+id/fragment"
android:name="com.example.jhon.myapplication.Info1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_info1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LinearInfo2"
android:layout_weight="1">
<fragment
android:id="@+id/fragment2"
android:name="com.example.jhon.myapplication.Info2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_info2" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

新的 Logcat 信息:

10-10 21:06:03.232 10887-10887/com.example.jhon.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jhon.myapplication, PID: 10887
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.example.jhon.myapplication.Info1$1.onClick(Info1.java:80)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

最佳答案

在您的代码中试试这个。

private LinearLayout XX;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_info1, container, false);
// add here
XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;

// edited here
if (XX != null) {
XX.setLayoutParams(param);
}
}
});
return v;
}

- 将 LinearLayout 作为全局变量。

- 使 LinearLayout 最终化

final LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);

Find the corresponding ID of LinearLayout

关于android - 更改 fragment 中 LinearLayout 的权重 - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46678161/

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