0

Ich habe 3 Fragmente in Hauptaktivität und eine von ihnen hat eine Schaltfläche, die ich Aktivität 2 starten möchte. Ich erstellte Schnittstelle im dritten Fragment und erweiterte die Hauptaktivität damit , aber ich kann nicht herausfinden, warum meine App immer noch abstürzt. Ich habe 2 Tage verloren und es macht mich verrückt. Meine Aktivität 2 wird in der Manifestdatei deklariert. Bitte helfen !!!App stürzt ab, wenn ich versuche, von Fragment zu Aktivität zu wechseln

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

Werden Sie das Crash-Protokoll hinterlassen bitte. –

+0

Meine app startet auf dem Handy und dann stürzt ab, ich sehe keinen Fehler im Ereignisprotokoll, es schreibt BUILD SUCCESSFUL – Victoria

+0

So stürzt Ihre App beim Start? –

Antwort

0

Um Absicht von Fragment zu Aktivität zu machen hinzufügen ,,

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

hier, fügen Sie diese Zeile in .xml-Datei dieser Tätigkeit, wo der Knopf befindet sich

android: OnClick = "Funktionsname"

und "MainActivity2.class" ist nächste Aktivität zu verschieben.

„Ziel ist, auf die Schaltfläche klicken, um zu gehen über diese Absicht mainactivity2.class ...

hoffen, dass es help..thanks ,,

0

Sie sollten keine weitere Aktivität starten, wenn die erste Aktivität noch in dem Zustand ist, erstellt ist. Ich habe keine Ahnung, warum Sie activity2 starten, bevor der Button angetippt wird. Wenn Sie möchten, können Sie die onProfileButonOKClicked() in onCreated() - Handler der Aktivität setzen. diese

+0

Mein Ziel ist es, Activity2 zu starten, wenn die Schaltfläche angetippt wird. Dies ist der Grund, warum ich onProfileButtonOkClicked() hinzugefügt habe. weil ich denke, es ist etwas wie OnClickListener – Victoria

Verwandte Themen