2017-09-29 7 views
0

imageButtonを含むレイアウトを膨張させています。私はイベントをimageButtonに割り当てようとしているので、クリックされるたびにジョブが実行されます。以下 は(C#で)私のコードです:レイアウトインフレータの「非スタティックメンバにアクセスするにはオブジェクト参照が必要です。」

public class HomeScreenAdapter : BaseAdapter<TableItem> 
    { 
     List<TableItem> items; 
     Activity context; 
     public HomeScreenAdapter(Activity context, List<TableItem> items) 
      : base() 
     { 
      this.context = context; 
      this.items = items; 
     } 
     public override long GetItemId(int position) 
     { 
      return position; 
     } 
     public override TableItem this[int position] 
     { 
      get { return items[position]; } 
     } 
     public override int Count 
     { 
      get { return items.Count; } 
     } 
     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      var item = items[position]; 
      View view = convertView; 
      if (view == null) // no view to re-use, create new 
       view = context.LayoutInflater.Inflate(Resource.Layout.ticket_news, null); view.FindViewById<ImageView>(Resource.Id.Image).SetImageBitmap(item.imageBitmap); 
      view.FindViewById<ImageButton>(Resource.Id.imageButton2).Click += delegate 
      { 
       Intent sharingIntent = new Intent(Intent.ActionSend); 
       sharingIntent.SetType("text/plain"); 
       sharingIntent.PutExtra(Intent.ExtraText, "Message"); 
       StartActivity(Intent.CreateChooser(sharingIntent, "Share via")); // here where I am getting the error 
       PackageManager pm = PackageManager; // and here I am getting the same error again 
      }; 
      return view; 
     } 
    } 

オブジェクト参照が非静的フィールド 'ContextWrapper.SartActivity(インテント)' にアクセスするために必要とされる

なぜ私が取得していますこのエラーとどのようにそれを解決するには? は

+1

context.StartActivity(....)のような新しいアクティビティを開始するには、コンテキスト参照を使用してください。 –

+0

ありがとう、それは働いた! 'PackageManager pm = PackageManager;' StartActivity'と同じエラーが発生しました。どのようにこれを解決することができますか? –

+0

ここでそのコードを共有してください。私はチェックしてお知らせしますが、同じ問題を解決するためにコメントを提案してもらう必要があります。 –

答えて

0

にですが、あなたはアダプタクラスから活動を開始するコンテキスト参照を必要のように思える、あなたはまだちょうど以下のようにそれを使用するコンテンツの参照を持っていただきありがとうございます。また、あなたは、コンテキストを使用してPackageManagerのインスタンスを取得することができます

context.StartActivity(Intent.CreateChooser(sharingIntent, "Share via")); 

PackageManager packageManager = context.getPackageManager(); 
+0

packageManagerのためにうまくいかなかった –

+0

nullインスタンスが与えられたときに何が問題になっていますか? –

+0

'context.StartActivity'にいくつか問題があります。 'createchooser'は何度も起動しています。イメージボタンをクリックして、createchooserが起動し、イメージボタンを再度クリックせずにWhatsappを選択し、私が押して、createchhoserを再起動したとしましょう。これを解決するには? –

関連する問題