Only understand with Android. There is a fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment setHasOptionsMenu(true); listView = (ListView) getActivity().findViewById(R.id.listView); SQLiteOpenHelper deck_helper = new DeckDatabaseHelper(getActivity()); SQLiteDatabase db = deck_helper.getReadableDatabase(); TextView name = (TextView) getActivity().findViewById(R.id.textView2); Cursor cursor = db.query("DECK", new String[]{"NAME"}, "_id=?", new String[]{Integer.toString(1)}, null, null, null); try { if (cursor.moveToFirst()) { String nameText = cursor.getString(0); name.setText(nameText); } cursor.close(); db.close(); } catch (SQLException e) { Toast toast = Toast.makeText(getActivity(), "Database unavailable", Toast.LENGTH_SHORT); toast.show(); } return inflater.inflate(R.layout.fragment_deck_manager, container, false); } I want the textView to be filled from the database with a column Name . Here is the DeckDatabaseHelper class:
public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE DECK (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "NAME TEXT, " + "CLASS TEXT, " + "IMAGE_CLASS INTEGER" + ");"); ContentValues values = new ContentValues(); values.put("NAME", "Mage"); values.put("CLASS", "Mage"); values.put("IMAGE_CLASS", "123"); db.insert("DECK",null,values); } When opening a fragment, I get an error:
ava.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang. java: 57)
UPD. New fragment code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_deck_manager, container, false); setHasOptionsMenu(true); listView = (ListView) view.findViewById(R.id.listView); SQLiteOpenHelper deck_helper = new DeckDatabaseHelper(getActivity()); SQLiteDatabase db = deck_helper.getReadableDatabase(); TextView name = (TextView) getActivity().findViewById(R.id.textView2); Cursor cursor = db.query("DECK", new String[]{"NAME"}, "_id=?", new String[]{Integer.toString(1)}, null, null, null); try { if (cursor.moveToFirst()) { String nameText = cursor.getString(0); name.setText(nameText); } cursor.close(); db.close(); } catch (SQLException e) { Toast toast = Toast.makeText(getActivity(), "Database unavailable", Toast.LENGTH_SHORT); toast.show(); } return view; } Markup:
<FrameLayout 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" tools:context="com.example.nikolaypavlin.deck.Fragments.DeckManagerFragment"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:headerDividersEnabled="false" android:visibility="visible" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_gravity="right|center_vertical" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/textView2" android:layout_gravity="right|top" />