【问题标题】:IllegalStateException: Could not execute method of the activity - What is wrong?IllegalStateException:无法执行活动的方法 - 出了什么问题?
【发布时间】:2013-09-26 09:15:35
【问题描述】:

我正在尝试创建一个列表视图,其中包含多个可点击布局的项目。在这种情况下,列表视图是联系人列表。列表视图中的每一行代表一个单独的联系人,列表视图项上的每个按钮都应该触发与该联系人相关的操作 - 例如,viewbook约会

每个列表视图项的 XML:

<LinearLayout
    android:id="@+id/layoutBookAppt"
    android:layout_width="56dp"
    android:layout_height="fill_parent"
    android:layout_alignBottom="@+id/divider"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical"
    android:background="@drawable/list_selector_flatcolour"
    android:onClick="newAppt" >
    <ImageView
        android:id="@+id/apptNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/appt_new" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/layoutInfo"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:padding="8dp"
    android:layout_toStartOf="@id/layoutBookAppt"
    android:layout_toLeftOf="@id/layoutBookAppt"
    android:onClick="viewCustomer"
    android:background="@drawable/list_selector_flatcolour" >

    <!-- Various contact information here, name, number etc -->
</RelativeLayout>

我的适配器:

public class CustListAdapter extends BaseAdapter {

ArrayList<Customer> customers;
Context ctx;

public CustListAdapter(Context context, ArrayList<Customer> custs) {
    this.ctx = context;
    this.customers = custs;
}  

static class ViewHolder {
    RelativeLayout info;
    LinearLayout book;
    TextView name;
    TextView number;
    int pos; // Not sure if this is the best approach
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Customer customer = getItem(position);
    ViewHolder viewHolder;

    if (convertView == null) { //inflate convertView and populate viewHolder
        viewHolder = new ViewHolder();
        viewHolder.pos = position;
        convertView = LayoutInflater.from(ctx).inflate(R.layout.customer_info, parent, false);
        viewHolder.info = (RelativeLayout) convertView.findViewById(R.id.layoutInfo);
        viewHolder.book = (LinearLayout) convertView.findViewById(R.id.layoutBookAppt);
        viewHolder.name = (TextView) convertView.findViewById(R.id.lblCustName);
        viewHolder.number = (TextView) convertView.findViewById(R.id.lblNumber);
        convertView.setTag(viewHolder);        
    }
    else
        viewHolder = (ViewHolder) convertView.getTag(); 

        // set the textviews here
    return convertView;
    }
/* Other mandatory functions here */    
}

最后,我的活动的相关部分:

public class CustListActivity extends Activity {

ListView lstCustomers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cust_list);
    // Show the Up button in the action bar.
    setupActionBar();
    lstCustomers = (ListView) findViewById(R.id.custlist);
    populateListView();
}

private void populateListView() {

    // Populate arraylist 'customers' with cursor from database

    lstCustomers.setAdapter(new CustListAdapter(this, customers));
}

public void newAppt(View view)
{
    if (view == null)
        Gen.popup("View is null");
    else
    {
        ViewHolder vh = (ViewHolder)view.getTag();
        Gen.popup("book " + String.valueOf(vh.pos));
    }
}

public void viewCustomer(View view)
{
    if (view == null)
        Gen.popup("View is null");
    else
    {
        ViewHolder vh = (ViewHolder)view.getTag();
        Gen.popup("view " + String.valueOf(vh.pos));
    }
}
}

logcat 中的异常:

09-26 09:37:38.070: E/AndroidRuntime(766): FATAL EXCEPTION: main
09-26 09:37:38.070: E/AndroidRuntime(766): java.lang.IllegalStateException: Could not execute method of the activity
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.view.View$1.onClick(View.java:3704)
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.view.View.performClick(View.java:4232)
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.view.View$PerformClick.run(View.java:17298)
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.os.Handler.handleCallback(Handler.java:615)
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.os.Looper.loop(Looper.java:137)
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.app.ActivityThread.main(ActivityThread.java:4921)
09-26 09:37:38.070: E/AndroidRuntime(766):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 09:37:38.070: E/AndroidRuntime(766):  at java.lang.reflect.Method.invoke(Method.java:511)
09-26 09:37:38.070: E/AndroidRuntime(766):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-26 09:37:38.070: E/AndroidRuntime(766):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-26 09:37:38.070: E/AndroidRuntime(766):  at dalvik.system.NativeStart.main(Native Method)
09-26 09:37:38.070: E/AndroidRuntime(766): Caused by: java.lang.reflect.InvocationTargetException
09-26 09:37:38.070: E/AndroidRuntime(766):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 09:37:38.070: E/AndroidRuntime(766):  at java.lang.reflect.Method.invoke(Method.java:511)
09-26 09:37:38.070: E/AndroidRuntime(766):  at android.view.View$1.onClick(View.java:3699)
09-26 09:37:38.070: E/AndroidRuntime(766):  ... 11 more
09-26 09:37:38.070: E/AndroidRuntime(766): Caused by: java.lang.NullPointerException
09-26 09:37:38.070: E/AndroidRuntime(766):  at com.example.myapp.CustListActivity.newAppt(CustListActivity.java:110)
09-26 09:37:38.070: E/AndroidRuntime(766):  ... 14 more

所以基本上我想要做的是能够在用户按下列表视图项时获得两个位信息。

  1. 行位置
  2. 被按下的布局

我可以分别获得这些详细信息,但尝试获得两者被证明是困难的。我可以通过 XML 触发单独的 onClick 事件,但无法弄清楚如何找出被按下的行的位置。我也可以通过设置onItemClickListener 来获取行位置,但这只会在列表项本身被按下时触发,而不是其中的任何视图。

使用我的newAppt()viewCustomer() 函数,我正在尝试使用getTag() 获得行位置(我不确定这是否是最好的方法,但我正在尝试任何方法)-但是getTag()总是会返回 null,这让我完全不知所措。

我在正确的轨道上吗?如果不是,那么获得行和被按下的视图的最佳方式是什么?

(编辑:Gen.popup 是我自己的 TOAST 速记方法)。

【问题讨论】:

  • 发布您的 logcat 输出。
  • @JiteshDalsaniya 已编辑
  • Logcat 对你很好:Caused by: java.lang.NullPointerExceptioncom.example.myapp.CustListActivity.newAppt(CustListActivity.java:110)
  • 那么Gen 是谁?因为它似乎是空的
  • Gen.popup() 是我自己制作 TOAST 弹出窗口的速记函数 :) 抱歉没有说清楚。编辑了问题。

标签: android listview illegalstateexception


【解决方案1】:

替换下面的代码

ViewHolder vh = (ViewHolder)view.getTag();

final int position = listview.getPositionForView((View) view.getParent());

这一行给出了你点击的位置。

【讨论】:

  • 之前,我尝试过int pos = listview.getPositionForView(view),但是当视图开始被回收时,它返回了错误的位置。但是将view 更改为view.getParent() 效果惊人!非常感谢,这就是我一直在寻找的 :)
【解决方案2】:

您正在将 @+id/layoutBookAppt 布局的 onClick 属性设置为 newAppt 方法。实际上,getTag() 在那里是空的。但它与 ListView 子视图的标记不同。那就是LinearLayout的标签。

所以你正在做一些没有意义的事情......我正在回答另一个问题:你打算用这种方法做什么?

【讨论】:

  • But it's not the same tag of ListView children views. That is the tag of LinearLayout. getTag() 实际做了什么。 Jitesh 帮助我解决了这个问题,但是 +1 以获得额外的信息 - 谢谢 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
相关资源
最近更新 更多