Just understand Android.
There is a fragment:
Public View Oncreater (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) GetActiveIny (). 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 to the textView filled from the database by the name
column.
Here is the class DeckDatabaseHelper:
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.charsequence)’ on a null object reference
at com.example.nikolaypavlin.deck.fragments.deckmanagerfragment.DeckMamentView (deckmanagerfragment.java:nd7)
UPD.
New fragment code:
Public View Oncreater (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) GetActiveIny (). 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:
& lt; 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" & gt;
& lt; listview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id / listview"
Android: Headerdividersenabled = "false"
Android: Visibility = "Visible" / & gt;
& lt; 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" / & gt;
& lt; TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textapppearance = "? Android: attr / textappealancelarge"
Android: Text = "Large Text"
Android: id = "@ + ID / TextView2"
Android: Layout_Gravity = "Right | top" / & gt;
Answer 1, Authority 100%
Read the official documentation before doing something at random, especially since the main points are translated into Russian. At the moment you need to read the section about fragments . OnCreateView
The fragment is designed to create a root element of the fragment user interface. Those. In it, the first thing you create View
which will be displayed, and at the end you must return this view
from this method. You for why you are trying to search for your view
in the activist calling getActivity (). FindViewByid ()
. Obviously, there are no them there, and this method returns null
, but you are trying to set some values in them after which the nullpoIntexception
flies. What do you can read here .
In your case, your code should look something like this:
Public View Oncreater (LayoutInflater Inflater, ViewGroup Container,
Bundle SavedInstanceState) {
View view = inflater.inflate (R.Layout.fragment_deck_Manager, Container, False);
// We are looking for and configuring VIEW
TextView Name = (TextView) view.findViewByid (R.ID.TextView2);
/ *
The rest of the code
* /
RETURN VIEW;
}