我已经使用了actionbar,它工作得很好。我的操作栏中有四个 fragment 。我想在其中一个 fragment 中实现 tabhost。我使用了以下代码。
package com.main.udebate;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class Info extends Fragment {
public Info() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
private TabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent i = new Intent(Info.this, about.class);
View view = inflater.inflate(R.layout.info, container, false);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
tab.setIndicator("my tab content");
tab.setContent(i);
mTabHost.addTab(tab);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
return view;
}
}
我在 Intent i = new Intent(Info.this, about.class);
行上收到错误,因为构造函数 Intent(Info, Class) 未定义。
关于.java
package com.main.udebate;
import android.app.Activity;
import android.os.Bundle;
public class about extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
有人可以帮我在 fragment 中设置 tabhost 吗?
谢谢
最佳答案
尝试使用这个
Intent i = new Intent(getActivity(), about.class);
而不是这一行
Intent i = new Intent(Info.this, about.class);
由于 Info.this 并不意味着任何上下文,这就是为什么您会收到“构造函数 Intent(Info, Class) 未定义”的原因。这个错误。
在使用 this
引用的 fragment 中,您应该使用 getActivity()
引用
关于java - 使用 fragment 在操作栏内的 tabhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17311788/