我在将一个画廊应用到片段方面遇到了一些麻烦。我试图放入的画廊曾经是一个活动,但我希望它现在是一个片段,因为我现在正在开发一个不同的项目,使我的应用程序看起来更清洁。
我收到错误gridView.setAdapter(new ImageAdapter(this));
和
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
老实说,我不知道如何解决这些问题,任何帮助都会很棒......
另外,我对Fragments没有多少经验,我对它很新,所以欢迎任何可以帮助我理解我做错的细节!
这是Java代码:
package info.androidhive.slidingmenu;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class GalleryActivity extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); }
//setContentView(R.layout.gallery_layout);
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle saedInstanceState) {
View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);
GridView gridView = (GridView) rootView.findViewById(R.id.photos);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
答案 0 :(得分:3)
更改此
gridView.setAdapter(new ImageAdapter(this));
到
gridView.setAdapter(new ImageAdapter(getActivity()));
this
未引用活动上下文,因为您的类扩展了Fragment。 getActvity()
返回与此片段关联的活动。
同时更改
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
到
Intent i = new Intent(getActivity(), FullImageActivity.class);
此外,您还需要在onCreateView
的末尾返回rootView@Override // missing annotation
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle saedInstanceState) {
View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);
...// rest of the code
return rootView;
}
或覆盖onActivityCreated
并使用getView
初始化gridview。只需返回onCreateView
@Override // missing annotation
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle saedInstanceState) {
View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);
return rootView;
}
然后
@Override
public void onActivityCreated(Bundle savedInstanceState) {
GridView gridView = (GridView) getView().findViewById(R.id.photos);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(getActivity()));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getActivity(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
答案 1 :(得分:0)
请尝试以下代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.gallery_layout, null);
GridView gv1=(GridView)root.findViewById(R.id.gridview);
gv1.setAdapter(new ImageAdapter(getActivity()));
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
return root;
}
答案 2 :(得分:0)
@Override// This must be in above in your fragment implementing class
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle saedInstanceState) {
View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);
GridView gridView = (GridView) rootView.findViewById(R.id.photos);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(getActivity()));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getActivity(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
}
);
}
return rootView;
}
从您的代码中删除它:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); }
//setContentView(R.layout.gallery_layout);
这也不是一个很好的方法,你应该在创建活动后放置你的片段代码。
在此方法中:
@Override
public void onActivityCreated(Bundle savedInstanceState) {