Question

I'm new to android app building, i'm doing this as an requirement for my college project. Please assist me.

My app has swiping tabs, using fragment. I have 5 tabs and 5 Fragments, In some tabs (fragments) i need to navigate to new fragments within the fragments; e.g. when in Tab1 i have buttons when pressed will bring user to new fragment, I had searched the internet and managed to write the code. My issue is when in Tab1 and press button to go to new fragment i'm getting " app stopped working" please assist me

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View.OnClickListener;

 public class Unsafe extends Fragment implements OnClickListener {
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.unsafe, container, false);
    Button button1 = (Button) v.findViewById(R.id.u1);
    button1.setOnClickListener(this);
    return v;
}

public void onClick(View v) {
    startActivity(new Intent(getActivity(),unsafe1.class));
}
}

UPDATE - Posting Logcat

12-03 08:59:39.538    1460-1460/com.example.hfacs D/AndroidRuntime﹕ Shutting down VM
12-03 08:59:39.538    1460-1460/com.example.hfacs W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a77b90)
12-03 08:59:39.538      379-645/system_process I/ActivityManager﹕ START u0 {cmp=com.example.hfacs/.Unsafe1} from pid 1460
12-03 08:59:39.588    1460-1460/com.example.hfacs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.hfacs, PID: 1460
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.hfacs/com.example.hfacs.Unsafe1}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
            at android.app.Activity.startActivityForResult(Activity.java:3423)
            at android.app.Activity.startActivityForResult(Activity.java:3384)
            at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:848)
            at android.support.v4.app.Fragment.startActivity(Fragment.java:878)
            at com.example.hfacs.Unsafe$1.onClick(Unsafe.java:28)
            at android.view.View.performClick(View.java:4424)
            at android.view.View$PerformClick.run(View.java:18383)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

No correct solution

OTHER TIPS

I spotted this line in the logcat:

ndroid.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.hfacs/com.example.hfacs.Unsafe1}; have you declared this activity in your AndroidManifest.xml?

That means you did not declare the activity Unsafe1 in the AndroidManifest.xml file.

I believe you should not call startActivity() in onClick. Instead, when user click on the button, notify the FragmentActivity containing this fragment. Inside the FragmentActivity, call ViewPager.setCurrentItem(position), where position is an int. The view pager will then scroll to the desired tab.

I am not sure if this is the best way to do it. But I tried this before and it works. Add a function in your FragmentActivity, which is used to scroll to the desired tab.

public static void scroll(int pos){
    mViewPager.setCurrentItem(pos); //mViewPager is the ViewPager you have in FragmentActivity
}

And this is the onClickListener

Button btn = (Button) v.findViewById(R.id.u1);
    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            //when button clicked, call the scroll function
            MainActivity.scroll(position_of_unsafe1);   //MainActivity is the FragmentActivity name         
        }           
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top