我有一个gridview,我想在那里展示一些图片。我使用BaseAdapter作为适配器。当我运行代码时,会显示以下消息:
java.lang.NullPointerException:尝试调用虚方法 “android.view.ViewGroup $的LayoutParams 关于空对象引用的android.view.View.getLayoutParams()'。
我甚至不确定它意味着什么...我的代码中没有任何内容表示getLayoutParams()。我该如何解决这个问题?
savedNewsPapers类:
File file;
StringBuilder text;
String tempFolder = "Fira/HUB/temp/";
String personalInformationFolder = "Fira/HUB/PersonalInformation/";
String favoriteAppsGeneral = "Fira/HUB/FavoriteApps/General/";
static String selectedBoxContent = "";
static int currentNumber = 1;
static ArrayList<String> where;
GridView myGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_news_papers);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
createON();
}
}, 1000);
}
private void createON() {
myGrid = (GridView) findViewById(R.id.gridView);
loadSubscriptions();
}
private void loadSubscriptions() {
file = new File(Environment.getExternalStorageDirectory(),personalInformationFolder + "newsSubscription" + Integer.toString(currentNumber) + ".txt");
if (file.exists()) {
text = new StringBuilder();
text.setLength(0);
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null) {
text.append(line);
}
bufferedReader.close();
}
catch (IOException e) {}
if (!text.toString().isEmpty()) {
where = new ArrayList<>();
where.add(text.toString());
Log.e("got called", "loadSusbscription is not empty");
}
Log.e("got called", "loadSubscription");
text.setLength(0);
currentNumber = currentNumber + 1;
loadSubscriptions();
}else {
Log.e("got called", "loadSusbscription doesnt exist");
myGrid.setAdapter(new VivzAdapter(this));
Log.e("got called", "Adapter set");
}
}
class VivzAdapter extends BaseAdapter {
ArrayList<String> list;
Context context;
VivzAdapter(Context context) {
Log.e("got called", "VivzAdapter");
this.context = context;
list = savedNewsPapers.where;
}
@Override
public int getCount() {
Log.e("got called", "getCount");
return list.size();
}
@Override
public Object getItem(int i) {
Log.e("got called", "getItem");
return list.get(i);
}
@Override
public long getItemId(int i) {
Log.e("got called", "getItemID");
return i;
}
class ViewHolder {
ImageView myImageView;
ViewHolder(View v) {
Log.e("got called", "ViewHolder");
myImageView = (ImageView) v.findViewById(R.id.imageView);
Log.e("got called", "ViewHolder found image");
}
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Log.e("got called", "getView");
View row = view;
ViewHolder holder = null;
if(row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_item, viewGroup, false);
holder = new ViewHolder(row);
row.setTag(holder);
}else {
holder = (ViewHolder) row.getTag();
}
String listItem = list.get(i);
if (listItem.equals("BBC")) {
Log.e("SUCCESS", "CONG FUCKING GRATULATION!");
holder.myImageView.setImageResource(R.drawable.amazon_music_with_title);
}else {
holder.myImageView.setImageResource(R.drawable.amazon_music_with_title);
}
return null;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}