2016-09-05 15 views
0

listviewでアイテムをクリックしたときに新しいアクティビティを開きたいとします。しかし、アイテムがクリックされるとアプリがクラッシュします。この行はgetView()方法新しいアクティビティがListViewアダプタで開けない

public class ListViewAdapter extends ArrayAdapter { 

    List list = new ArrayList(); 
    private Context context; 

    public ListViewAdapter(Context context, int resource) { 
     super(context, resource); 
     this.context = context; 
    } 

    static class DataHandler { 
     TextView title; 
     CheckBox checkBox; 
    } 

    @Override 
    public void add(Object object) { 
     super.add(object); 
     list.add(object); 
    } 

    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return this.list.get(position); 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View row; 
     row = convertView; 

     final DataHandler handler; 
     if (convertView == null) { 
      final SharedPreferences sharedPreferences = getContext().getSharedPreferences("MyBookmark", Context.MODE_PRIVATE); 
      LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.row, parent, false); 
      handler = new DataHandler(); 
      handler.title = (TextView) row.findViewById(R.id.textView); 
      handler.checkBox = (CheckBox) row.findViewById(R.id.checkBox); 
      Boolean check = sharedPreferences.getBoolean(String.valueOf(position), false); 
      Boolean opened = sharedPreferences.getBoolean(String.valueOf(position) + "selected", false);//if opened = true listview 
      if (opened == true) { 
       //row.setBackgroundColor(Color.parseColor("#DCDCDC")); 
       handler.title.setTextColor(Color.parseColor("#696969")); 
      } 

      handler.checkBox.setChecked(check); 
      handler.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putBoolean(String.valueOf(position), handler.checkBox.isChecked()); 
        editor.commit(); 
       } 
      }); 
      row.setTag(handler); 
     } else { 
      handler = (DataHandler) row.getTag(); 
     } 

     DataProvider dataProvider; 
     dataProvider = (DataProvider) this.getItem(position); 
     handler.title.setText(dataProvider.get_title()); 

     row.setClickable(true); 
     row.setFocusable(true); 
     row.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(getContext(), handler.title.getText(), Toast.LENGTH_SHORT).show(); 
       handler.title.setTextColor(Color.parseColor("#696969")); 
       SharedPreferences sharedPreferences = getContext().getSharedPreferences("MyBookmark", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putBoolean(String.valueOf(position) + "selected", true); 
       editor.commit(); 
       context.startActivity(new Intent(context, MusicPlayer.class)); 
      } 

     }); 
     return row; 
    } 
} 

Logcat

FATAL EXCEPTION: main Process: com.radioaudio.motivationalaudios, PID: 5307 
     android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
     at android.app.ContextImpl.startActivity(ContextImpl.java:672) 
     at android.app.ContextImpl.startActivity(ContextImpl.java:659) 
     at android.content.ContextWrapper.startActivity(ContextWrapper.java:331) 
     at com.radioaudio.motivationalaudios.ListViewAdapter$2.onClick(ListViewAdapter.java:105) 
+0

context.startActivity(new Intent(context、MusicPlayer.class));でコンテキストを複製しようとしました。 getApplicationContext()で? – nitinsh99

+0

新しいアクティビティを開始する前にフラグを設定する必要があるというエラーメッセージが表示されます。 –

+0

@ nitinsh99は動作しません – akkk

答えて

1

私はコメントで述べたように、あなたのエラーメッセージがこの含まれています:Activityコンテキストの外側からstartActivity()を呼び出す

がFLAG_ACTIVITY_NEW_TASKフラグが必要です。これは本当にあなたが望むものですか?

だから、あなたが意図最初に宣言して、その意図にフラグを設定する必要があります。

Intent intent = new Intent(context, MusicPlayer.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(intent); 

例外は、少なくとも何であるかを満たす必要があります。

+0

お受け取りいただきありがとうございます! –

0

ないアダプタで表示するには、活動のリストビューにクリックイベントを割り当てる内部のエラーcontext.startActivity(new Intent(context, MusicPlayer.class));を与えます。

0

多分これは参考になっ次のようになります。

Intent myIntent = new Intent(v.getContext(), MusicPlayer.class); 
v.getContext().startActivity(myIntent); 
関連する問題