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)
不确定要改变什么到这里。我的统计页面代码是:
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);
}
}
统计页面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是我的类,它为Android导航抽屉启动xml:
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();
}
}
答案 0 :(得分:0)
您的StatisticPage
类是Activity而非Fragment,因此您无法将其与SupportFragmentManager一起使用。这就是你有ClassCastException的原因。
了解如何在http://developer.android.com/guide/components/fragments.html
中使用片段