我有一个底部导航栏,其中包含用户可以选择的 4 个菜单项。根据他们选择的内容,确定哪个片段显示在我的框架中。我可以加载片段并可以与它们进行交互。但是我还需要向每个片段发送一些字符串值,但无法找出我出错的地方。当我启动我的片段并尝试使用 getArguments(( 访问我的值时,它总是返回 null。 下面是我的代码:
unitactivity.java我在其中运行代码以选择片段
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
public class UnitActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigation;
private Fragment fragment;
private FragmentManager fragmentManager;
Boolean CheckEditText;
ProgressDialog progressDialog;
String ReadOnly;
String LexaUser;
String Password;
String QA;
String SearchValue;
String UnitId;
String UnitSize;
String UnitStatus;
Button UnitDetailsHeader;
TextView UnitSizeText;
TextView UnitStatusText;
String finalResultUnitDetails;
String HttpURLUnitDetails = "https://[path/to/server]/getUnitDetails.php";
HashMap<String, String> hashMapUnitDetails = new HashMap<>();
HttpParse httpParse = new HttpParse();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_units);
Intent intent = getIntent();
LexaUser = intent.getStringExtra("UserName");
ReadOnly = intent.getStringExtra("ReadOnly");
Password = intent.getStringExtra("Password");
QA = intent.getStringExtra("QA");
SearchValue = intent.getStringExtra("SearchInput");
bottomNavigation = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigation.inflateMenu(R.menu.bottom_menu);
fragmentManager = getSupportFragmentManager();
bottomNavigation.getMenu().getItem(0).setChecked(true);
UnitDetailsHeader = findViewById(R.id.UnitDetailsViewTitle);
UnitDetailsHeader.setText(SearchValue);
UnitSizeText = findViewById(R.id.UnitSize);
UnitStatusText = findViewById(R.id.UnitStatus);
if (SearchValue.contains("-")) {
getUnitDetails(SearchValue, LexaUser);
} else {
getSiblings();
}
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_search:
fragment = new NewUnitStatusFragment();
break;
case R.id.action_cart:
fragment = new PendingUnitStatusFragment();
break;
case R.id.action_hot_deals:
fragment = new FinalUnitStatusFragment();
break;
case R.id.action_siblings:
fragment = new SiblingUnitFragment();
break;
}
Bundle connBundle = new Bundle();
connBundle.putString("SearchValue", SearchValue);
connBundle.putString("LexaUser", LexaUser);
connBundle.putString("Password", Password);
connBundle.putString("QA", QA);
fragment.setArguments(connBundle);
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
}
}
当我尝试访问片段中的值时:(onCreateView(
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (bundle != null) {
SearchValue = bundle.getString("SearchValue");
LexaUser = bundle.getString("LexaUser");
Password = bundle.getString("Password");
}
View view = inflater.inflate(R.layout.fragment_newunitstatus, container, false);
newUnitStatusContext = getContext();
newUnitStatusActivity = getActivity();
statusSpinner = view.findViewById(R.id.Status);
generalCauseSpinner = view.findViewById(R.id.GeneralCause);
newUSComment = view.findViewById(R.id.NewComment);
newUnitStatusImage = view.findViewById(R.id.AddPhoto);
addPhotoBtn = view.findViewById(R.id.AddPhotosLabel);
addNewUnitStatus = view.findViewById(R.id.addBtnNewUnitStatus);
ArrayAdapter<CharSequence> statusSpinnerAdapter = ArrayAdapter.createFromResource(newUnitStatusContext,
R.array.new_unit_status_array, android.R.layout.simple_spinner_item);
statusSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
statusSpinner.setAdapter(statusSpinnerAdapter);
newUnitStatus = statusSpinner.getSelectedItem().toString();
ArrayAdapter<CharSequence> generalCauseSpinnerAdapter = ArrayAdapter.createFromResource(newUnitStatusContext,
R.array.status_general_cause_array, android.R.layout.simple_spinner_item);
generalCauseSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
generalCauseSpinner.setAdapter(generalCauseSpinnerAdapter);
generalCause = generalCauseSpinner.getSelectedItem().toString();
addPhotoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startGallery();
}
});
// Set a click listener for the text view
addNewUnitStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newUnitStatus = statusSpinner.getSelectedItem().toString();
generalCause = generalCauseSpinner.getSelectedItem().toString();
newUnitStatusComment = newUSComment.getText().toString();
Toast.makeText(newUnitStatusContext, "SV: " +SearchValue + " US: " + newUnitStatus + " GC: " + generalCause + " USC: " + newUnitStatusComment + " User: " + LexaUser + " PW: " + Password, Toast.LENGTH_LONG).show();
//addNewUnitStatus(SearchValue, newUnitStatus, generalCause, newUnitStatusComment, newUnitStatusPhoto, LexaUser, Password);
}
});
return view;
}
感谢所有帮助!谢谢!
对于替代答案,您可以尝试以下方法:
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Bundle connBundle = new Bundle();
connBundle.putString("SearchValue", SearchValue);
connBundle.putString("LexaUser", LexaUser);
connBundle.putString("Password", Password);
connBundle.putString("QA", QA);
switch (id){
case R.id.action_search:
fragment = new NewUnitStatusFragment(connBundle);
break;
case R.id.action_cart:
fragment = new PendingUnitStatusFragment(connBundle);
break;
case R.id.action_hot_deals:
fragment = new FinalUnitStatusFragment(connBundle);
break;
case R.id.action_siblings:
fragment = new SiblingUnitFragment(connBundle);
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
在片段中,您可以从传递的捆绑包参数中获取参数,例如:
private Bundle bundle;
public static FragmentA(Bundle bundle) {
this.bundle = bundle;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (bundle != null) {
SearchValue = bundle.getString("SearchValue");
LexaUser = bundle.getString("LexaUser");
Password = bundle.getString("Password");
}
//etc...
}