Question

2Can someone help me with onClick method? basically i have a array which changes every few seconds. Goal is when "1" displays and if user clicks on screen new activity displays same goes when "2" display and if user click on screen different activity class displays.

     public TextSwitcher mSwitcher, mSwitcher1, mSwitcher2;


    String textToShow[] = {
            "1", "2"
    };


 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.example_layout, container, false);

        mSwitcher = (TextSwitcher) v.findViewById(R.id.textSwitcher);

        mSwitcher.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent n = null;
                switch (){
                case 0:
                    n = new Intent(getActivity(), FragMent1.class);
                    break;
                case 1:
                    n = new Intent(getActivity(), FragMent2.class);
                    break;
                }
                if(null!=n)
                    startActivity(n);
            }

                //Intent myIntent = new Intent(getActivity(), Listtube.class);
                //getActivity().startActivity(myIntent);        


        });

errors

03-23 21:10:09.216: W/dalvikvm(1876): threadid=1: thread exiting with uncaught exception (group=0xb1b11ba8)
03-23 21:10:09.236: E/AndroidRuntime(1876): FATAL EXCEPTION: main
03-23 21:10:09.236: E/AndroidRuntime(1876): Process: com.example.actionbartab, PID: 1876
03-23 21:10:09.236: E/AndroidRuntime(1876): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.actionbartab/com.example.actionbartab.FragMent1}: java.lang.ClassCastException: com.example.actionbartab.FragMent1 cannot be cast to android.app.Activity
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.os.Looper.loop(Looper.java:136)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at java.lang.reflect.Method.invoke(Method.java:515)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at dalvik.system.NativeStart.main(Native Method)
03-23 21:10:09.236: E/AndroidRuntime(1876): Caused by: java.lang.ClassCastException: com.example.actionbartab.FragMent1 cannot be cast to android.app.Activity
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-23 21:10:09.236: E/AndroidRuntime(1876):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
03-23 21:10:09.236: E/AndroidRuntime(1876):     ... 11 more
Was it helpful?

Solution

You can get the id of the View parameter then use that in your switch. Something like

 public void onClick(View v) {
       Intent n = null;
       int id = v.getId();
       switch (id){
           case R.id.someId:
                n = new Intent(getActivity(), FragMent1.class);
                break;
           case R.id.someOtherId:
                n = new Intent(getActivity(), FragMent2.class);
                break;
        }

It sounds like this is what you are looking for. If not, please explain a little better what you want.

Edit

I'm not sure that I understand your latest problem but if I do then you can do something like

public void onClick(View v) {
       Intent n = null;
       int id = v.getId();
       switch (id){
           case R.id.someId:
           case R.id.someOtherId:
                n = new Intent(getActivity(), FragMent1.class);
                break;

OTHER TIPS

Fragment and Activity are difference.
Activity.startActivity(Intent) method call new Activity but FragMent1 is not Activity.
so you can't cast FragMent1 class to Activity class.
if FragMent1 inherit Fragment, you should use FragmentManager.

Fragment fragment = FragMent1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, fragment);
ft.commit();

R.id.content is id of one of layout.


It just is simple example code. you should write the code depending your application.

public void onClick(View v) {
    int id = v.getId();

    Fragment fragment = null;
    Bundle args = new Bundle();
    switch ( id ) {
    case R.id.fragment1:
        fragment = new FragMent1();
        args.putString("someKey1", "someValue1");
        fragment.setArguments(args);
        showFragment(fragment);
        break;
    case R.id.fragment2:
        fragment = new FragMent2();
        args.putString("someKey2", "someValue2");
        fragment.setArguments(args);
        showFragment(fragment);
        break;
    default:
        break;
    }
}

public void showFragment(Fragment fragment) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.content, fragment);
    ft.commit();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top