在活動中呼叫碎片的控制元件然后更改其屬性。
主活動布局如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout_root">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_change"
android:text="change"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java 如下:
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Fragment fragment;
private Button buttonChange;
private void initFragment(Bundle savedInstanceState){
if(savedInstanceState == null){
fragment = new DemoFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
// fragmentTransaction.add().commit();
fragmentTransaction.commit();
}
}
private TextView getFragmentTextView(){
TextView textView = null;
getSupportFragmentManager().executePendingTransactions();
textView = getSupportFragmentManager().findFragmentByTag("DemoFragment").getView().findViewById(R.id.text_view);
getSupportFragmentManager().executePendingTransactions();
return textView;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFragment(savedInstanceState);
buttonChange = (Button) findViewById(R.id.button_change);
buttonChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentTextView().setText("Content End.");
}
});
}
}
碎片布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:text="Test Start."
android:gravity="center"/>
</LinearLayout>
DemoFragment.java 如下:
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class DemoFragment extends Fragment {
private TextView textView;
public DemoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View demoView = inflater.inflate(R.layout.fragment_demo, container, false);
demoView.setTag("DemoFragment");
textView = (TextView) demoView.findViewById(R.id.text_view);
return demoView;
}
}
點擊button后報錯如下:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sg.fragmentsupportdemo1, PID: 4575
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.fragment.app.Fragment.getView()' on a null object reference
at com.sg.fragmentsupportdemo1.MainActivity.getFragmentTextView(MainActivity.java:32)
at com.sg.fragmentsupportdemo1.MainActivity.access$000(MainActivity.java:14)
at com.sg.fragmentsupportdemo1.MainActivity$1.onClick(MainActivity.java:49)
網上有的說應該用getSupportFragmentManager而不是getFragmentManager,有的說加上executePendingTransactions()因為碎片事務是異步執行緒處理,還有的說未對空值判斷、未在其它生命周期內寫(onStart等),這些方法都試了均不行,還望大佬指點迷津,感激不盡
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/168328.html
標籤:Android
上一篇:怎么買pyqt5著作權費的
下一篇:如何判斷一個物件是陣列
