当我尝试从片段切换到活动时,应用程序崩溃

时间:2017-02-22 15:46:30

标签: java android performance android-studio android-fragments

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

3 个答案:

答案 0 :(得分:0)

当第一个活动仍处于创建状态时,您不应该启动另一个活动。在按下按钮之前,我不知道你为什么要启动activity2。 如果需要,可以将onProfileButonOKClicked()放在onCreated()活动处理程序中。

答案 1 :(得分:0)

要从片段到​​活动制作意图,请添加此内容,

     public void functionname(View v)
     {
        Intent in = new Intent(getActivity(), MainActivity2.class);
        startActivity(in);
     }

此处,将此行添加到按钮所在的该活动的.xml文件中

机器人:的OnClick =&#34; functionname&#34;

和&#34; MainActivity2.class&#34;是下一个要移动的活动。

&#34;目标是点击按钮通过该意图转到mainactivity2.class ...

希望它会有所帮助..谢谢,

答案 2 :(得分:0)

您应该尝试使用 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);

            }
        });