在TabHost项目中从MainActivity打开Tab Fragment时出错

时间:2014-05-27 13:37:20

标签: android android-fragments classcastexception fragment-tab-host

我从ActionBar转移到TabHost类型的项目,但现在无法打开Tab1.java。

错误 - 由以下引起:java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.tabhost / com.example.tabhost.Tab1}:java.lang.ClassCastException:com.example.tabhost.Tab1

MainActivity.java

...

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

...

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

        ...

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Reusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Tab1.class);
        spec = tabHost.newTabSpec("home")
                .setIndicator("HOME", res.getDrawable(R.drawable.ic_tab1_1))
                .setContent(intent);
        tabHost.addTab(spec);   << ERROR <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

...

        // set tab which one you want open first time 0 or 1 or 2
        //tabHost.setCurrentTab(0); 

...

Tab1.java

...

public class Tab1 extends Fragment {
ListView list;
LazyAdapter adapter;

ArrayList<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    Document doc = MainActivity.parser.getDomElement(MainActivity.xml);
    NodeList nl = doc.getElementsByTagName("tab1");

...
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tab1, container, false);

    list = (ListView) rootView.findViewById(R.id.list);

    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            @SuppressWarnings("unchecked")
            String sss = ((Map<String, String>) adapter.getItem(position))
                    .get(MainActivity.KEY_LINK);
            Log.d("myLogs", sss);

            Uri address = Uri.parse(sss);
            Intent openlink = new Intent(Intent.ACTION_VIEW, address);
            startActivity(openlink);
        }
    });

    return rootView;
}

}

请帮助。 感谢任何人。

1 个答案:

答案 0 :(得分:0)

您正在作为活动进行扩展,然后在使用Fragment的tab1中进行扩展..

如果您想在意图中使用此代码

public class AndroidTabLayoutActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();

        // Tab for Photos
        TabSpec photospec = tabHost.newTabSpec("Photos");
        // setting Title and Icon for the Tab
        photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent photosIntent = new Intent(this, PhotosActivity.class);
        photospec.setContent(photosIntent);

        // Tab for Songs
        TabSpec songspec = tabHost.newTabSpec("Songs");        
        songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
        Intent songsIntent = new Intent(this, SongsActivity.class);
        songspec.setContent(songsIntent);

        // Tab for Videos
        TabSpec videospec = tabHost.newTabSpec("Videos");
        videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
        Intent videosIntent = new Intent(this, VideosActivity.class);
        videospec.setContent(videosIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(photospec); // Adding photos tab
        tabHost.addTab(songspec); // Adding songs tab
        tabHost.addTab(videospec); // Adding videos tab
    }
}

»PhotosActivity.java

package com.example.androidtablayout;

import android.app.Activity;
import android.os.Bundle;

public class PhotosActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photos_layout);
    }
}

»SongsActivity.java

package com.example.androidtablayout;

import android.app.Activity;
import android.os.Bundle;

public class SongsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.songs_layout);
    }
}

»VideosActivity.java

package com.example.androidtablayout;

import android.app.Activity;
import android.os.Bundle;

public class VideosActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videos_layout);
    }
}

点击此链接clickhere

For Fragment试试这个链接 Fragment使用view pager

关注此代码

MainActivity.java

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Top Rated", "Games", "Movies" };

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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }