2017-02-22 93 views
0

我在主要活动中有3个片段,其中一个有一个按钮,我想要启动活动2.我在第三个片段中创建了接口并使用它扩展了主要活动,但我无法弄清楚为什么我的应用程序仍然崩溃。我在这个问题上失去了2天,并且使我疯狂。我的活动2在Manifest文件中声明。请帮忙!!!当我尝试从片段切换到活动时,应用程序崩溃

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

你将张贴崩溃日志请。 –

+0

我的应用程序在移动设备上启动,然后崩溃,在事件日志中没有看到错误,它写入BUILD SUCCESSFUL – Victoria

+0

因此,您的应用程序在启动时崩溃了吗? –

回答

0

为了意图从片段活动添加此,,

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

这里,该活动在按钮所在的.xml文件中加入这一行

安卓的OnClick =“functionname”

和“MainActivity2.class”是下一个要移动的活动。

“的目标是点击按钮即可进入经由意图mainactivity2.class ...

希望它会help..thanks ,,

0

当第一个活动仍处于正在创建的国家,你不应该开始另一项活动。我不知道为什么你在点击按钮之前启动activity2。 如果需要,可以将onProfileButonOKClicked()放入activity的onCreated()处理函数中。

+0

我的目标是点击按钮时启动Activity2,这就是我添加onProfileButtonOkClicked()的原因;因为我认为它的作为OnClickListener – Victoria

相关问题