1

I have been at this for a little while and after several abortive attempts, I am very close to achieving what I am after. I have mined various answers from this site, the most helpful being :-

Passing data between a fragment and its container activity

I have implemented all of Harlo's code snippets into my program but I'm left with one problem.

When I run the app, I tap on the list item to load the "Core" fragment and I get the dreaded "Unfortunately application has stopped" as it crashes.

My fragment code is below and I have highlighted the offending line, I have also included the relevant section of the logcat.

Any help would be greatly appreciated.

Thank You

Gary

import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View.OnClickListener;


public class CoreFragment extends Fragment{

int index;
Button Button1,Button2,Button3;
String Str;
OnDataPass dataPasser;

@Override
public void onAttach(Activity a) {
    super.onAttach(a);
        dataPasser = (OnDataPass) a;
}


public static CoreFragment newInstance(int index) {
    CoreFragment coreFragment = new CoreFragment();
    coreFragment.index = index;
    return coreFragment;
}


public interface OnDataPass {
    public void onDataPass(String data);
}

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

    if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist. The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed. Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }
    RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.corefragment,
            container, false);

    Button Button1 = (Button) mRelativeLayout.findViewById(R.id.Button1);
    Button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Str = "I ";
        passData(Str);
            }

       public void passData(String data) {
            data = Str;
            dataPasser.onDataPass(data);
        }

    });
    return mRelativeLayout;
}
}//end

LogCat

01-18 12:19:18.430: W/dalvikvm(1601): threadid=1: thread exiting with uncaught exception (group=0x40acf228)
01-18 12:19:18.440: E/AndroidRuntime(1601): FATAL EXCEPTION: main
01-18 12:19:18.440: E/AndroidRuntime(1601): java.lang.ClassCastException: com.epsilonitsystems.fragger.MainActivity cannot be cast to com.epsilonitsystems.fragger.CoreFragment$OnDataPass
01-18 12:19:18.440: E/AndroidRuntime(1601):     at com.epsilonitsystems.fragger.CoreFragment.onAttach(CoreFragment.java:25)

Hi TNR, Thank you for that,as I said new to fragments and they are proving a real pain!! I have added the line you suggest, and I now get a crash! The LogCat is below. Thanks for the help so far, I'll be glad to get this one sorted!!

01-23 15:30:40.659: W/dalvikvm(3098): threadid=1: thread exiting with uncaught exception (group=0x40acf228)
01-23 15:30:40.749: E/AndroidRuntime(3098): FATAL EXCEPTION: main
01-23 15:30:40.749: E/AndroidRuntime(3098): java.lang.NullPointerException
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.epsilonitsystems.fragger.MainActivity.onDataPass(MainActivity.java:33)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.epsilonitsystems.fragger.CoreFragment$1.passData(CoreFragment.java:69)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.epsilonitsystems.fragger.CoreFragment$1.onClick(CoreFragment.java:62)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.view.View.performClick(View.java:3549)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.view.View$PerformClick.run(View.java:14393)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.os.Handler.handleCallback(Handler.java:605)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.os.Looper.loop(Looper.java:154)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.app.ActivityThread.main(ActivityThread.java:4945)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at dalvik.system.NativeStart.main(Native Method)

Thanks again.

Community
  • 1
  • 1
Gary
  • 69
  • 1
  • 9

2 Answers2

1

You are trying to TypeCast an Activity to an Interface. So You are getting ClassCastException. Check whether your MainActivity is implementing onDataPass interface or not. If it implements onDataPass Interface then your problem is solved. Hope it works.

Updated Answer:

change your onClick() as below. you have written passData() but never called it to pass the Data to onDataPass() This will 100% solve your issue for sure.

@Override
public void onClick(View v) {
 Str = "I ";
 passData(Str);
}
TNR
  • 5,839
  • 3
  • 33
  • 62
  • Thank you Nagaraj436, that cured the crashing. The only problem I have now is that the onDataPass method never seems to get triggered!! I thought I had it in the right place, i.e. within the onClickListener but eclipse has the wiggly yellow line underneath it and says "The method passData(String) from the type new View.OnClickListener(){} is never used locally". Sorry to seem a complete idiot, but how do I get round this please. This is driving me mad!! – Gary Jan 18 '13 at 14:54
  • Sorry to un-accept the answer, but I was hoping for a wee bit more help with the last bit. I have a feeling that as I accepted the answer from Nagaraj436, people stopped looking at this. Can anyone help me with the onDataPass method not being fired please? Thank you all in advance!! – Gary Jan 21 '13 at 13:37
  • No response so set back to answered. Still stuck tho so any help greatly appreciated. – Gary Jan 23 '13 at 14:49
  • @Gary Check my updated answer and It will work for sure. if it work then upvote the answer. – TNR Jan 23 '13 at 15:14
  • It has just occurred to me. The CoreFragment is called from another fragment,(although this calling fragment is permanent and CoreFragment is in a frame layout as it will be one of many possible Fragments to be used), does this make a difference to how I need to go about this? – Gary Jan 23 '13 at 16:06
  • Hi TNR, thank you for the the solution. It works now. I had a stupid schoolboy level error in my code in my MainActivity!! I have a new problem now but I will post that one separately!! – Gary Jan 24 '13 at 15:20
1

I am not sure why you are making this so complicated. Why don't you just do this:

public void onClick(View v) {
    ((OnDataPass) CoreFragment.this.getActivity()).passData("I");
}

Obviously, of course, the Activity has to be:

public class ActivityThatIncludesCoreFragment implements OnDataPass {    
    ....
}
Patrick
  • 1,717
  • 7
  • 21
  • 28
G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
  • My MainActivity implements OnDataPass, (one question though, I had to import it from the fragment,am I doing something wrong?). When I add your line to the OnClick handler I get the following error message:- The method passData(String) is undefined for the type CoreFragment.OnDataPass. This a sending me round the bend!! Thank you to everyone who has tried to help me fix this. It is very much appreciated!!! – Gary Jan 23 '13 at 15:52
  • It has just occurred to me. The CoreFragment is called from another fragment,(although this calling fragment is permanent and CoreFragment is in a frame layout as it will be one of many possible Fragments to be used), does this make a difference to how I need to go about this? – Gary Jan 23 '13 at 16:08
  • Thank you for all your help, I finally have it working using TNR's solution. The reason it didn't work is I had a stupid, schoolboy level error in my MainActivity(Hangts head in shame)!! – Gary Jan 24 '13 at 15:18