0
votes
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.adapter.CustomAdapter;
import com.nusecond.suredeal.app.suredeal.adapter.CustomPromoAdapter;

import java.util.ArrayList;

public class Promo extends AppCompatActivity {
    GridView gv;
    Context context;
    ArrayList programName;
    public static String[] programNameList={"AppleMacBook","HP_note_Book","LG_NEXUS","NokiaLumia","SamsungRT","SONY_BRAVIA","Sansui"};
    public static int [] programImages={R.drawable.apple,R.drawable.hp,R.drawable.nexus,R.drawable.lumia,R.drawable.fridge,R.drawable.tv,R.drawable.tv1};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gv=(GridView)findViewById(R.id.gridView1);
        //gv.setAdapter(new CustomPromoAdapter(this,programNameList,programImages));
        gv.setAdapter(new CustomPromoAdapter(this,programNameList,programImages));

    }
}
  1. This is my Activity To Display Images and Text In GridView.
  2. I'm Using Intent In Some Other Activity To This.
  3. I'm Getting Error With setAdapter method.
  4. Below Is my Adapter.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.activity.MainActivity;
import com.nusecond.suredeal.app.suredeal.activity.Promo;

/**
 * Created by ns2 on 2/4/16.
 */
public class CustomPromoAdapter extends BaseAdapter{
    ImageView imageView;
    TextView textView;
    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomPromoAdapter(Promo promo, String[] programNameList, int[] programImages) {
        // TODO Auto-generated constructor stub
        result=programNameList;
        context=promo;
        imageId=programImages;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        view=inflater.inflate(R.layout.promo_gridlist,null);
        textView=(TextView)view.findViewById(R.id.textView1);
        imageView=(ImageView)view.findViewById(R.id.imageView1);
        imageView.setImageResource(imageId[position]);
        textView.setText(imageId[position]);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }
}
  1. This is my Adapter I don't know where it went wrong.
  2. While Starting This Activity My app got Crash.

FATAL EXCEPTION: main Process: com.nusecond.suredeal.app.suredeal, PID: 25273 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nusecond.suredeal.app.suredeal/com.nusecond.suredeal.app.suredeal.activity.Promo}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.nusecond.suredeal.app.suredeal.activity.Promo.onCreate(Promo.java:36) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


This is My error Log Can anyone Help me to Solve This.

1
Check for gridView1 in your activity_main.xml file. As per the stacktrace your gridview (gv) is null.Krish
yes boss my Activity_main.xml pointed to some other layout.........ThanksRakesh

1 Answers

0
votes

Try this,

 CustomPromoAdapter adapter = new CustomPromoAdapter(this,programNameList,programImages)
    gv.setAdapter(adapter);