(首先我必须说我有一个普通的英语)。
大家好,我试图从LinearLayout
改变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代码中,我尝试将每个片段放在线性布局中,以便能够更轻松地修改它们的重量。
<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)
答案 0 :(得分:2)
在您的代码中尝试此操作。
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);
找到
的相应IDLinearLayout
答案 1 :(得分:1)
尝试将LinearLayout置于内部匿名方法之外,并从相对布局参数切换到线性布局参数:
View v = inflater.inflate(R.layout.fragment_info1, container, false);
LinearLayout 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;
XX.setLayoutParams(param);
}
});
return v;