单击某个列表视图中的项目时出错

时间:2015-01-08 13:11:50

标签: android listview parse-platform

当我点击某个项目并打开其他活动时,单击列表中的图像内容,但当此项目位于主视图之外时,我收到错误。

点击后我可以在下一个活动中看到这些项目

These items I can see in the next activity when are clicked

我在下一个活动中看不到这些项目

These item I cannot see in the next activity

配置列表时的代码

// Initialize the subclass of ParseQueryAdapter
    urgentTodosAdapter = new CustomAdapter(this);

    // Initialize ListView and set initial view to mainAdapter
    listView = (ListView) findViewById(R.id.list);
    /*listView.setAdapter(mainAdapter);
    mainAdapter.loadObjects();*/
    listView.setAdapter(urgentTodosAdapter);
    urgentTodosAdapter.loadObjects();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
        {
            // Get Person "behind" the clicked item

            View elemento = listView.getChildAt(position);
            System.out.println("position: " + position);
            ParseImageView imagen = (ParseImageView) elemento.findViewById(R.id.icon);

            Bitmap bitmap = ((BitmapDrawable)imagen.getDrawable()).getBitmap();

            System.out.println("bitmap: " + bitmap);
           bitmap = scaleDownBitmap(bitmap, 100, getApplicationContext());


            Intent i = new Intent(getApplicationContext(), SingleTouchImageViewActivity.class);

            i.putExtra("imagen_enviada", bitmap);
            startActivity(i);



        }
    });

我看到图片点击的下一个活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_touchimageview);
    //
    // DecimalFormat rounds to 2 decimal places.
    //
    df = new DecimalFormat("#.##");

    image = (TouchImageView) findViewById(R.id.img);

    Bundle extras = getIntent().getExtras();
    Bitmap bmp = (Bitmap) extras.getParcelable("imagen_enviada");
    image.setImageBitmap(bmp);

    //
    // Set the OnTouchImageViewListener which updates edit texts
    // with zoom and scroll diagnostics.
    //

}

}

适配器:

public class CustomAdapter extends ParseQueryAdapter<ParseObject> {

public CustomAdapter(Context context) {
    // Use the QueryFactory to construct a PQA that will only show
    // Todos marked as high-pri
    super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("Image");
            query.orderByAscending("fechaFiesta");
            //query.whereEqualTo("highPri", true);
            return query;
        }
    });
}

// Customize the layout by overriding getItemView
@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
    if (v == null) {
        v = View.inflate(getContext(), R.layout.urgent_item, null);
    }

    super.getItemView(object, v, parent);

    // Add and download the image
    ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.icon);
    ParseFile imageFile = object.getParseFile("sizeNormal");
    if (imageFile != null) {
        todoImage.setParseFile(imageFile);
        todoImage.loadInBackground();


    }

    // Add the title view
    TextView titleTextView = (TextView) v.findViewById(R.id.text1);
    titleTextView.setText(object.getString("title"));


    return v;
}

}

控制台日志

    01-08 12:42:30.400: W/dalvikvm(20591): threadid=1: thread exiting with uncaught exception (group=0x417f3d58)
01-08 12:42:30.410: E/AndroidRuntime(20591): FATAL EXCEPTION: main
01-08 12:42:30.410: E/AndroidRuntime(20591): Process: com.trastevere, PID: 20591
01-08 12:42:30.410: E/AndroidRuntime(20591): java.lang.NullPointerException
01-08 12:42:30.410: E/AndroidRuntime(20591):    at com.trastevere.Eventos$2.onItemClick(Eventos.java:66)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.widget.AbsListView.performItemClick(AbsListView.java:1129)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2927)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.widget.AbsListView$3.run(AbsListView.java:3676)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.os.Handler.handleCallback(Handler.java:733)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.os.Looper.loop(Looper.java:136)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at android.app.ActivityThread.main(ActivityThread.java:5047)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at java.lang.reflect.Method.invoke(Method.java:515)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
01-08 12:42:30.410: E/AndroidRuntime(20591):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

由于视图回收listView.getChildAt(i)的作用,其中0是第一个可见行,(n-1)是最后一个可见行(其中n是您看到的可见视图数)。

如果您只需要点击的视图,则已经通过 OnItemClick() 方法传递给您。

所以这就是你需要做的事情

    @Override
    public void onItemClick(AdapterView<?> arg0, View elemento, int position, long arg3) 
    {
        // Get Person "behind" the clicked item

        System.out.println("position: " + position);
        ParseImageView imagen = (ParseImageView) elemento.findViewById(R.id.icon);

        Bitmap bitmap = ((BitmapDrawable)imagen.getDrawable()).getBitmap();

        System.out.println("bitmap: " + bitmap);
       bitmap = scaleDownBitmap(bitmap, 100, getApplicationContext());


        Intent i = new Intent(getApplicationContext(), SingleTouchImageViewActivity.class);

        i.putExtra("imagen_enviada", bitmap);
        startActivity(i);


    }