在itemListner

时间:2017-09-15 21:17:17

标签: java android listview android-fragments android-intent

我有一个ListView并且适应ArrayAdatper并且我想在点击任何项目时打开新的活动,因此我使用了setOnItemClickListener然后使用了Intent

    ...
    ListView mList = (ListView) findViewById(R.id.list);
    mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(MainActivity.this,containerAll.class);
            startActivity(intent);
        }
    });

并且不知道containerAll.class:我有两个Fragments类:

      public class containerAll extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_pager);

    ViewPager mPager = (ViewPager) findViewById(R.id.pager);

    fragmentAdapter adapt = new fragmentAdapter(getSupportFragmentManager());

    mPager.setAdapter(adapt);

   }
}

当我单独运行每个类时,它可以正常工作,但是当我运行应用程序时,当我按下listView上的Item时,它会崩溃。

所以有人知道为什么会这样吗? 这是自定义适配器类:

Intent

这是logCat:

   public class cAdapter extends ArrayAdapter<arrayObject> {

    public cAdapter(Activity context, ArrayList<arrayObject> object){
        super(context,0,object);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //Create object from layout
         if(convertView == null){
             convertView = LayoutInflater.from(getContext()).inflate(R.layout.inside,parent,false);
         }
         arrayObject item= getItem(position);

        TextView distance = (TextView) convertView.findViewById(R.id.distance);
        // Change image inside the ifnlator
        ImageView image = (ImageView) convertView.findViewById(R.id.pic);
        image.setImageResource(item.imageRes);

        TextView name = (TextView) convertView.findViewById(R.id.name);
        name.setText(item.mName);

        distance.setVisibility(View.GONE);
        if(item.distance != null) {

            distance.setText(item.distance);
            distance.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

  }

1 个答案:

答案 0 :(得分:2)

错误说明了一切。在清单中添加活动,错误将消失。 喜欢这个

template <class C>
auto as_comma_list(C&& c)
{
    using std::begin; 
    using std::end;
    using string_type = std::decay_t<decltype(*begin(c))>;
    using char_type = std::decay_t<decltype(*begin(*begin(c)))>;
    string_type result;
    auto const ec = end(c);
    for (auto it = begin(c); it != ec; )
    {
        result += *it;
        if (++it != ec)
        {
            result += detail::comma<char_type>::c;
        }
        else break;
    }
    return result;
}

希望有所帮助。 :)