android listview onItemClick

时间:2014-08-02 21:10:19

标签: android listview onclick

每次我运行时,都会出错。我不知道出了什么问题? 我只想通过索引从array获取字符串并将其放在URL上并在其他活动中运行。

package info.androidhive.customlistviewvolley;

import java.util.ArrayList;

import com.squareup.picasso.Picasso;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.app.Activity;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class Show extends Activity {
    ArrayList<String> lista = new ArrayList<String>();
    ArrayAdapter<String> adaptera;
       int po;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);

     ListView myListView = (ListView)findViewById(R.id.mylistView1);
        myListView.setClickable(true);  
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                 Intent appInfo = new Intent(getApplicationContext(), VideoViewActivity.class);
                 ArrayList<String> dad =appInfo.getStringArrayListExtra("eps");
                 int itemPosition  = position;
                 appInfo.putExtra("url",dad.get(itemPosition).toString());
                 startActivity(appInfo);
              }
            });



    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Intent i = getIntent();
        String aaaaa = i.getStringExtra("img");
        String tit = i.getStringExtra("rank");
        TextView te = (TextView) findViewById(R.id.textView1);
        te.setText(tit);
        ArrayList<String> dad =i.getStringArrayListExtra("eps");
        ImageView isd = (ImageView)findViewById(R.id.imageView1);
        Picasso.with(getBaseContext()).load(aaaaa).into(isd);
        ListView myListView = (ListView)findViewById(R.id.mylistView1);
        ArrayList<String> myStringArray1 =  new ArrayList<String>();
        for (int f= 1; f <= dad.size();f++)
        {
            myStringArray1.add(String.valueOf(f));
        }
        adaptera = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray1);

            adaptera.notifyDataSetChanged();
            myListView.setAdapter(adaptera);
    }


}

2 个答案:

答案 0 :(得分:2)

在这段代码上:

Intent appInfo = new Intent(getApplicationContext(), VideoViewActivity.class);
ArrayList<String> dad =appInfo.getStringArrayListExtra("eps");
int itemPosition  = position;
appInfo.putExtra("url",dad.get(itemPosition).toString());
startActivity(appInfo);

变量dadnull,因此dad.get(itemPosition)会抛出NullPointerException。

答案 1 :(得分:0)

appInfo是一个新创建的意图,因此它没有任何关联的额外数据,因此dad将为null,并且在尝试调用{{1}的任何方法时将获得NullPointerException }。

如果有人使用dad方法添加数据,您只能从意图中获取数据。

我只能猜测你要做什么,但是如果你想获得点击列表的值,那么正确的代码就是:

putExtra