2014-05-24 29 views
-1

我需要添加滚动选项卡到我的操作栏,因此我需要将我的活动转换为片段。 但是这样做后,我的应用程序返回以下错误日志:转换为片段后接收类抛出异常

05-24 12:02:14.504: E/AndroidRuntime(393): FATAL EXCEPTION: main 
05-24 12:02:14.504: E/AndroidRuntime(393): Process: info.androidhive.jsonparsen, PID:  393 
05-24 12:02:14.504: E/AndroidRuntime(393): java.lang.RuntimeException: Unable to   instantiate activity  ComponentInfo{info.androidhive.jsonparsen/info.androidhive.jsonparsen.StarActivity}:  java.lang.ClassCastException: info.androidhive.jsonparsen.StarActivity cannot be cast to android.app.Activity 
05-24 12:02:14.504: E/AndroidRuntime(393): at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252) 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.app.ActivityThread.access$800(ActivityThread.java:139) 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200) 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.os.Handler.dispatchMessage(Handler.java:102) 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.os.Looper.loop(Looper.java:136) 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.app.ActivityThread.main(ActivityThread.java:5103) 
05-24 12:02:14.504: E/AndroidRuntime(393): at java.lang.reflect.Method.invokeNative(Native Method) 
05-24 12:02:14.504: E/AndroidRuntime(393): at java.lang.reflect.Method.invoke(Method.java:515) 
05-24 12:02:14.504: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
05-24 12:02:14.504: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) 
05-24 12:02:14.504: E/AndroidRuntime(393): at dalvik.system.NativeStart.main(Native Method) 
05-24 12:02:14.504: E/AndroidRuntime(393): Caused by: java.lang.ClassCastException: info.androidhive.jsonparsen.StarActivity cannot be cast to android.app.Activity 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 
05-24 12:02:14.504: E/AndroidRuntime(393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2119) 
05-24 12:02:14.504: E/AndroidRuntime(393): ... 11 more 

我找不到这个问题的任何解决方案。

,是造成这片段是我的第一个片段(在启动时应用的) 这是代码:

import android.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class StarActivity extends Fragment { 
private LinearLayout llLayout = null; 
private FragmentActivity faActivity = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    faActivity = (FragmentActivity) super.getActivity(); 
    llLayout = (LinearLayout) inflater.inflate(R.layout.staractivity, container, false); 
    Button json = (Button)getView().findViewById(R.id.jsonbut); 
    json.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View V){ 
      Intent jsonnew = new Intent(V.getContext(), MainActivity.class); 
      V.getContext().startActivity(jsonnew); 
     } 
    }); 

    Button geoloc = (Button)getView().findViewById(R.id.geolocation); 
    geoloc.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View V){ 
      Intent geonew = new Intent(V.getContext(), GeoFence.class); 
      V.getContext().startActivity(geonew); 
     } 
    }); 
    Button mapv = (Button)getView().findViewById(R.id.mapview); 
    mapv.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View V){ 
      Intent mapv = new Intent(V.getContext(), mapview.class); 
      V.getContext().startActivity(mapv); 
     } 
    }); 
    Button writedb = (Button)getView().findViewById(R.id.writedb); 
    writedb.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View V){ 
      Intent writedb = new Intent(V.getContext(), writedb.class); 
      V.getContext().startActivity(writedb); 
     } 
    }); 
    Button login = (Button)getView().findViewById(R.id.login); 
    login.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View V){ 
      Intent login = new Intent(V.getContext(), login_Login.class); 
      V.getContext().startActivity(login); 
     } 
    }); 

    Button sharedUser = (Button)getView().findViewById(R.id.sharedUser); 
    sharedUser.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View V){ 
      Intent sharedUser = new Intent(V.getContext(), sharedUser.class); 
      V.getContext().startActivity(sharedUser); 
     } 
    }); 
    return llLayout; 
} 
} 

我的XML由以下部分组成:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/viewPager" 
android:orientation="vertical" 
> 
    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:clickable="true" 
     /> 
<Button 
    android:id="@+id/jsonbut" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="60dp" 
    android:text="JSON download" /> 

<Button 
    android:id="@+id/writedb" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/jsonbut" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="23dp" 
    android:text="WRITE DB" /> 

<Button 
    android:id="@+id/mapview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/writedb" 
    android:layout_below="@+id/geolocation" 
    android:text="MAPVIEW" /> 

<Button 
    android:id="@+id/geolocation" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/mapview" 
    android:layout_below="@+id/writedb" 
    android:layout_marginTop="112dp" 
    android:text="GEOFENCE" /> 

<Button 
    android:id="@+id/login" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="Button" /> 

<Button 
    android:id="@+id/sharedUser" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/login" 
    android:layout_below="@+id/writedb" 
    android:text="SharedUser" /> 

任何人都可以给我一个解释,在哪里可以找到它,以及如何?

+0

为什么这个'faActivity =(FragmentActivity)super.getActivity();'只是使用'getActivity()'。确保活动延伸标准'活动'。 'getView()'返回null。使用充气的视图对象来初始化视图。 – Raghunandan

+0

StarActivity为什么不直接扩展FragmentActivity? – Xcihnegn

回答

0

替换代码,

V.getContext()

通过

getActivity()

+0

remove faActivity =(FragmentActivity)super.getActivity(); –

0

好像你还有info.androidhive.jsonparsen.StarActivity声明为您的应用程序的Android清单的活动 - 用LAUNCHER类别。请删除。

+0

这样做会使应用程序在运行后从手机中删除,而不是启动它 – user3671459

+0

@ user3671459那么,这是例外情况告诉你的。你正在尝试启动一个'Fragment' - 这不会飞。您需要启动一个活动,该活动将创建并放置活动中的片段。 – user3264740