2013-01-23 79 views
0

我有一个回调类gaGPS这就要求我觉得是在一个正常的活动能正常工作的上下文,但是当我在一个片段用它失败Android的回调片段失败,失败,无法转换为android.content.Context

这里是我的接口类

Public class gaGPS extends Service implements LocationListener { 

private final Context mContext; 

// Declaring a Location Manager 
protected LocationManager locationManager; 

private gaGPSinterface gpsu; 

public interface gaGPSinterface { 
    public void locationChanged(Location newloc); 
} 


public gaGPS(Context c, gaGPSinterface gpsiface) { 
    mCallback = gpsiface; 
    //mContext = (Context) gpsiface; 
    mContext = c; 


    locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 
} 

剪断

我使用这个FragmentActivity类

public class gaDetailsActivity extends FragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gpsfragment); 

} 
启动片段

这里是gpsfragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<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" 
tools:context=".gpsfragment" > 
<fragment 
    android:id="@+id/gpsTestFragLayout" 
    android:name="id.wilsononline.gcadroid.gpsfragment" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 

下面是我使用

public class gpsfragment extends Fragment implements gaGPS.gaGPSinterface { 

private gaGPS gps; 

@Override 
public void locationChanged(Location newloc) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onAttach(Activity activity) { 
    // TODO Auto-generated method stub 
    super.onAttach(activity); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    View rootView = inflater.inflate(R.layout.gpsdetails, container, false); 
    ((TextView) rootView.findViewById(R.id.gpsText1)).setText("testing fragment"); 

    Context c = getActivity().getApplicationContext(); 

    gps = new gaGPS(c, (gaGPSinterface) getActivity()); 

    return rootView; 
} 

}

错误我得到的片段是: 的java .lang.ClassCastException:com.test.app.gpsfragment无法转换为android.content.Context

更多更全面的输出:

E/AndroidRuntime(3118): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment 
E/AndroidRuntime(3118):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
E/AndroidRuntime(3118):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
E/AndroidRuntime(3118):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
E/AndroidRuntime(3118):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
E/AndroidRuntime(3118):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
E/AndroidRuntime(3118):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
E/AndroidRuntime(3118):  at android.com.test.app.Activity.setContentView(Activity.java:1867) 
E/AndroidRuntime(3118):  at id.wilsononline.gcadroid.gaDetailsActivity.onCreate(gaDetailsActivity.java:12) 
E/AndroidRuntime(3118):  at android.com.test.app.Activity.performCreate(Activity.java:5008) 
E/AndroidRuntime(3118):  at android.com.test.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
E/AndroidRuntime(3118):  at android.com.test.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
E/AndroidRuntime(3118):  ... 11 more 
E/AndroidRuntime(3118): Caused by: java.lang.ClassCastException: com.test.com.test.app.gaDetailsActivity cannot be cast to id.wilsononli 
ne.gcadroid.gaGPS$gaGPSinterface 
E/AndroidRuntime(3118):  at com.test.com.test.app.gpsfragment.onCreateView(gpsfragment.java:49) 
E/AndroidRuntime(3118):  at android.support.v4.com.test.app.Fragment.performCreateView(Fragment.java:1460) 
E/AndroidRuntime(3118):  at android.support.v4.com.test.app.FragmentManagerImpl.moveToState(FragmentManager.java:884) 
E/AndroidRuntime(3118):  at android.support.v4.com.test.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066) 
E/AndroidRuntime(3118):  at android.support.v4.com.test.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168) 
E/AndroidRuntime(3118):  at android.support.v4.com.test.app.FragmentActivity.onCreateView(FragmentActivity.java:280) 
E/AndroidRuntime(3118):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 
E/AndroidRuntime(3118):  ... 21 more 
+0

我们可以看到/ RES /布局/gpsfragment.xml? –

+0

添加gpsfragment – mtraxdroid

回答

1
public gaGPS(gaGPSinterface gpsu) { 
    this.mContext = (Context) gpsu; 
    this.gpsu = gpsu; 
    // ... 
} 

在这里,你想投gpsuContext。这不是一个;这是一个Fragment。你应该通过ContextgaGPSinterface分开:

public gaGPS(Context c, gaGPSinterface gpsu) { 
    this.mContext = c; 
    this.gpsu = gpsu; 
    // ... 
} 

然后,使用.getActivity()把它从Fragment(它返回一个ActivityContext一个子类):

gps = new gaGPS(getActivity(), this); 
+0

好吧我会试一试,但我的第一个直觉是,这将失败,因为我得到了片段中的转换错误,而不是接口类 – mtraxdroid

+0

不能:“'at com.test.app.gaGPS (gaGPS.java:55)'“(来自您的堆栈跟踪)。 'Fragment'不能膨胀,因为这个异常(作为执行的一部分抛出)存在。 – Eric

+0

好的,谢谢,这些堆栈痕迹有时候很难读取。 – mtraxdroid