无法解析构造函数 Intent
Cannot resolve constructor Intent
我正在尝试向我的应用程序添加一个 tabhost,但我无法理解为什么会显示此错误 'cannot resolve constructor intent',这是我的 activity 代码:
public class SixthFragment extends Fragment {
public static final String TAG = "sixth";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sixthfragment, container, false);
TabHost tabHost = (TabHost) view.findViewById(R.id.tabHost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,MainActivity.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,Tab2Activity.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,Tab3Activity.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
感谢您的宝贵时间。
您尝试使用的 Intent
构造函数的第一个参数采用 Context
。 Fragment
不是 Context
。使用 getActivity()
而不是 this
.
我正在尝试向我的应用程序添加一个 tabhost,但我无法理解为什么会显示此错误 'cannot resolve constructor intent',这是我的 activity 代码:
public class SixthFragment extends Fragment { public static final String TAG = "sixth"; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.sixthfragment, container, false); TabHost tabHost = (TabHost) view.findViewById(R.id.tabHost); TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab"); TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab"); TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab"); // Set the Tab name and Activity // that will be opened when particular Tab will be selected tab1.setIndicator("Tab1"); tab1.setContent(new Intent(this,MainActivity.class)); tab2.setIndicator("Tab2"); tab2.setContent(new Intent(this,Tab2Activity.class)); tab3.setIndicator("Tab3"); tab3.setContent(new Intent(this,Tab3Activity.class)); /** Add the tabs to the TabHost to display. */ tabHost.addTab(tab1); tabHost.addTab(tab2); tabHost.addTab(tab3); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } }
感谢您的宝贵时间。
您尝试使用的 Intent
构造函数的第一个参数采用 Context
。 Fragment
不是 Context
。使用 getActivity()
而不是 this
.