我正在创建一个TabHost,它是一个片段中的Tab视图。子片段将连接到名为fragment_usage
的主片段。运行后,它不断给我这个错误:
您需要指定一种创建标签指示符的方法。
MAIN FRAGMENT(FRAGMENT_USAGE)
package com.example.redir.wealthtrack;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TabHost;
import com.example.redir.wealthtrack.Tabs.Tag1;
/**
* A simple {@link Fragment} subclass.
*/
public class UsageFragment extends Fragment {
public UsageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_usage, container, false);
TabHost tab = (TabHost) view.findViewById(R.id.tabHost);
tab.setup();
TabHost.TabSpec spec1 = tab.newTabSpec("Tab 1");
spec1.setIndicator("Tab 1");
spec1.setContent(R.id.Tab1);
tab.addTab(spec1);
TabHost.TabSpec spec2 = tab.newTabSpec("Tab 2");
spec1.setIndicator("Tab 2");
spec1.setContent(R.id.Tab2);
tab.addTab(spec2);
TabHost.TabSpec spec3 = tab.newTabSpec("Tab 3");
spec1.setIndicator("Tab 3");
spec1.setContent(R.id.Tab3);
tab.addTab(spec3);
TabHost.TabSpec spec4 = tab.newTabSpec("Tab 4");
spec1.setIndicator("Tab 4");
spec1.setContent(R.id.Tab4);
tab.addTab(spec4);
return view;
}
}
[enter image description here][1]
[1]: http://i.stack.imgur.com/hLyii.png
答案 0 :(得分:1)
更改 spec1-> SPEC2,SPEC3,spec4 强>;复制时要小心:)
TabHost.TabSpec spec1 = tab.newTabSpec("Tab 1");
spec1.setIndicator("Tab 1");
spec1.setContent(R.id.Tab1);
tab.addTab(spec1);
TabHost.TabSpec spec2 = tab.newTabSpec("Tab 2");
spec2 .setIndicator("Tab 2");
spec2 .setContent(R.id.Tab2);
tab.addTab(spec2);
TabHost.TabSpec spec3 = tab.newTabSpec("Tab 3");
spec3 .setIndicator("Tab 3");
spec3 .setContent(R.id.Tab3);
tab.addTab(spec3);
TabHost.TabSpec spec4 = tab.newTabSpec("Tab 4");
spec4 .setIndicator("Tab 4");
spec4 .setContent(R.id.Tab4);
tab.addTab(spec4);