单击某些列表视图中的项目时出错
error clicking item in some listview
当我单击一个项目并打开其他 activity 并单击列表中的图像内容时,但是当该项目位于主视图之外时我收到错误消息。
点击这些项目后activity我可以看到
这些项目我在下一个看不到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);
}
});
下一个 activity 我看到图片的地方被点击了
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.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.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)
由于视图回收 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);
}
当我单击一个项目并打开其他 activity 并单击列表中的图像内容时,但是当该项目位于主视图之外时我收到错误消息。
点击这些项目后activity我可以看到
这些项目我在下一个看不到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);
}
});
下一个 activity 我看到图片的地方被点击了
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.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.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)
由于视图回收 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);
}