在android操作栏选项卡和选项卡侦听器中使用片段时出现ClassCastException

时间:2015-05-20 12:23:48

标签: android android-fragments android-actionbar classcastexception

我在Android Studio中编译源代码时遇到ClassCastException。我无法理解是什么导致了他们。我希望你能帮助我解决它。这是我的课程,但是甚至开始它我想用模拟器和API 16测试基础知识(如果你想要我的话,我可以使用任何可用的API测试它,但是无法让它工作。) / p>

MainActivity

import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {

    private android.support.v7.app.ActionBar.Tab tabOne;
    private android.support.v7.app.ActionBar.Tab tabTwo;
    private android.support.v7.app.ActionBar.Tab tabThree;
    private Fragment fragmentOne = new FragmentOne();
    private Fragment fragmentTwo = new FragmentTwo();
    private Fragment fragmentThree = new FragmentThree();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        tabOne = actionBar.newTab().setText("fragmentOne").setTabListener( new TabListener<FragmentOne>(this, "fragmentOne", FragmentOne.class));
        tabTwo = actionBar.newTab().setText("fragmentTwo").setTabListener( new TabListener<FragmentTwo>(this, "fragmentOTwo", FragmentTwo.class));
        tabThree = actionBar.newTab().setText("fragmentThree").setTabListener( new TabListener<FragmentThree>(this, "fragmentThree", FragmentThree.class));

        actionBar.addTab(tabOne);
        actionBar.addTab(tabTwo);
        actionBar.addTab(tabThree);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

和我的TabListener

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;


public  class TabListener<T extends Fragment> implements android.support.v7.app.ActionBar.TabListener {
    private android.support.v4.app.Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
     * @param activity  The host Activity, used to instantiate the fragment
     * @param tag  The identifier tag for the fragment
     * @param clz  The fragment's Class, used to instantiate the fragment
     */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    @Override
    public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
        if (mFragment == null){
            mFragment = android.support.v4.app.Fragment.instantiate(mActivity, mClass.getName());
            fragmentTransaction.add(android.R.id.content, mFragment, mTag);
        }
        else{
            fragmentTransaction.attach(mFragment);
        }
    }

    @Override
    public void onTabUnselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
        if(mFragment != null){
            fragmentTransaction.detach(mFragment);
        }
    }

    @Override
    public void onTabReselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {

    }
}

片段类目前完全相同,因为我只想查看xml内容是否正确显示

public class FragmentThree extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.three_layout, container, false);
        return rootView;
    }
}

堆栈追踪:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.patryk.android4/com.example.patryk.android4.MainActivity}: java.lang.ClassCastException: com.example.patryk.android4.FragmentOne cannot be cast to android.support.v4.app.Fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: com.example.patryk.android4.FragmentOne cannot be cast to android.support.v4.app.Fragment
        at android.support.v4.app.Fragment.instantiate(Fragment.java:420)
        at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
        at com.example.patryk.android4.TabListener.onTabSelected(TabListener.java:30)
        at android.support.v7.internal.app.WindowDecorActionBar.selectTab(WindowDecorActionBar.java:634)
        at android.support.v7.internal.app.WindowDecorActionBar.addTab(WindowDecorActionBar.java:563)
        at android.support.v7.internal.app.WindowDecorActionBar.addTab(WindowDecorActionBar.java:549)
        at com.example.patryk.android4.MainActivity.onCreate(MainActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 在android.app.ActivityThread.access $ 600(ActivityThread.java:130)

MainActivity.java:34就是这一行

 actionBar.addTab(tabOne);

(TabListener.java:30)就是这个

        mFragment = android.support.v4.app.Fragment.instantiate(mActivity, mClass.getName());

2 个答案:

答案 0 :(得分:3)

首先:public class MainActivity extends AppCompatActivity

第二

private android.support.v4.app.Fragment fragmentOne = new FragmentOne();
private android.support.v4.app.Fragment fragmentTwo = new FragmentTwo();
private android.support.v4.app.Fragment fragmentThree = new FragmentThree();

第三

public class FragmentThree extends android.support.v4.app.Fragment
public class FragmentOne extends android.support.v4.app.Fragment
public class FragmentTwo extends android.support.v4.app.Fragment

答案 1 :(得分:2)

你没有发布堆栈跟踪,但我已经看到了类中的一些不匹配。您可以使用支持库中的一些:android.support.v4.app.FragmentTransaction,其中一些来自主集:import android.app.FragmentTransaction;

请检查此方面,并确保使用相同的课程。