2

Hie Friends

I have one ListView which displays image data from JSON parser for that I use Custom Base Adapter.

But I got following exception.

FATAL EXCEPTION: main
java.lang.NullPointerException
    at com.example.ecard.ECard_main$AsycLove.onPostExecute(ECard_main.java:193)
    at com.example.ecard.ECard_main$AsycLove.onPostExecute(ECard_main.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:602)
    at android.os.AsyncTask.access$600(AsyncTask.java:156)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

My Code Is:

package com.example.ecard;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import com.loopj.android.image.SmartImageView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ECard_main extends Activity 
{
    String catagories;
    String anim_id,album_id,anim_name,anim_thumb,anim_url;

    TextView title;
    SmartImageView image;
    ListView hlv;
    ArrayList<HashMap<String, String>> albumList;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ecard_main);

        Intent i=getIntent();
        catagories=i.getExtras().getString("Catagories");


        title=(TextView)findViewById(R.id.tv_main_title);
        image=(SmartImageView)findViewById(R.id.IV_image);
        title.setText(catagories);

        //hlv = getListView();
        hlv = (ListView) findViewById(android.R.id.list);

        albumList = new ArrayList<HashMap<String, String>>();

        new AsycLove().execute();

    }

    class AsycLove extends AsyncTask<String, String, String>     
    {
        ProgressDialog progressDialog;      

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();            
            progressDialog = new ProgressDialog(ECard_main.this);
            progressDialog.setTitle("Loading");
            progressDialog.setMessage("Please wait");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }

        @Override
        protected String doInBackground(String... aurl)        
        {           
            try 
            {
                HttpPost postMethod = new HttpPost("http://demo1.idevtechnolabs.com/smartecard/love.php");              
                BufferedReader bufferedReader = null;

                HttpClient client = new DefaultHttpClient();
                HttpResponse response = null;

                response = client.execute(postMethod);
                final int statusCode = response.getStatusLine().getStatusCode();

                Log.v("Album ::","Response:::--->"+response.toString());
                Log.v("Album ::","Status Code:::--->"+statusCode);

                bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer stringBuffer = new StringBuffer("");
                String line = "";
                String LineSeparator = System.getProperty("line.separator");
                while ((line = bufferedReader.readLine()) != null) 
                {
                    stringBuffer.append(line + LineSeparator); 
                }
                bufferedReader.close(); 

                //-------------CONVERT DATA TO JSON---------------------------------

                try 
                {
                    String myjsonstring = stringBuffer.toString();

                    JSONArray jsonArray = new JSONArray(myjsonstring);

                    JSONObject jsonObj = null;

                    albumList.clear();

                    jsonObj = jsonArray.getJSONObject(0);   


                    for(int i=0; i<jsonArray.length();i++)
                    {
                            jsonObj = jsonArray.getJSONObject(i);                           

                            anim_id = jsonObj.getString("animation_id");      
                            album_id = jsonObj.getString("album_id");
                            anim_name = jsonObj.getString("animation_name");      
                            anim_thumb= jsonObj.getString("animation_thumb");
                            anim_url = jsonObj.getString("animation_url");    

                            anim_url=anim_url.replaceAll("\"","");


                            Log.v("Anim URL","Anim URL::"+anim_url);
                            Log.v("Anim Name","Anim Name::"+anim_name);

                            HashMap<String, String> tmp_album = new HashMap<String, String>();
                            tmp_album.put("anim_id", anim_id);
                            tmp_album.put("anim_thumb", anim_thumb);
                            albumList.add(tmp_album);   

                    }

                } 
                catch (Exception e) 
                {
                    Log.v("Home ::","Call JSON Exception in get Album in List--->"+e.toString());
                    e.printStackTrace();
                }
            } 
            catch (IOException e) 
            {
                Log.v("Exception: Get get Album in List","Name-"+e.toString());
                e.printStackTrace();
            }

            return "0";
        }

        @Override
        protected void onPostExecute(String code) 
        {   

            Toast.makeText(getApplicationContext(), "Image URL Call Successfully", Toast.LENGTH_LONG).show();
                Log.v("Album","Album List::"+albumList);
            ECard_main_Custom_Adapter adapter =new ECard_main_Custom_Adapter(getApplicationContext(),albumList);
            hlv.setAdapter(adapter);                

            progressDialog.dismiss();
            progressDialog = null;

        }
    }

}

And My Adapter Is:

import java.util.ArrayList;
import java.util.HashMap;

import com.loopj.android.image.SmartImageView;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class ECard_main_Custom_Adapter extends BaseAdapter
{
    private Context context;
    ArrayList<HashMap<String, String>> listAlbum;

    ViewHolder vholder;
    Drawable image; 

    public ECard_main_Custom_Adapter(Context context, ArrayList<HashMap<String, String>> albumList) 
    {

        this.context = context;
        this.listAlbum=albumList;
    }

    @Override
    public int getCount() 
    {
        Log.v("1","1");
        return listAlbum.size();
    }

    @Override
    public Object getItem(int position) 
    {
        Log.v("1","2");
        return position;
    }

    @Override
    public long getItemId(int position) 
    {
        Log.v("1","3");
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        Log.v("1","1");
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
        View vi=convertView;


        vi = inflater.inflate(R.layout.ecard_main_adapter, null);

        vholder = new ViewHolder();
        vholder.img_album=(SmartImageView)vi.findViewById(R.id.IV_image);
        vi.setTag(vholder);

        try
        {
            String anim_id=listAlbum.get(position).get("anim_id");
            String url=listAlbum.get(position).get("anim_thumb");

            Log.v("Anim ID and Anim URL","Id:"+anim_id+" URL:"+url);

            vholder.img_album.setImageUrl(url);
        }
        catch (Exception e) 
        {
            // TODO: handle exception
            Log.v("Error Ecard","Error is:::"+e);
        }
        return vi;
    } 

    static class ViewHolder 
    {
        SmartImageView img_album;
    }
}

Please tell me where I am wrong.

Any help is appreciated.

Thanks In advance.

2
  • Try, In post execute, Use ECard_main.this instead of getApplicationContext(). Commented Apr 16, 2014 at 6:59
  • What is line 193 in ECard_main.java? Commented Apr 16, 2014 at 6:59

1 Answer 1

8

First correct this

 hlv = (ListView) findViewById(android.R.id.list);

to

 hlv = (ListView) findViewById(R.id.list);

It might cause an error. and make sure your ecard_main.xml layout contains ListView with id list

Sign up to request clarification or add additional context in comments.

5 Comments

Oh That was reaily Sili Mistake. Thanks alot.
@NiravDabhi yo welcome! only silly mistake take long time to solved.
Hey Can I convert The listview to Horizontal List view?
why are you doing this? instead of used Horizontal Scroll View
Bcause My Client Require that. I cant do anything. Do you have any way for that dude?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.