0
01-03 18:39:41.423  10451-10451/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beerportfolio.beerportfoliopro/com.beerportfolio.beerportfoliopro.MainDraw}: java.lang.ClassCastException: com.beerportfolio.beerportfoliopro.StatisticsPage cannot be cast to android.support.v4.app.Fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: com.beerportfolio.beerportfoliopro.StatisticsPage cannot be cast to android.support.v4.app.Fragment
            at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
            at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
            at com.beerportfolio.beerportfoliopro.MainDraw.onCreate(MainDraw.java:51)
            at android.app.Activity.performCreate(Activity.java:5195)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
01-03 18:39:43.646    1045-5127/com.htc.launcher E/Prism.WidgetManager﹕ Widget ComponentInfo{com.levelup.beautifulwidgets/com.levelup.beautifulwidgets.ClockWidget52} can not fit on this device (960, 330 => 5x2), spanXY: 5, 2, minSpanXY: 5, 2
01-03 18:39:43.696    1045-5127/com.htc.launcher E/Prism.WidgetManager﹕ The same lists. No need to update. skip it.
01-03 18:39:43.826  10559-10559/eu.chainfire.supersu E/SuperSU﹕ isLaunchedBySettings: No
01-03 18:39:44.857  10593-10670/com.yahoo.mobile.client.android.flickr:feather_system_receiver E/a.a.ar﹕ Batch has been disabled: app_loads
01-03 18:39:44.957  10611-10687/com.yahoo.mobile.client.android.flickr E/a.a.ar﹕ Batch has been disabled: app_loads
01-03 18:39:46.138  10786-10793/? E/jdwp﹕ Failed writing handshake bytes: Broken pipe (-1 of 14)

Not sure what to change to what here. My statistics page code is:

import android.app.ActionBar;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by Mike on 1/3/14.
 */
public class StatisticsPage extends FragmentActivity {

    ActionBar.Tab tab;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.statistics_pagelayout);

        ActionBar actionBar = getActionBar();
        // Create Actionbar Tabs

        //launch rate dialog
        //AppRater.app_launched(this);



        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);
        String url = "http://www.beerportfolio.com/app_getStatistics.php?";
        String userURLComp = "u=" + userID;


        url = url + userURLComp ;

        Log.d("basicStats", url);


        new getBasicStats(this).execute(url);



    }



}

Statistics page xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/statisticsTitle"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Your Statistics"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <View
        android:layout_width="1dp"
        android:layout_height="30dp">
    </View>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Brewery"
            android:textStyle="bold"
            android:onClick="openBreweryStatistics"
            android:layout_weight="1"
            android:id="@+id/button"
            android:textColor="@color/orange"
            style="?android:attr/borderlessButtonStyle"
            android:layout_gravity="center_horizontal|top"
            />

        <View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dip"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="4dip"
            android:background="?android:attr/dividerVertical"
            android:layout_centerHorizontal="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Style"
            android:onClick="openStyleStatistics"
            android:layout_weight="1"
            android:id="@+id/button2"
            android:textColor="@color/orange"
            style="?android:attr/borderlessButtonStyle"
            android:layout_gravity="center_horizontal|top"/>

        <View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dip"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="4dip"
            android:background="?android:attr/dividerVertical"
            android:layout_centerHorizontal="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Taste"
            android:onClick="openTasteStatistics"
            android:layout_weight="1"
            android:id="@+id/button3"
            android:textColor="@color/orange"
            style="?android:attr/borderlessButtonStyle"
            android:layout_gravity="center_horizontal|top"/>
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="20dp">
    </View>


    <ListView
        android:id="@+id/yourStats"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:dividerHeight="0px"
        android:divider="@null"

        >
    </ListView>



</LinearLayout>

MainDraw is my class that launches the xml for the Android Navigation Drawer:

package com.beerportfolio.beerportfoliopro;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by Mike on 1/3/14.
 */
public class MainDraw extends FragmentActivity {
    final String[] data ={"Statistics","two","three"};
    final String[] fragments ={
            "com.beerportfolio.beerportfoliopro.StatisticsPage",
            "com.beerportfolio.beerportfoliopro.FragmentTwo",
            "com.beerportfolio.beerportfoliopro.FragmentThree"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        //todo: load statistics fragment

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

        final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        final ListView navList = (ListView) findViewById(R.id.drawer);
        navList.setAdapter(adapter);
        navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                    @Override
                    public void onDrawerClosed(View drawerView){
                        super.onDrawerClosed(drawerView);
                        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                        tx.replace(R.id.main, Fragment.instantiate(MainDraw.this, fragments[pos]));
                        tx.commit();
                    }
                });
                drawer.closeDrawer(navList);
            }
        });
        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.main,Fragment.instantiate(MainDraw.this, fragments[0]));
        tx.commit();
    }

}
Mike
  • 6,751
  • 23
  • 75
  • 132

1 Answers1

0

Your StatisticPage class is an Activity not Fragment so you can't use it with SupportFragmentManager. That's why you have ClassCastException.

Learn how to use fragments in: http://developer.android.com/guide/components/fragments.html

Hellboy
  • 1,052
  • 6
  • 12
  • FragmentACtivity doesn't work? I am trying to convert my app from fragments to activities. – Mike Jan 04 '14 at 00:17
  • FragmentActivity is a version of activity from support library that allows to add fragments into it for Android 2.3 and older. It is not a fragment and it cannot be used as a fragment. If you want to put fragments into MainDraw Activity, the only thing you need to do is to change StatisticPage(and FragmentTwo and FragmentThree) into fragment. – Hellboy Jan 04 '14 at 00:24
  • I figured it out I think, your link helped me a bit. I wasn't setting the xml right, I had to inflate it. – Mike Jan 04 '14 at 00:27
  • Now I have an issue with my drawer, when I close the drawer with a swipe, it seems to reload the statistics fragment each time its closed. – Mike Jan 04 '14 at 00:28
  • First of all you need to understand the difference between Activities and Fragments. They are not the same and they have slightly different lifecycle. Anyway if you want to use Fragments, then you have to have activity (like FragmentActivity) in which you place Fragments (must extend Fragment or ListFragment class). Also you should set/inflate fragments layout in `onActivityCreated` method – Hellboy Jan 04 '14 at 00:32
  • In your DrawerListener you replace StatisticFragment every time the drawer is closing that's why it is reloaded every time. btw it is pointless to set your listener everytime user tap a button – Hellboy Jan 04 '14 at 00:35
  • I honestly didnt realize that was what I am doing and am a little lost in moy code I am piecing together from examples and docs. I am about to open another question on this if your interested in further explaining. – Mike Jan 04 '14 at 01:40
  • Just asked this question here if you have time for a more detailed answer: – Mike Jan 04 '14 at 01:44
  • http://stackoverflow.com/questions/20915787/android-navigation-draw-re-loading-fragment-whenever-closed – Mike Jan 04 '14 at 01:45