Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to start fragment activity in a textview click in activity class but I get below exception.I am new to android please help.This is my code

 Intent intent=new Intent(this,ThreeFragment.class);
    startActivity(intent);

and got below exception.

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.siluni.myapplication/com.example.siluni.myapplication.ThreeFragment}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1791)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1513)
        at android.app.Activity.startActivityForResult(Activity.java:3933)
        at android.app.Activity.startActivityForResult(Activity.java:3881)
        at android.app.Activity.startActivity(Activity.java:4204)
        at android.app.Activity.startActivity(Activity.java:4172)
        at com.example.siluni.myapplication.SheduleActivity.SaveShedule(SheduleActivity.java:178)
        at com.example.siluni.myapplication.SheduleActivity$5.onClick(SheduleActivity.java:150)
        at android.view.View.performClick(View.java:5076)
        at android.view.View$PerformClick.run(View.java:20279)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5930)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
366 views
Welcome To Ask or Share your Answers For Others

1 Answer

if ThreeFragment extends Fragment then you should initialize it like this

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new ThreeFragment();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();

UPDATE:

R.id.fragment_container is a FrameLayout that will contain your fragment.

Include this at the bottom of Activity layout in which you want to initialize the fragment

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

and if ThreeFragment extends AppCompatActivity or Activity the in your manifest you need to include it like this

<activity
        android:name=".ThreeFragment"
        android:label="@string/app_name">
</activity>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...