在我的主視圖應用程序中有3個選項卡。這3個標籤填充了帶有查看頁面適配器的獨立片段。在我的第二個選項卡中,有一個列表視圖(包含在一個片段中)。當用戶點擊一個列表視圖時,我需要在那裏顯示另一個片段。但是,當用戶點擊一個列表視圖項目時,我得到了這樣的錯誤片段內的Listview onitemclick不起作用
No view found for id 0x7f0e008a (com.example.abc.myapp:id/new_output) for fragment MantraFragment{30391e5 #3 id=0x7f0e008a}
我在其他問題在stackoverflow中發現此錯誤。試過他們的解決方案,但沒有奏效。請幫我解決這個問題。
類,其中包括列表視圖
public class TwoFragment extends Fragment {
private ListView listView;
private String items[];
public TwoFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
items = getResources().getStringArray(R.array.races_array);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
listView = (ListView) rootView.findViewById(R.id.mylistView);
CustomListAdapter adapter = new CustomListAdapter(getActivity(), items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
FragmentManager fm = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
Fragment newFrag = new MyNewFragment();
Bundle arguments = new Bundle();
newFrag.setArguments(arguments);
ft.replace(R.id.new_output, newFrag);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}}
MyNewFragment.class
public class MyNewFragment extends Fragment{
public MyNewFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView= inflater.inflate(R.layout.mynewfrag_view, container, false);
return rootView;
}}
MyNewFragment視圖(mynewfrag_view)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#FF0000"
android:textSize="20sp"
android:id="@+id/msg2"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b0b0ff"
android:id="@+id/new_output" />
</LinearLayout>
MainActivity類別
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "One");
adapter.addFragment(new TwoFragment(), "Two");
adapter.addFragment(new ThreeFragment(), "Three");
viewPager.setAdapter(adapter);
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
} }}
MainActivity佈局(activity_main)
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
PS:我仍然在Android開發初學者,我認爲這是很好用的一個片段有insted的展示活動中。 (糾正我,如果我錯了)
你可以請張貼有關您是如何加載標籤頁的代碼?可能是你正在加載基片段容器中的子片段。這就是你遇到問題的地方。 –
@ TrickySolutons-非常感謝。我已經更新了代碼。 – venura
'<片段 機器人:layout_width = 「match_parent」 機器人:layout_height = 「match_parent」 機器人:背景= 「#b0b0ff」 機器人:ID = 「@ + ID/new_output」/>' 上面的FrameLayout替換 '<的FrameLayout 機器人:layout_width = 「match_parent」 機器人:layout_height = 「match_parent」 機器人:背景= 「#b0b0ff」 機器人:ID = 「@ + ID/new_output」/>' 應該解決您問題 –