转换为片段后接收类强制转换异常

时间:2014-05-24 10:17:55

标签: android android-fragments

我需要在我的Actionbar中添加一个滚动选项卡,因此我需要将我的活动转换为片段。 但是,执行此操作后,我的应用程序将返回以下错误日志:

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" />

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

2 个答案:

答案 0 :(得分:0)

替换代码,

V.getContext()

通过

getActivity()

答案 1 :(得分:0)

看起来您仍然在应用的Android清单中将info.androidhive.jsonparsen.StarActivity声明为活动 - 具有LAUNCHER类别。请删除它。