ViewRootImpl: ViewPostImeInputStage处理ListView中OnItemClick事件的指针0,涉及到Android的IT技术。

41
我有一个像这样的 cart_layout
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:id="@+id/cart_listview"
        android:layout_gravity="center_horizontal"
        android:background="@color/whiteBg"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="bottom"
        android:padding="5dp"
        android:gravity="bottom"
        android:background="@color/whiteBg">
        <!-- this layout contains a button and a textview which I don't think is the problem -->
    </LinearLayout>
</LinearLayout>

Cart.java中的Java代码:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cart_layout);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    dbhandler = new DatabaseHandler(this);
    product_all = dbhandler.getProduct();
    total = (TextView)findViewById(R.id.cart_total_textview);

    listview = (ListView)findViewById(R.id.cart_listview);
    
    cart_adapter = new Custom_Cart_Adapter(this,product_all);
    listview.setAdapter(cart_adapter);
    
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.v("ITEM CLICK","CLICKED ITEM POSITION: "+position);
            Intent intent = new Intent(Cart.this, Item_edit_details.class);
            intent.putExtra("the_product", product_all.get(position));
            startActivity(intent);
        }
    });
}

我只想创建一个OnItemClick事件,但每次点击项目时,即使有listview.setOnItemClickListener,Logcat也会显示以下内容:

ViewRootImpl:ViewPostImeInputStage处理指针0

ViewRootImpl:ViewPostImeInputStage处理指针1

然后什么也不会发生。
我还看到了一个奇怪的日志,有时它会说“true”,有时它会说“false”:

ActivityThread:updateVisibility:ActivityRecord {3308191 token = android.os.BinderProxy @ c7ed098 {com.iwant.namhhgames.newiwant / com.iwant.namhhgames.newiwant.Listing_items}} show:false

我不知道它是否与问题有关,而且我也不知道问题是什么时候出现的,可能是在我弄乱某些东西之后。
而且Logcat仅在真实设备上显示。使用AVD,则没有显示任何内容。
感谢您宝贵的时间。

同样的问题在这里。非常奇怪。 - filthy_wizard
当我按返回键时,应用程序不再冻结。 - filthy_wizard
这里也有同样的问题。有解决方法吗? - Waterfr Villa
有解决方案了吗? - sasha
我遇到了这个问题,我只是将我的“Activity”“content view”设置为空的“FrameLayout”。 - Eduardo Reis
显示剩余3条评论
4个回答

1

不应使用listview的onItemClickListener,因为它可能会引起许多问题。针对这种情况,建议在适配器的视图上使用自定义点击侦听器,并通过此点击侦听器传递索引,也就是尝试实现自己的onItemClickListener。

正如您所注意到的,在recyclerView中不会出现onItemClickListener,正确的方法是实现自己的onItemClickListener。


1
您没有列出Custom_Cart_Adapter.java。 尝试在Custom_Cart_Adapter.java中使用的single_item.xml中添加android:focusable="false" 这可以帮助。

1
这个可以顺利运行,所以可能是您的适配器出了问题。
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.testlist);

        ListView listview = (ListView)findViewById(R.id.listest);

        ArrayList<String> cart_adapter = new ArrayList<String>();

        cart_adapter.add("Me");
        cart_adapter.add("Him");
        cart_adapter.add("You");

        listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cart_adapter));

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("ITEM CLICK","CLICKED ITEM POSITION: "+position);
            }
        });

    }

11-16 14:55:31.735 1915-1915/ca.dti.grounded.app E/ITEM CLICK: 点击的项目位置:2 11-16 14:55:34.233 1915-1915/ca.dti.grounded.app E/ITEM CLICK: 点击的项目位置:0 11-16 14:55:35.616 1915-1915/ca.dti.grounded.app E/ITEM CLICK: 点击的项目位置:1 11-16 14:55:36.061 1915-1915/ca.dti.grounded.app E/ITEM CLICK: 点击的项目位置:2

0
你做得对,但是对于`ArrayAdapter`,你的`onItemClick()`监听器应该像这样。
ListView listview = (ListView)findViewById(R.id.listest);
   listview .setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            Log.e("ITEM CLICK","CLICKED ITEM POSITION: "+position);
      }
   });

希望这能对你有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接