2013-03-07 46 views
0

我在自定義的onClick監聽器中調用startActivity()方法時遇到了問題。代碼編譯得很好,但是當它到達執行的那一點時,我得到一個nullPointerException。Android FLAG_ACTIVITY_NEW_TASK無法正常工作

代碼

private static class ProverbAdapter extends ArrayAdapter<String> { 
    MainActivity ma; 
    public ProverbAdapter(Context context, int layout, int resId, String[] items) { 
     super(context, layout, resId, items); 
     this.ma = new MainActivity(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     Context context = getContext(); 
     if(row == null) { 
      row = LayoutInflater.from(context).inflate(R.layout.proverb_layout, parent, false); 
     } 

     String item = getItem(position); 
     TextView proverbDate = (TextView)row.findViewById(R.id.proverb_date); 
     TextView proverbText = (TextView)row.findViewById(R.id.proverb_content); 
     String[] data = item.split("----"); 
     proverbDate.setText(data[0]); 
     proverbText.setText(data[1]); 

     ImageButton emailButton = (ImageButton)row.findViewById(R.id.emailButton); 
     ImageButton twitterButton = (ImageButton)row.findViewById(R.id.twitterButton); 
     ImageButton facebookButton = (ImageButton)row.findViewById(R.id.facebookButton); 

     emailButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "email", context)); 
     twitterButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "twitter", context)); 
     facebookButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "facebook", context)); 

     return row; 
    } 
} 

public class ProverbOnClickListener implements OnClickListener { 

    String proverb_date, proverb_content, eventType; 
    Context context; 

    public ProverbOnClickListener(String proverb_date, String proverb_content, String eventType, Context context) { 
      this.proverb_date = proverb_date; 
      this.proverb_content = proverb_content; 
      this.eventType = eventType; 
      this.context = context; 
    } 

    @Override 
    public void onClick(View v) { 
     Toast.makeText(this.context, this.eventType + ": " + this.proverb_content, Toast.LENGTH_SHORT).show(); 
     if(this.eventType == "email") { 
      Intent i = new Intent(Intent.ACTION_SEND); 
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      i.setType("message/rfc822"); 
      i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
      i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
      i.putExtra(Intent.EXTRA_TEXT , "body of email"); 
      try { 
       context.startActivity(Intent.createChooser(i, "Send mail...")); 
      } catch (android.content.ActivityNotFoundException ex) { 
       Toast.makeText(this.context, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

    } 

異常

致命異常:主android.util.AndroidRuntimeException:從一個活動上下文之外調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。這真的是你想要的嗎?

我添加了新的標誌作爲編譯器建議,但它仍然崩潰與相同的錯誤。

* 編輯 * ***

第一響應以下建議後,我的適配器現在的樣子:

private static class ProverbAdapter extends ArrayAdapter<String> { 
    Context context; 
    public ProverbAdapter(Context context, int layout, int resId, String[] items) { 
     super(context, layout, resId, items); 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     if(row == null) { 
      LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = li.inflate(R.layout.proverb_layout, parent, false); 
     } 

     String item = getItem(position); 
     TextView proverbDate = (TextView)row.findViewById(R.id.proverb_date); 
     TextView proverbText = (TextView)row.findViewById(R.id.proverb_content); 
     String[] data = item.split("----"); 
     proverbDate.setText(data[0]); 
     proverbText.setText(data[1]); 

     ImageButton emailButton = (ImageButton)row.findViewById(R.id.emailButton); 
     ImageButton twitterButton = (ImageButton)row.findViewById(R.id.twitterButton); 
     ImageButton facebookButton = (ImageButton)row.findViewById(R.id.facebookButton); 

     emailButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "email", context)); 
     twitterButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "twitter", context)); 
     facebookButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "facebook", context)); 

     return row; 
    } 
} 

但是我收到一個編譯錯誤。

沒有可以訪問MainActivity的封閉實例類型。

+0

嘗試'我.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);'而不是' i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);' – 2013-03-07 14:58:55

+0

@RaghavSood感謝您的建議。我試過了,而且我仍然得到相同的錯誤。 – 2013-03-07 15:02:33

+0

你確定這個特定的意圖是造成這個問題嗎? – 2013-03-07 15:04:24

回答

2

嘗試一些修改

this.ma = new MainActivity(); //it is wrong 

不要實例化活動

嘗試這樣

Context context; 
    public ProverbAdapter(Context context, int layout, int resId, String[] items) { 
     super(context, layout, resId, items); 
     this.context=context; 
    } 

使用這個變量情況下在任何你需要的上下文

+0

@JackSlingerland更新您的問題... – Pragnani 2013-03-07 15:43:49

+0

我更新了一些編輯和新編譯錯誤的問題。 – 2013-03-07 15:44:43

+0

LayoutInflater li =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)get layout inflatter像這樣,靜態工廠方法instaed – Pragnani 2013-03-07 15:46:52