0

我有MainActivity其中有3个片段,我想从MainActivity中的片段之一切换到Activity2,但我的尝试总是失败。当我按下第三个片段中的按钮ok将我连接到Activity2时,我的应用程序崩溃。我正在研究我在一篇教程中找到的代码。先谢谢你!从片段android启动活动错误

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(); 
    } 


} 
+0

什么是你的错误,你有没有在声明活性2明显吗? –

+0

你可以显示R.layout.fragment_profile? –

+0

您的片段已经实现的onclick监听器添加这对开关状态 '情况R.id.btn_logout: 意向意图=新意图(getActivity(),activity2.class); startActivity(intent); 突破;' –

回答

0

startActivity() 还添加NewTask标志前加getActivity作为背景。

Intent intent = new Intent(getActivity(), activity2.class); 
intent.addflags(intent.flag_activity_new_task); 
getActivity().startActivity(intent); 
+0

我想你的方式,但我的应用程序再次崩溃。 – Victoria

+0

@Victoria所以送的错误日志有可能是另一个原因崩溃。 –

+1

谢谢你,我设法找到了我的错误,这是不是在你的代码,它是在活性2。类和一切工作正常:) – Victoria

0

你不能调用方法goToLogin()的片段,你需要在你的活动来调用包含此片段,因为您的片段不包含在你的活动布局fragment_frame,它的。

如果要调用这个方法在你的片段,你的一举一动方法goToLogin()到yourActivity并调用您的片段是这样的:

if(getActivity() instanceOf yourActivity) { 
     ((youActivity) getActivity()).goToLogin(); 
} 
0

错误可能发生,因为您没有在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); 
    } 
} 

欲了解更多详情,请阅读Communicating with Other Fragments


UPDATE

对于API级别弃用onAttach()> = 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"); 
    } 
} 
+0

它说onAttach已弃用,当我使用活动。当我尝试使用上下文时,我的应用程序开始崩溃可能是因为它是API16而上下文需要API23。我完全不解...请帮助我 – Victoria

+0

@Victoria:我已经更新了代码。 onAttach()方法在API Level> = 23时被android团队弃用。 –

+0

谢谢,但我最终设法通过在Intent中使用getActivity()而不是这个intent = new Intent(getActivity())来解决这个问题。 Activity2.class); – Victoria