从android启动活动时出错
我有一个包含3个片段的MainActivity,我想从MainActivity中的一个片段切换到Activity2,但我的尝试总是失败。当我按下第三个片段中的ok按钮将我连接到Activity2时,我的应用程序崩溃。我正在编写我在一个教程中找到的代码。提前谢谢你从android启动活动时出错,android,android-layout,android-studio,android-fragments,android-activity,Android,Android Layout,Android Studio,Android Fragments,Android Activity,我有一个包含3个片段的MainActivity,我想从MainActivity中的一个片段切换到Activity2,但我的尝试总是失败。当我按下第三个片段中的ok按钮将我连接到Activity2时,我的应用程序崩溃。我正在编写我在一个教程中找到的代码。提前谢谢你 public class ProfileFragment extends Fragment implements View.OnClickListener { private TextView tv_name,tv_email
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;
@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) {
Intent intent=new Intent(getActivity(),activity2.class);
startActivity(intent);
}
});
}
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);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_chg_password:
showDialog(); //I deleted this method from the code, it doesnt have a lot in common with my question
break;
case R.id.btn_logout:
logout();
break;
}
private void logout() {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,false);
editor.putString(Constants.EMAIL,"");
editor.putString(Constants.NAME,"");
editor.putString(Constants.UNIQUE_ID,"");
editor.apply();
goToLogin();
}
private void goToLogin(){
Fragment login = new LoginFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,login);
ft.commit();
}
}
您无法在片段中调用方法
goToLogin()
,您需要在活动中调用包含此片段的方法,因为您的片段不包含布局fragment\u frame
,它在您的活动中
如果要在片段中调用此方法,请将方法goToLogin()
移动到活动中,并按如下方式调用片段:
if(getActivity() instanceOf yourActivity) {
((youActivity) getActivity()).goToLogin();
}
将getActivity作为上下文添加到startActivity()之前 还要添加NewTask标志
Intent intent = new Intent(getActivity(), activity2.class);
intent.addflags(intent.flag_activity_new_task);
getActivity().startActivity(intent);
发生错误的原因可能是您没有在AndroidManifest.xml中注册Activity2 但是要从片段到活动进行通信,您应该使用MainActivity作为它的入口。虽然这看起来有点过头了,但它将为将来更大的项目简化维护 您可以为此使用界面 在ProfileFragment中定义并创建一个接口:
public class ProfileFragment extends Fragment implements View.OnClickListener {
OnProfileListener mCallback;
// Container Activity must implement this interface
public interface OnProfileListener {
public void onProfileButtonOkClicked();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnProfileListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnProfileListener");
}
}
...
...
}
然后调用按钮单击方法中的界面:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
...
btn_ok=(AppCompatButton)view.findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onProfileButtonOkClicked();
}
});
...
}
最后,您需要实现MainActivity的接口:
public static class MainActivity extends Activity
implements ProfileFragment.OnProfileListener{
...
// When button ok in ProfileFragment clicked, this method will be called.
public void onProfileButtonOkClicked() {
// we can call the Activity here now.
Intent intent=new Intent(this, activity2.class);
startActivity(intent);
}
}
有关更多详细信息,请阅读
更新 对于API级别>=23中已弃用的
onAttach()
,可以使用以下代码:
@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");
}
}
得到的错误是什么,您是否在清单中声明了activity2?您是否可以显示R.layout.fragment\u配置文件?您的片段已实现onclick listener在开关条件下添加此选项
case R.id.btn\u注销:Intent Intent=new Intent(getActivity(),activity2.class);星触觉(意向);中断代码>它说当我使用Activity时,onAttach是不推荐使用的。当我尝试使用上下文时,我的应用程序在启动时崩溃,可能是因为它是API16,而上下文需要API23。我完全困惑了…帮帮我please@Victoria:我已经更新了代码。android团队在API级别>=23时不推荐使用onAttach()方法。谢谢你,但我最终通过使用getActivity()而不是Intent-Intent=new-Intent(getActivity(),Activity2.class)中的方法解决了这个问题@Victoria因此发送错误日志崩溃可能还有另一个原因。谢谢我设法找到了我的错误,它不在您的代码中,而是在activity2.class中,一切正常:)