我在尝试利用bundle,传递string数据给fragment,打印log的时候出错,提示我on a null object reference;
相关代码如下,MainAcitivity中:
package com.example.fragmentmanger;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.btn1);
Button btn2 = findViewById(R.id.btn2);
Button btn3 = findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
Log.e("Fragment1", "fragment1 click");
Bundle bundle = new Bundle();
bundle.putString("message", "i love android");
BlankFragment1 bf1 = new BlankFragment1();
bf1.setArguments(bundle);
replaceFrag(new BlankFragment1());
break;
case R.id.btn2:
Log.e("Fragment2", "fragment2 click");
replaceFrag(new BlankFragment2());
break;
case R.id.btn3:
Log.e("ItemFragment", "item fragment click");
replaceFrag(new ItemFragment());
break;
default:
break;
}
}
/**
* 动态切换fragment
*
* @param fragment
*/
private void replaceFrag(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout1, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
在BlankFragment1中代码如下:
package com.example.fragmentmanger;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
/**
* A simple {@link Fragment} subclass.
* Use the {@link BlankFragment1#newInstance} factory method to
* create an instance of this fragment.
*
* @author MAC
*/
public class BlankFragment1 extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static final String TAG = "BlankFragment1";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public BlankFragment1() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment1.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment1 newInstance(String param1, String param2) {
BlankFragment1 fragment = new BlankFragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
String res = bundle.getString("message");
Log.e(TAG, "onCreate:" + res);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank1, container, false);
}
}