我在setContentView
,findViewById
和getSupportFragmentManager()
面临错误。这个代码在扩展AppCompatActivity
时运行正常,但是当我尝试将其更改为片段时,它会给出错误。我需要将Activity的这段代码转换为片段,以便我可以在navigationDrawer
中使用它。
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
public class PerdidosActivity extends Fragment {
FrameLayout simpleFrameLayout;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perdidos);
// get the reference of FrameLayout and TabLayout
simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
// Create a new Tab named "First"
TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setText("First"); // set the Text for the first Tab
firstTab.setIcon(R.drawable.android_icon); // set an icon for the
// first tab
tabLayout.addTab(firstTab); // add the tab at in the TabLayout
// Create a new Tab named "Second"
TabLayout.Tab secondTab = tabLayout.newTab();
secondTab.setText("Second"); // set the Text for the second Tab
secondTab.setIcon(R.drawable.android_icon); // set an icon for the second tab
tabLayout.addTab(secondTab); // add the tab in the TabLayout
// perform setOnTabSelectedListener event on TabLayout
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// get the current selected tab's position and replace the fragment accordingly
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new FirstFragment();
break;
case 1:
fragment = new SecondFragment();
break;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
的setContentView()
在活动中,您可以通过覆盖onCreate()
并调用setContentView()
来创建UI。在片段中,您可以采用不同的方式。而不是覆盖onCreate()
,覆盖onCreateView()
然后写这样的东西:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.my_layout, container, false);
...
return root;
}
findViewById()
在一个Activity中,您只需在任何地方调用findViewById()
,它就会在您发送到setContentView()
的布局中搜索视图。在片段中,你需要一个View
实例来调用findViewById()
。
在onCreateView()
内,你可以打电话给root.findViewById(R.id.my_view)
。在其他方法中,只要你已经从onCreateView()
返回了一些内容,就可以编写getView().findViewById(R.id.my_view)
。
getSupportFragmentManager()
这个很简单:只需使用getFragmentManager()
。
这将是public class PerdidosActivity extends AppCompatActivity
,你的错误将消失。解释:您正在扩展一个活动,您将要使用TabLayout,并且您将使用适配器连接到TabLayout的视图将是片段而不是PerdidosActivity。