1

I am using the navigation-drawer template in eclipse to do a simple Android application. I have some trouble with fragment. I declared a fragment called PresenceLog Fragment in manifest but when I called it in MainActivity, the log still says that

03-23 13:54:50.817: E/AndroidRuntime(16750): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.singtel.ricecooker/com.singtel.ricecooker.PresenceLogFragment}; have you declared this activity in your AndroidManifest.xml?

Here is my manifest

Here is my fragment class

public class PresenceLogFragment extends Fragment{
private TextView myText = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.presence_log, null);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    ArrayList<String> userList = null;
    RiceServerRequest newRequest = new RiceServerRequest();
    //newRequest.getRequestInfo(this);

}

public void updateUserList(ArrayList<String> userList){
    LinearLayout lView = (LinearLayout) getView().findViewById (R.layout.presence_log);
    //LinearLayout ll = (LinearLayout)fragment.getView().findViewById(R.id.commentFragmentLayout);

    for (int i = 0; i < userList.size();i++){
        myText = new TextView(getActivity());
        myText.setText(userList.get(i));
        lView.addView(myText);
    }
    //setContentView(lView);
}

Here is my MainActivity

private void launchPresenceLog(){
    Intent intent = new Intent(this,PresenceLogFragment.class);
    startActivity(intent);
}

It would be great if you know what is wrong with my code. Also, since I am new to Android programming, I would appreciate it if you could suggest some online courses.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Haribo
  • 41
  • 1
  • 3
  • you haven't added added Activity in your manifest file – MrDumb Mar 23 '15 at 12:33
  • Course: https://www.udacity.com/course/ud853 – An SO User Mar 23 '15 at 12:36
  • @ashutiwari4 He is trying to start a FRAGMENT using an INTENT as an ACTIVITY. This is completely wrong, refer to https://developer.android.com/training/basics/fragments/creating.html – EpicPandaForce Mar 23 '15 at 12:53
  • @LittleChild thanks for the recommendation, appreciate it – Haribo Mar 23 '15 at 14:35
  • @EpicPandaForce Yup, I just realized that. I want to change a previously written Activity into Fragment and forgot to change this part. Also, do you know what is the equivalent of setContentView for Fragment (I want to update the page as I request information from server)? – Haribo Mar 23 '15 at 14:43
  • Activity `setContentView(R.layout.something)` becomes Fragment `public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) { View view = inflater.inflate(R.layout.something, container, false); return view; }` – EpicPandaForce Mar 23 '15 at 14:45

8 Answers8

2

You have created a Fragment so you could not call it like a Activity. You need to replace a container view, properly an FrameLayout with your Fragment.

getSupportFragmentManager()
  .beginTransaction()
  .replace(R.id.content_frame, new PresenceLogFragment())
  .commit();
HeW
  • 448
  • 2
  • 12
1

You can't load a fragment through Intent. You have to do it using fragment manager in this way:

Fragment fragment = new PresenceLogFragment(MainActivity.this);
FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction ft = fragmentManager.beginTransaction();                
            ft.replace(R.id.yourFragmentContainer, fragment).commit();
Prashant Patel
  • 1,087
  • 11
  • 18
0
 have you declared this activity in your AndroidManifest.xml?  

Look in your manifest and see if you have an <activity> element that has your activity registered. If not, add one.

Have a look here: http://developer.android.com/guide/topics/manifest/activity-element.html

An SO User
  • 24,612
  • 35
  • 133
  • 221
0

it is clear.

"have you declared this activity in your AndroidManifest.xml?"

you should check is there a tag。

see this or maybe this

Community
  • 1
  • 1
Evan
  • 266
  • 4
  • 5
0

Please open the manifest file and declare like this:

<activity
        android:name=".MainActivity" //your activity name
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

if this is your lauch activity then do this otherwise do this

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

type the name of your java file which extends Activity not the Fargment. Means the Fragment which creates from that Activity java file.

Avishek Das
  • 661
  • 1
  • 6
  • 20
0
 <activity
        android:name="com.singtel.ricecooker.PresenceLogFragment"
        android:label="@string/app_name" >
    </activity>

add this in your manifest file if com.singtel.ricecooker.PresenceLogFragment represents an activity and if it is a fragment then you are doing it wrong. in second case use below code,

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new PresenceLogFragment())
.commit();
Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
0

You are trying to use a fragment as an activity. You can either rename PresenceLogFragment to PresenceLogActivity and have it extend Activity instead of Fragment or you can try and use your fragment as a fragment.

Also, any activity you try to use in your app needs to be declared in the manifest (link)

More about fragments and how to use them here

Community
  • 1
  • 1
Andrei Tudor Diaconu
  • 2,157
  • 21
  • 26
-1
Navigation.findNavController(view).navigate(R.id.youAc)
ata
  • 1,254
  • 1
  • 9
  • 30