gpt4 book ai didi

java - SwipeListView 使用 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:07 26 4
gpt4 key购买 nike

我正在尝试使用 SwipeListView,但是当我触摸我的应用程序上的某个项目时,它会崩溃,日志信息在那里:

05-26 21:52:26.545: E/AndroidRuntime(19862): java.lang.NullPointerException
05-26 21:52:26.545: E/AndroidRuntime(19862): at com.fortysevendeg.android.swipelistview.SwipeListViewTouchListener.setFrontView(SwipeListViewTouchListener.java:121)
05-26 21:52:26.545: E/AndroidRuntime(19862): at com.fortysevendeg.android.swipelistview.SwipeListViewTouchListener.onTouch(SwipeListViewTouchListener.java:485)
05-26 21:52:26.545: E/AndroidRuntime(19862): at com.fortysevendeg.android.swipelistview.SwipeListView.onInterceptTouchEvent(SwipeListView.java:481)

一步步使用

import android.app.Activity;
import android.os.Bundle;
import com.fortysevendeg.android.swipelistview.SwipeListView;
import com.mkyong.android.adaptor.MobileArrayAdapter;

public class ListMobileActivity extends Activity {

static final String[] MOBILE_OS = new String[] { "Android", "iOS",
"WindowsMobile", "Blackberry"};

MobileArrayAdapter myad ;
private SwipeListView swipeListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
swipeListView =(SwipeListView)findViewById(R.id.book_listview);
myad=new MobileArrayAdapter(this, MOBILE_OS);
swipeListView.setAdapter(myad);

}

}

这是我的布局文件

<?xml version="1.0" encoding="UTF-8"?>
android:id="@+id/LinearLayoutListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >

xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="@+id/book_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#00000000"
swipe:swipeActionLeft="dismiss"
swipe:swipeBackView="@+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="@+id/front"
swipe:swipeMode="both" />

有什么我想念的吗?谢谢

最佳答案

只需确保您在适配器中膨胀的布局包含 @+id/back@+id/front 组件。

换句话说,如果您的 SwipeListView 声明将 @+id/back@+id/front 声明为滑动 View ...

    <...SwipeListView
android:id="@+id/swipe_list"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:layout_height="0dp"
app:swipeFrontView="@+id/front" <=====
app:swipeBackView="@+id/back" <=====
app:swipeActionLeft="dismiss"
app:swipeCloseAllItemsWhenMoveList="true"
app:swipeMode="both"
/>

...那么您在适配器中膨胀的布局应该包含这些组件。例如,在适配器中膨胀的布局称为 package_row.xml,这里是适配器的 fragment :

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final PackageItem item = getItem(position);
ViewHolder holder;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.package_row, parent, false);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
holder = new ViewHolder();
holder.ivImage = (ImageView) convertView.findViewById(R.id.example_row_iv_image);
...
} else {
holder = (ViewHolder) convertView.getTag();
}
...

关于java - SwipeListView 使用 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16760155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com