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" /> 

    1 answer 1

    Read the official documentation before you do something at random, especially since the main points are translated into Russian. At the moment you need to read the section about fragments . The fragment's onCreateView method is used to create the root element of the fragment user interface. Those. in it, the first thing you do is create a view that will be displayed, and at the end you should return this view from this method. Why are you trying to look for your getActivity().findViewById() call by calling getActivity().findViewById() . It is obvious that they are not there, and this method returns null , but you try to set some values ​​to them and then a NullPointerException . What is it can read here .

    In your case, your code should look like this:

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_deck_manager, container, false); //ищем и настраиваем view TextView name = (TextView) view.findViewById(R.id.textView2); /* Остальной код */ return view; } 
    • Redid on your advice, the error remained. - Nikolay Pavlin
    • @NikolayPavlin show markup - temq
    • @NikolayPavlin and the code you wrote in onCreateView - temq
    • Updated the main question. - Nikolay Pavlin
    • @NikolayPavlin look more attentively, the text field is still searched in the activation - temq