2016-11-13 10 views
-3

変数(url_1)の値をメインアクティビティからwebviewに転送しようとしています。しかし、ボタンをクリックすると、アプリがクラッシュします。私もputExtraを使用しますが、動作しません。下記のコードをご覧ください。webviewを開いたときにAndroidアプリがクラッシュする

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

[緊急物乞いのために] downvotedています(https://meta.stackoverflow.com/questions/326569/under-what-緊急または他の同様のフレーズからマイクエストへ)あなたの将来の質問に、この方法でボランティアに対処することをお控えください、ありがとう! – halfer

+0

は、その質問で緊急の懇願を見つけることができません...それは削除されますか? – Opiatefuchs

答えて

1

myIntentはnullです。

Intent myIntent = null; 

、あなたが内部のいくつかのデータを入れたい初期化せずに:あなたはでそれを作成している

myIntent.putExtra("url", url_1); 
私はあなたが myIntentで何をしたいのかを知っているが、それはで、初期化されなければならないドント

:あなたのアダプタで

myIntent = getIntent(); 

と例えば活動は、次のような文脈からそれを参照することができます

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

と意図はそれで何かを行う前に、nullでないことを確認し:

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

か、あなたは(たとえば、あなたの場合は)新しい活動を開始する場合:

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

       } 
+0

こんにちは@Opiatefuchsあなたの素早い返信をありがとう...私は全体の活動コードを追加しました。私が編集する必要があることを教えてください。私は初心者のプログラマーですが、まだコーディングの基本を学んでいます。 –

+0

それは魅力のように動作します。あなたの答えから新しいことを学ぶ,,,ありがとう... –

1

あなたは完全に間違っている私のインテント変数は常にnull.Andあなたは新しいインテントオブジェクトでアクティビティを開始し、別のインテントオブジェクトにvaibalesを追加しています。あなたは同じインテントオブジェクトを使用し、最初に変数を入れてからインテントを開始する必要があります。クラスこの

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

様およびWebViewの中

String url = getIntent().getExtras().getString("url"); 
関連する問題