当webview打开时,Android App崩溃了

时间:2016-11-13 10:32:25

标签: android android-layout android-studio

我正在尝试将变量(url_1)值从主要活动转移到webview。但是,当我点击按钮应用程序崩溃。我也使用putExtra但它不起作用。请看下面的代码。

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;
    }
}

2 个答案:

答案 0 :(得分:1)

您的myIntent为空。您正在创建它:

Intent myIntent = null;

如果没有初始化,你想在里面放一些数据:

myIntent.putExtra("url", url_1);

我不知道你想对myIntent做什么,但必须在一个Activity中进行初始化,例如

myIntent = getIntent();

在适配器中,您可以从上下文中引用它,如:

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

并且在使用它之前确保intent不为null:

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

或者,如果你想要开始一个新的Activity(在你的情况下):

 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);

                }

答案 1 :(得分:1)

你这样做完全错了我的intent变量总是为null。你正在用新的Intent对象开始活动,然后将vaibales添加到不同的intent对象。你需要使用相同的Intent对象并首先将变量放入其中然后开始意图。

像这样

Intent myIntent  = new Intent(this,WebShow1.class);
myIntent.putExtra("url", url_1);
startActivity(myIntent);

和WebView类

String url = getIntent().getExtras().getString("url");