android_king22
android_king22

Reputation: 779

Fragment Activity cannot be cast to app.Activity.

Okay i keep getting this error when i have a button click launch another Fragment..

07-30 20:54:05.950: ERROR/AndroidRuntime(7816): Caused by: java.lang.ClassCastException: com.fttech.gameIT.shopping_details_fragment cannot be cast to android.app.Activity

07-30 20:54:05.950: ERROR/AndroidRuntime(7816): at android.app.Instrumentation.newActivity(Instrumentation.java:1022) 07-30 20:54:05.950: ERROR/AndroidRuntime(7816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1663)

Here is what i am trying to do, when the button findIt is clicked from this activity..

  public class shoppingClass extends FragmentActivity{
Button findIT;
EditText game;
String item = null;
WebView browser;
RadioGroup site;
RadioGroup type;
String url;
String console;
shopping_details_fragment shopping;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shopping);

    findIT = (Button)findViewById(R.id.findIT);
    shop = (EditText)findViewById(R.id.item);
    type = (RadioGroup)findViewById(R.id.console);
    site = (RadioGroup)findViewById(R.id.shopping_group);

    final Intent d = new Intent(this, shopping_details_fragment.class);
    findIT.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            getUserPreference();
            shopping.loadUrl(url);
            startActivity(d);


        }
    });

  }

I am launching another this Fragment into the view...

public class shopping_details_fragment extends Fragment{

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

    //Return the view for our WebView
    return(inflater.inflate(R.id.browserFrag,container, false));

}

public void loadUrl(String url){
    ((WebView)getView().findViewById(R.id.browser)).loadUrl(url);
}

}

This uses the browser i have set up in the same xml that the first activitiy is set up in to launch a webbrowser and look up a URL in a fragment i have set..

Its getting the browser in loadUrl() from a webview layout i created and inflating it into the fragment. But i keep getting the error above.

Upvotes: 1

Views: 9603

Answers (1)

Adam
Adam

Reputation: 733

Fragment and Activity aren't directly related like that, you can't just cast between them. Use the getActivity() method on the fragment instead, to return its activity.

Upvotes: 2

Related Questions