我希望在ListView
中使用GridView
和Fragment
,但我在此行中收到错误:
adapter.notifyDataSetChanged();
我的错误是NullpointException
,我正在使用库支持版本4。
public class Gallery_fragment extends FragmentActivity {
private ViewGroup layout;
public static Database db;
private View view;
public static ArrayAdapter adapter;
public static ArrayList<StructureImage> images = new ArrayList<StructureImage>();
public static ArrayList<StructureImage> images2 = new ArrayList<StructureImage>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.gallery);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new PagerAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
this.db = new Database(this);
db.database();
getImageListFromServer();
// GridView list = (GridView) layout.findViewById(R.id.list);
// list.setAdapter(new AdapterImage(images));
}//end oncreate
private void getImageListFromServer() {
String result = Webservice.readUrl("http://192.168.1.100/file-server/service.php", null);
if (result != null) {
try {
db.open();
JSONArray tasks = new JSONArray(result);
String[] array_1 = new String[100];
for (int i1 = 0; i1 < db.count_gallery(); i1++) {
array_1[i1] = db.Display_gallery(i1, 0);
}
for (int i = 0; i < tasks.length(); i++) {
JSONObject object = tasks.getJSONObject(i);
String id = object.getString("id");
String title = object.getString("title");
String tozihat = object.getString("tozihat");
String thumbnail = object.getString("thumbnail");
int pars = Integer.parseInt(id);
Log.i("LOG", "i=" + i);
if (Arrays.asList(array_1).contains(id)) {
Log.i("LOG", "NO ! new Record");
} else {//new id}
db.inser_gallery(pars, title, tozihat, thumbnail);
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
db.close();
fill_listview();
}
private void getImageListFromServer2() {
String result = Webservice.readUrl("http://192.168.1.100/file-server/service.php", null);
if (result != null) {
try {
db.open();
JSONArray tasks = new JSONArray(result);
String[] array_1 = new String[100];
for (int i1 = 0; i1 < db.count_news(); i1++) {
array_1[i1] = db.Display_news(i1, 0);
}
for (int i = 0; i < tasks.length(); i++) {
JSONObject object = tasks.getJSONObject(i);
String id = object.getString("id");
String title = object.getString("title");
String tozihat = object.getString("tozihat");
String thumbnail = object.getString("thumbnail");
int pars = Integer.parseInt(id);
Log.i("LOG", "i=" + i);
if (Arrays.asList(array_1).contains(id)) {
Log.i("LOG", "NO ! new Record");
} else {//new id}
db.inser_news(pars, title, tozihat, thumbnail);
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
db.close();
fill_listview2();
}
private void fill_listview() {
images.clear();
G.pagenumber.clear();
G.imageIds.clear();
G.imageTitles.clear();
G.filenaming.clear();
db.open();
G.dbcountposition = db.count_gallery();
for (int i = 0; i < db.count_gallery(); i++) {
StructureImage image = new StructureImage();
image.id = db.Display_gallery(i, 0);
image.title = db.Display_gallery(i, 1);
image.tozihat = db.Display_gallery(i, 2);
image.thumbnail = db.Display_gallery(i, 3);
// Start - add for view pager
int imageId = G.context.getResources().getIdentifier("image" + db.Display_gallery(i, 0), "drawable", G.context.getPackageName());
Log.i("LOG", "image id: " + imageId);
G.pagenumber.add(i);
G.imageIds.add(imageId);
G.imageTitles.add("" + db.Display_gallery(i, 1));
G.filenaming.add("" + db.Display_gallery(i, 3));
// End
int pp = Integer.parseInt(db.Display_gallery(i, 0));
Log.i("LOG2", pp + "--- 1");
if (pp > 17) {
Log.i("LOG2", "image.thumbnail" + image.thumbnail);
DownloadManager.addToDownloadList(image.thumbnail);
}
images.add(image);
}
Collections.sort(images);
Collections.reverse(images);
adapter.notifyDataSetChanged();
db.close();
}
private void fill_listview2() {
images2.clear();
G.pagenumber2.clear();
G.imageIds2.clear();
G.imageTitles2.clear();
G.filenaming2.clear();
db.open();
G.dbcountposition2 = db.count_news();
for (int i = 0; i < db.count_news(); i++) {
StructureImage image = new StructureImage();
image.id = db.Display_news(i, 0);
image.title = db.Display_news(i, 1);
image.tozihat = db.Display_news(i, 2);
image.thumbnail = db.Display_news(i, 3);
// Start - add for view pager
int imageId = G.context.getResources().getIdentifier("image" + db.Display_news(i, 0), "drawable", G.context.getPackageName());
Log.i("LOG", "image id: " + imageId);
G.pagenumber2.add(i);
G.imageIds2.add(imageId);
G.imageTitles2.add("" + db.Display_news(i, 1));
G.filenaming2.add("" + db.Display_news(i, 3));
// End
int pp = Integer.parseInt(db.Display_news(i, 0));
Log.i("LOG2", pp + "--- 1");
if (pp > 17) {
Log.i("LOG2", "image.thumbnail" + image.thumbnail);
DownloadManager.addToDownloadList(image.thumbnail);
}
images.add(image);
}
Collections.sort(images2);
Collections.reverse(images2);
adapter.notifyDataSetInvalidated();
db.close();
}
private boolean isNetworkConnected() {// chek network
ConnectivityManager cm = (ConnectivityManager) getSystemService(G.context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
G.internet_con = 0;// No network
return false;
} else
G.internet_con = 1;// network OK
return true;
}
private void alertdialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(G.context);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("خطا در برقراری ارتباط");
builder.setMessage(" تنظیمات اینترنت خود را چک کنید و دوباره تلاش کنید");
builder.setCancelable(false);
builder.setPositiveButton("خب", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
isNetworkConnected();
// intent = new Intent(MainActivity.this, MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(intent);
}
});
builder.create().show();
}
}
答案 0 :(得分:3)
在你的onCreate中你有一个名为adapter
的局部变量,你还有一个名为adapter
的全局变量你永远不会设置全局适配器变量,这就是为什么它的null