Android的回调片段失败,失败不能转换为android.content.Context [英] android callback fails in fragment fails cannot be cast to android.content.Context

查看:977
本文介绍了Android的回调片段失败,失败不能转换为android.content.Context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回调类gaGPS需要,我认为这是一个正常的活动正常工作的上下文,但是当我在一个片段使用失败

这是我的接口类

 公共类gaGPS扩展服务实现LocationListener的{私人最终上下文mContext;//声明一个位置管理
受保护的LocationManager的LocationManager;私人gaGPSinterface gpsu;公共接口gaGPSinterface {
    公共无效locationChanged(位置newloc);
}
公共gaGPS(上下文C,gaGPSinterface gpsiface){
    mCallback = gpsiface;
    // mContext =(上下文)gpsiface;
    mContext = C;
    的LocationManager =(的LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
}

剪断

我使用这个FragmentActivity类启动片段

 公共类gaDetailsActivity扩展FragmentActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.gpsfragment);}

这里是gpsfragment.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:gpsfragment上下文=>
<片段
    机器人:ID =@ + ID / gpsTestFragLayout
    机器人:名字=id.wilsononline.gcadroid.gpsfragment
    机器人:layout_width =0dp
    机器人:layout_height =match_parent
    机器人:layout_weight =1/>

以下是我使用的片段

 公共类gpsfragment扩展片段实现gaGPS.gaGPSinterface {私人gaGPS GPS;@覆盖
公共无效locationChanged(位置newloc){
    // TODO自动生成方法存根}@覆盖
公共无效onAttach(活动活动){
    // TODO自动生成方法存根
    super.onAttach(活动);
}
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    // TODO自动生成方法存根
    查看rootView = inflater.inflate(R.layout.gpsdetails,集装箱,FALSE);
    ((的TextView)rootView.findViewById(R.id.gpsText1))的setText(测试片段);    上下文C = getActivity()getApplicationContext()。    GPS =新gaGPS(C,(gaGPSinterface)getActivity());    返回rootView;
}

}

我得到的错误是:
java.lang.ClassCastException:com.test.app.gpsfragment不能转换为android.content.Context

更多更全面的输出:

  E / AndroidRuntime(3118):android.view.InflateException:通过引起二进制XML文件行#8:错误充气类片段
E / AndroidRuntime(3118):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E / AndroidRuntime(3118):在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E / AndroidRuntime(3118):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E / AndroidRuntime(3118):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E / AndroidRuntime(3118):在android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E / AndroidRuntime(3118):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
E / AndroidRuntime(3118):在android.com.test.app.Activity.setContentView(Activity.java:1867)
E / AndroidRuntime(3118):在id.wilsononline.gcadroid.gaDetailsActivity.onCreate(gaDetailsActivity.java:12)
E / AndroidRuntime(3118):在android.com.test.app.Activity.performCreate(Activity.java:5008)
E / AndroidRuntime(3118):在android.com.test.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E / AndroidRuntime(3118):在android.com.test.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E / AndroidRuntime(3118):11 ...更多
E / AndroidRuntime(3118):java.lang.ClassCastException:致com.test.com.test.app.gaDetailsActivity不能转换为id.wilsononli
ne.gcadroid.gaGPS $ gaGPSinterface
E / AndroidRuntime(3118):在com.test.com.test.app.gpsfragment.onCreateView(gpsfragment.java:49)
E / AndroidRuntime(3118):在android.support.v4.com.test.app.Fragment.performCreateView(Fragment.java:1460)
E / AndroidRuntime(3118):在android.support.v4.com.test.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
E / AndroidRuntime(3118):在android.support.v4.com.test.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
E / AndroidRuntime(3118):在android.support.v4.com.test.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
E / AndroidRuntime(3118):在android.support.v4.com.test.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
E / AndroidRuntime(3118):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
E / AndroidRuntime(3118):21 ...更多


解决方案

 公共gaGPS(gaGPSinterface gpsu){
    this.mContext =(上下文)gpsu;
    this.gpsu = gpsu;
    // ...
}

在这里,你想投 gpsu 上下文。它不是一;这是一个片段。你应该通过上下文 gaGPSinterface 分开:

 公共gaGPS(上下文C,gaGPSinterface gpsu){
    this.mContext = C;
    this.gpsu = gpsu;
    // ...
}

然后,使用称之为 .getActivity()(返回一个活动的一个子类<$ C从$ C>上下文)的片段

  GPS =新gaGPS(getActivity(),这一点);

I have a callback class gaGPS which requires I think is a context which works fine in a normal activity but when I use in a Fragment it fails

here is my interface class

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);
}

snip

I'm launching the Fragment using this FragmentActivity class

public class gaDetailsActivity extends FragmentActivity {

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

}

here is the 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" />

the following is the Fragment I'm using

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;
}

}

errors I'm getting are : java.lang.ClassCastException: com.test.app.gpsfragment cannot be cast to android.content.Context

more fuller output:

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

解决方案

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

Here, you're trying to cast gpsu to a Context. It's is not one; it's a Fragment. You should pass the Context and gaGPSinterface separately:

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

Then, call it using .getActivity() (which returns an Activity, a subclass of Context) from the Fragment:

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

这篇关于Android的回调片段失败,失败不能转换为android.content.Context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆