Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:48:28+00:00 2026-06-17T10:48:28+00:00

I have been at this for a little while and after several abortive attempts,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:48:29+00:00Added an answer on June 17, 2026 at 10:48 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little question that's been on my mind for a while now.
I have been trying this for a little while nut just cannot get to
Thus far you guys have been wildly helpful with me getting this little ditty
i have been working on this sub-menu for 8 hours now, the last little
I have been trying to make this to be a little jQuery plugin that
This is a little different than the questions that have already been asked on
I've been struggling with this for a little while now.. I am using this
This problem has been plaguing me for a little while, but I've think I've
I've been attempting to figure this out for a little while now, and it's
I have been studying Grails for quite a while now. And scanned a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.