如果我点击导航抽屉项目中的图库,我需要为图库页面创建一个单独的活动。我发布了迄今为止我尝试过的代码。
MainActivity.java:
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new GalleryFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
default:
break;
}
GalleryFragment.java:
public class GalleryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.chapter, container, false);
Intent i = new Intent(getActivity(), GalleryActivity.class);
startActivity(i);
return rootView;
}
}
gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
GalleryActivity.java:
public class GalleryActivity extends Activity {
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_activity);
textView1=(TextView)findViewById(R.id.textView1);
}
}
gallery_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lesson Page" />
</LinearLayout>
到目前为止,我这样做。但它没有用。在输出中,首先我得到了Gallery的单独活动。然后,如果我单击后退按钮,它将显示主页。但是,如果我单击导航抽屉中的图库,它不会移动到单独的库活动。在logcat中没有错误。
答案 0 :(得分:0)
displayView
中的代码实际上没有任何内容(假设没有你没有发布的内容)。它设置了一个局部变量fragment
,当方法结束时,它会超出范围。
如果您想启动GalleryActivity
,则需要与此类似的代码:
Intent i = new Intent(context, GalleryActivity.class);
startActivity(i);