0

Из адаптера вставляются данные в textView в Item, при попытки вывести данные в тост таким образом Toast.makeText(CardActivity.this, tvInviz.getText().toString(),Toast.LENGTH_LONG).show(); Получаю ошибку android.widget.TextView.getText()' on a null object reference' Использую либу https://github.com/Diolor/Swipecards

Item

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center"
    android:layout_width="300dp"
    android:layout_height="250dp">


<TextView
    android:id="@+id/helloText"
    android:textSize="40sp"
    android:textColor="@android:color/white"
    android:background="#E339"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<View
    android:id="@+id/item_swipe_left_indicator"
    android:alpha="0"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_margin="10dp"
    android:background="#A5F" />

<View
    android:id="@+id/item_swipe_right_indicator"
    android:alpha="0"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_margin="10dp"
    android:layout_gravity="right"
    android:background="#5AF" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView3"
    />

Activity

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.vadik.alfatest.CardActivity">

<com.lorentzos.flingswipe.SwipeFlingAdapterView
    android:id="@+id/frame"
    android:background="#ffeee9e2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:rotation_degrees="15.5"
    tools:context=".CardActivity" />

</RelativeLayout>

Код полностью

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card);

    tvInviz = (TextView)findViewById(R.id.textView3);


    SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
    dbHelper = new DBHelper(this);
    db = dbHelper.getReadableDatabase();

    Intent intent1 = getIntent();
    selectionArgs = intent1.getStringArrayExtra("selection_args");
    String [] forQuery  = new String[] {"_id","col_rus","col_eng"};
    cursor = db.query("my_table",forQuery,"col_category =?",selectionArgs,null,null,null);
    final ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

  cursor.moveToFirst();
  while(!cursor.isAfterLast()) {
      m = new HashMap<String, Object>();
      m.put(ATTRIBUTE_RUS_TRANSLATE,cursor.getString(cursor.getColumnIndex(DBHelper.COL_RUS)));
      m.put(ATTRIBUTE_ENG_TRANSLATE,cursor.getString(cursor.getColumnIndex(DBHelper.COL_ENG)));

        data.add(m);
        cursor.moveToNext();

    }

    String[] from = new String[]{ATTRIBUTE_RUS_TRANSLATE,ATTRIBUTE_ENG_TRANSLATE} ;
    int[] to = new int[]{R.id.helloText,R.id.textView3};

     final SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,R.layout.item,from,to);



    flingContainer.setAdapter(simpleAdapter);
    flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
        @Override
        public void removeFirstObjectInAdapter() {

            data.remove(0);

            simpleAdapter.notifyDataSetChanged();
        }

        @Override
        public void onLeftCardExit(Object dataObject) {

            Toast.makeText(CardActivity.this, "Left!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRightCardExit(Object dataObject) {
            Toast.makeText(CardActivity.this, "Right!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdapterAboutToEmpty(int itemsInAdapter) {

            simpleAdapter.notifyDataSetChanged();
            Log.d("LIST", "notified");

        }

        @Override
        public void onScroll(float v) {

        }
    });


    flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
        @Override
        public void onItemClicked(int itemPosition, Object dataObject) {
            Toast.makeText(CardActivity.this, tvInviz.getText().toString(),Toast.LENGTH_LONG).show();
        }
    });


}

}

xebeche
  • 65
  • 7

1 Answers1

2

Правильно, потому что у Вас нет в Activity textView.

Вы должны получить текс либо по itemPosition из data, либо непосредственно из dataObject

VAndrJ
  • 15,791