我继续在setText
方法上获取此空对象引用。与导航标头一起正常工作,但在片段中它一直崩溃。我尝试过这种方式,或使用Bundle传递字符串,但似乎没有用。
我应该如何测试或应该进行哪些更改?
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
//NavHeader
private TextView header_name, header_email;
//SessionManager
SessionManager sessionManager;
//maps
private static final String TAG = "MainActivity";
private static final int ERROR_DIALOG_REQUEST = 9001;
private static final String FINELOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COURSELOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private Boolean mLocationPermissionsGranted = false;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private static final float DEFAULT_ZOOM = 15f;
private FusedLocationProviderClient mFusedLocationProviderClient;
private GoogleMap mMap;
String pontopartida, pontochegada;
private EditText ponto_partidaInput, ponto_chegadaInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GET USER DATA
sessionManager = new SessionManager(this);
sessionManager.checkLogin();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
header_name = (TextView) headerView.findViewById(R.id.header_name);
header_email = (TextView) headerView.findViewById(R.id.header_email);
HashMap<String, String> user = sessionManager.getUserDetail();
String mName = user.get(sessionManager.NAME);
String mEmail = user.get(sessionManager.EMAIL);
header_name.setText(mName);
header_email.setText(mEmail);
Log.d("Name: ", mName);
Log.d("Email: ", mEmail);
FragmentManager MyAccfrag = getSupportFragmentManager();
MyAccountFragment fragment = (MyAccountFragment) MyAccfrag.findFragmentById(R.id.nav_myaccount);
fragment.setText(mName, mEmail);
...
public class MyAccountFragment extends Fragment {
private TextView account_name;
private TextView account_email;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_myaccount, container, false);
account_name = (TextView) view.findViewById(R.id.name_accountfrag);
account_email = (TextView) view.findViewById(R.id.email_accountfrag);
return view;
}
public void setText (String Name, String Email){
account_name.setText(Name);
account_email.setText(Email);
}
}