查看包含片段的寻呼机

时间:2014-02-23 00:41:54

标签: java android android-fragments

我的程序有两个选项卡,每个选项卡都必须创建片段。我刚开始使用一个选项卡,程序运行时出错。可以帮助我。

Main Java。

package com.example.version1;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

public class MainActivity extends FragmentActivity {
    // Fragment TabHost as mTabHost
    private FragmentTabHost mTabHost;

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

 mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("Line1").setIndicator("Line1"),
            Tab1Fragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Line2").setIndicator("Tab2"),
            Tab2Fragment.class, null);

    }
}

Tab1.java(此类已为片段创建)

package com.example.version1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class Tab1Fragment extends FragmentActivity {

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

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) 
           {

             return Machine1.newInstance("Machine1");                 

        }

        @Override
        public int getCount() {
            return 4;
        }       
    }
    }

Machine1.java

package com.example.version1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Machine1 extends Fragment  {



     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.machine1, container, false);

          //  TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
        //    tv.setText(getArguments().getString("msg"));

            return v;
        }

        public static Machine1 newInstance(String text) {

            Machine1 f = new Machine1();
            Bundle b = new Bundle();
            b.putString("msg", text);

            f.setArguments(b);

            return f;
        }
    }

我得到的错误是:

02-22 18:53:22.100: D/AndroidRuntime(2394): Shutting down VM
02-22 18:53:22.120: W/dalvikvm(2394): threadid=1: thread exiting with uncaught exception (group=0x41465700)
02-22 18:53:22.230: E/AndroidRuntime(2394): FATAL EXCEPTION: main
02-22 18:53:22.230: E/AndroidRuntime(2394): java.lang.ClassCastException: com.example.version1.Tab1Fragment cannot be cast to android.support.v4.app.Fragment
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:343)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.View.dispatchAttachedToWindow(View.java:12125)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2453)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1207)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.Choreographer.doFrame(Choreographer.java:532)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.os.Handler.handleCallback(Handler.java:730)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.os.Looper.loop(Looper.java:137)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at java.lang.reflect.Method.invoke(Method.java:525)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-22 18:53:22.230: E/AndroidRuntime(2394):     at dalvik.system.NativeStart.main(Native Method)
02-22 18:53:26.620: I/Process(2394): Sending signal. PID: 2394 SIG: 9

1 个答案:

答案 0 :(得分:0)

你的问题来自这些方面:

mTabHost.addTab(mTabHost.newTabSpec("Line1").setIndicator("Line1"),
        Tab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Line2").setIndicator("Tab2"),
        Tab2Fragment.class, null);

Tab1Fragment.class和Tab1Fragment.class属于FragmentActivity类型,其中addTab方法需要一种Fragment类型。我相信你想要使用Machine1.class。