[英]App crashes when I try to switch from fragment to activity
我在Main Activity中有3個片段,其中一個具有要啟動Activity 2的按鈕。我在第三個片段中創建了接口,並用它擴展了Main Activity,但是我無法弄清為什么我的應用仍然崩潰。 我在這個問題上損失了2天,這讓我發瘋了。 我的活動2在清單文件中聲明。 請幫忙!!!
Profile.Fragment:
public class ProfileFragment extends Fragment implements View.OnClickListener {
private TextView tv_name,tv_email,tv_message;
private SharedPreferences pref;
private AppCompatButton btn_change_password,btn_logout, btn_ok;
private EditText et_old_password,et_new_password;
private AlertDialog dialog;
private ProgressBar progress;
public interface OnProfileListener{
void onProfileButtonOkClicked();
}
private OnProfileListener mCallBack;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile,container,false);
initViews(view);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
pref = getActivity().getPreferences(0);
tv_name.setText("Здравей, "+pref.getString(Constants.NAME,"")+"!");
tv_email.setText(pref.getString(Constants.EMAIL,""));
btn_ok=(AppCompatButton)view.findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallBack.onProfileButtonOkClicked();
}
});
}
//I have API16 and I cannot run my app with
//onAttach(Context context) because it is supported by API>=23,
//but Android deprecated API16, so that is why I use both Activity and Context
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
if (activity instanceof OnProfileListener){
mCallBack = (OnProfileListener) activity;
} else {
throw new RuntimeException(activity.toString()
+ " must implement OnProfileListener");
}
}}
@TargetApi(23)
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnProfileListener) {
mCallBack = (OnProfileListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnProfileListener");
}
}
...
private void initViews(View view){
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_email = (TextView)view.findViewById(R.id.tv_email);
btn_change_password = (AppCompatButton)view.findViewById(R.id.btn_chg_password);
btn_logout = (AppCompatButton)view.findViewById(R.id.btn_logout);
btn_ok=(AppCompatButton)view.findViewById(R.id.btn_ok);
btn_change_password.setOnClickListener(this);
btn_logout.setOnClickListener(this);
}
//Here I go to other fragment in Main Activity flawlessly, wish I could manage to go Activity2 with the same ease
private void goToLogin(){
Fragment login = new LoginFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,login);
ft.commit();
}
}
MainActivity.java:
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity implements
ProfileFragment.OnProfileListener{
private SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
initFragment();
onProfileButtonOkClicked();
}
@Override
public void onProfileButtonOkClicked() {
Intent intent=new Intent(this, Activity2.class);
startActivity(intent);
}
private void initFragment(){
Fragment fragment;
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
fragment = new ProfileFragment();
}else {
fragment = new LoginFragment();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,fragment);
ft.commit();
}
當第一個活動仍處於創建狀態時,您不應啟動另一個活動。 我不知道為什么您要在輕按按鈕之前啟動activity2。 如果需要,可以將onProfileButonOKClicked()放在活動的onCreated()處理程序中。
要使意圖從片段變為活動,請添加此內容,
public void functionname(View v)
{
Intent in = new Intent(getActivity(), MainActivity2.class);
startActivity(in);
}
在這里,將該行添加到該按鈕所在的活動的.xml文件中
機器人:的OnClick = “functionname”
而“ MainActivity2.class”是下一個要移動的活動。
“目的是通過該意圖單擊按鈕以轉到mainactivity2.class ...
希望能有所幫助..謝謝,
您應該嘗試使用getActivityContext
public void functionname(View v)
{
Intent in = new Intent(getActivityContext(), MainActivity2.class);
startActivity(in);
}
當您調用此方法時,請確保您位於onCreate內
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
functionname(v);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.