Arbaz Alam Arbaz Alam - 22 days ago 8
Android Question

Android App get crashed when webview open

I am trying to transfer variable (url_1) value from main activity to webview. But when I click button app get crashed. I also use putExtra but it doesn't work. Please have a look on below code.

public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.new_custom_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = null;
if (position == 0) {
CustomAdapter.this.context.startActivity(new Intent(CustomAdapter.this.context, BalanceActivity.class));
}
if (position == 1) {
String url_1 = "http://www.facebook.com";
myIntent.putExtra("url", url_1);
CustomAdapter.this.context.startActivity(new Intent(CustomAdapter.this.context, WebShow1.class));

}
}
});
return rowView;
}
}

Answer

Your myIntent is null. You are creating it with:

Intent myIntent = null;

and without initializing you want to put some data inside:

myIntent.putExtra("url", url_1);

I don´t know what you want to do with myIntentbut it must be initialized, in an Activity for example with

myIntent = getIntent();

in your adapter, you can reference it from context like:

Intent myIntent = ((Activity) context).getIntent();

and be sure intent is not null before doing something with it:

if(myIntent!=null){
//do your stuff
}

or, if you for example want to start a new Activity (in your case):

 Intent myIntent = null;
                if (position == 0) {
                    myIntent = new Intent(CustomAdapter.this.context, BalanceActivity.class);
                    CustomAdapter.this.context.startActivity(myIntent);
                }
                if (position == 1) {

                    myIntent = new Intent(CustomAdapter.this.context, WebShow1.class);
                    String url_1 = "http://www.facebook.com";
                    myIntent.putExtra("url", url_1);
                    CustomAdapter.this.context.startActivity(myIntent);

                }
Comments