0
Good Afternoon, I’m using a listview to display some images per url and cache them. So far, so good. But when I put Onitemclicklistener just to show Titulo with a message it gives the error
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object erikjota9.testeurl.AdapterListView.getItem(int)' on a null object reference
Mainactivity
package erikjota9.testeurl;
import java.util.ArrayList;
import java.util.List;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lvPosts;
private List<Post> list;
private ImageLoader mImageLoader;
public AdapterListView Adaptador;
public Post postteste;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<Post>();
list.add(new Post("Title 1", "https://cdn.ndtv.com/tech/images/gadgets/pikachu_hi_pokemon.jpg?output-quality=70&output-format=webp"));
list.add(new Post("Title 2", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 3", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 4", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 5", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 6", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 7", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 8", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 9", "https://image.ibb.co/nvOv9d/24bbe5962453d06619a0f29aae3a3d5e.jpg"));
list.add(new Post("Title 10", ""));
list.add(new Post("Title 11", "http://www.villopim.com.br/android/universal-image-loader/teste.png"));
DisplayImageOptions mDisplayImageOptions = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_launcher_background)
.showImageOnFail(R.drawable.ic_launcher_background)
.showImageOnLoading(R.drawable.ic_launcher_background)
.cacheInMemory(true)
.cacheOnDisk(true)
//.displayer(new FadeInBitmapDisplayer(1000))
.build();
ImageLoaderConfiguration conf = new ImageLoaderConfiguration.Builder(MainActivity.this)
.defaultDisplayImageOptions(mDisplayImageOptions)
.memoryCacheSize(50 * 1024 * 1024)
.diskCacheSize(50 * 1024 * 1024)
.threadPoolSize(5)
.writeDebugLogs()
.build();
mImageLoader = ImageLoader.getInstance();
mImageLoader.init(conf);
PauseOnScrollListener mPauseOnScrollListener = new PauseOnScrollListener(mImageLoader, true, true);
lvPosts = (ListView) findViewById(R.id.lvPosts);
lvPosts.setAdapter(new AdapterListView(MainActivity.this, list, mImageLoader));
lvPosts.setOnScrollListener(mPauseOnScrollListener);
lvPosts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Post pick = (Post) Adaptador.getItem(position);
String message = "You Selected " + pick.getTitle();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onDestroy(){
super.onDestroy();
mImageLoader.stop();
}
}
Post.java
package erikjota9.testeurl;
public class Post {
public String title;
private String urlImage;
public Post(String title, String urlImage){
this.title = title;
this.urlImage = urlImage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrlImage() {
return urlImage;
}
public void setUrlImage(String urlImage) {
this.urlImage = urlImage;
}
}
Adapterlistview
package erikjota9.testeurl;
import java.util.List;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AdapterListView extends BaseAdapter {
public List<Post> list;
private LayoutInflater inflater;
private ImageLoader mImageLoader;
public AdapterListView(Context context, List<Post> list, ImageLoader mImageLoader){
this.list = list;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mImageLoader = mImageLoader;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.item, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.ivPost = (ImageView) convertView.findViewById(R.id.ivPost);
holder.tvPost = (TextView) convertView.findViewById(R.id.tvPost);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.tvPost.setText(list.get(position).getTitle());
// IMAGE LOADER
mImageLoader.displayImage(list.get(position).getUrlImage(),
holder.ivPost,
null,
new ImageLoadingListener(){
@Override
public void onLoadingCancelled(String uri, View view) {
Log.i("Script", "onLoadingCancelled()");
}
@Override
public void onLoadingComplete(String uri, View view, Bitmap bmp) {
Log.i("Script", "onLoadingComplete()");
}
@Override
public void onLoadingFailed(String uri, View view, FailReason fail) {
Log.i("Script", "onLoadingFailed("+fail+")");
}
@Override
public void onLoadingStarted(String uri, View view) {
Log.i("Script", "onLoadingStarted()");
}
}, new ImageLoadingProgressListener(){
@Override
public void onProgressUpdate(String uri, View view, int current, int total) {
Log.i("Script", "onProgressUpdate("+uri+" : "+total+" : "+current+")");
}
});
return convertView;
}
private class ViewHolder{
public ImageView ivPost;
public TextView tvPost;
}
}
Perfect, I was having this difficulty, but it worked. Thanks and Hugs :)
– Erik Grupioni