我遇到了拖放问题。
在我的场景中,我有一个片段,其中包含一组卡片,其中一张卡片包含拖放元素,即文本视图优先级。
Here the tutotrial for the drag and drop
This the youtube video for the same
当我介绍另一项活动时,该示例工作正常。但是当我在片段刷卡中做的时候它会引起问题。
错误日志
02-07 12:09:49.271 17876-17876/com.panenviron E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.panenviron, PID: 17876
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getChildCount()' on a null object reference
at com.panenviron.fragments.TaskCardListShow.onCreateView(TaskCardListShow.java:61)
TaskCardListShow.java片段,其中包含堆叠的卡片,当我尝试在此处实施拖放概念时这工作正常会导致应用程序崩溃
下面的是代码
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;
import com.jmedeisis.draglinearlayout.DragLinearLayout;
import com.panenviron.R;
import java.util.ArrayList;
import java.util.List;
import link.fls.swipestack.SwipeStack;
public class TaskCardListShow extends Fragment implements SwipeStack.SwipeStackListener, View.OnClickListener {
private ArrayList<String> mData;
private ArrayList<String> LayOutData;
private SwipeStack mSwipeStack;
private SwipeStackAdapter mAdapter;
public int cardCounter;
public TaskCardListShow() {
}
public static TaskCardListShow newInstance() {
TaskCardListShow _TaskCardListShow = new TaskCardListShow();
return _TaskCardListShow;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.task_card_list_show, container, false);
Log.e("im on","TaskCardListShow");
mSwipeStack = (SwipeStack) view.findViewById(R.id.swipeStack);
mData = new ArrayList<>();
LayOutData = new ArrayList<>();
mAdapter = new SwipeStackAdapter(mData,LayOutData);
mSwipeStack.setAdapter(mAdapter);
mSwipeStack.setListener(this);
cardCounter=4;// write function for getting card count.
fillStackCard();
DragLinearLayout dragDropAndroidLinearLayout = (DragLinearLayout) view.findViewById(R.id.drag_drop_layout);
for (int i = 0; i < dragDropAndroidLinearLayout.getChildCount(); i++) {
View child = dragDropAndroidLinearLayout.getChildAt(i);
dragDropAndroidLinearLayout.setViewDraggable(child, child);
}
return view;
}
private void fillStackCard() {
for (int x = 0; x <cardCounter; x++) {
mData.add(getString(R.string.dummy_text) + " " + (x + 1));
LayOutData.add(getString(R.string.str_card)+ (x + 1));
}
}
@Override
public void onClick(View v) {
}
@Override
public void onViewSwipedToRight(int position) {
String swipedElement = mAdapter.getItem(position);
}
@Override
public void onViewSwipedToLeft(int position) {
String swipedElement = mAdapter.getItem(position);
}
@Override
public void onStackEmpty() {
removeTaskCardFragment();
}
public void removeTaskCardFragment(){
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction mFragmentTransaction = manager.beginTransaction();
TaskCardListShow _TaskCardListShowFragment = new TaskCardListShow();
mFragmentTransaction.remove(_TaskCardListShowFragment);
mFragmentTransaction.commit();
manager.popBackStack();
}
public class SwipeStackAdapter extends BaseAdapter {
private List<String> mData;
private List<String> LayOutData;
public SwipeStackAdapter(List<String> data,List<String> Ldata) {
this.mData = data;
this.LayOutData = Ldata;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
String LName=LayOutData.get(position);
int id = getActivity().getApplicationContext().getResources().
getIdentifier(LName, "layout", getActivity().getPackageName());
convertView = getActivity().getLayoutInflater().inflate(id, parent, false);
Log.e("position=", String.valueOf(position));
return convertView;
}
}
}
Card_3.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
card_view:cardCornerRadius="@dimen/card_corner_radius"
card_view:cardElevation="@dimen/elevation_large"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.jmedeisis.draglinearlayout.DragLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drag_drop_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/drag_drop_textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fff"
android:elevation="3dp"
android:gravity="center"
android:padding="30dp"
android:text="P - 3"
android:textStyle="bold" />
<TextView
android:id="@+id/drag_drop_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fff"
android:elevation="3dp"
android:gravity="center"
android:padding="30dp"
android:text="P - 2"
android:textStyle="bold" />
<TextView
android:id="@+id/drag_drop_textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fff"
android:elevation="3dp"
android:gravity="center"
android:padding="30dp"
android:text="P - 4"
android:textStyle="bold" />
<TextView
android:id="@+id/drag_drop_textview3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fff"
android:elevation="3dp"
android:gravity="center"
android:padding="30dp"
android:text="P - 5"
android:textStyle="bold" />
</com.jmedeisis.draglinearlayout.DragLinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
gradle这个
compile 'com.jmedeisis:draglinearlayout:1.1.0'
答案 0 :(得分:1)
我的错误是我试图以错误的方式访问布局。即在来到我的拖放布局之前,我试图访问那里的字段,所以我只是改变了
if(position==2){ // drag and layou card will come at this condition.
DragLinearLayout dragDropAndroidLinearLayout =
(DragLinearLayout) findViewById(R.id.drag_drop_layout);
for (int i = 0; i < 3; i++) {
View child = dragDropAndroidLinearLayout.getChildAt(i);
dragDropAndroidLinearLayout.setViewDraggable(child, child);
}
}
现在我能看到拖拽lis但是这里也有另一个问题,
如果我拖放文本视图无法删除位置。它又回到了之前的位置。我有人吗?你能帮我解决吗?