android-can't get value of edit text in fragment [duplicate] - java

This question already has answers here:
Get Value of a Edit Text field
(13 answers)
Closed 6 years ago.
I've tried get edittext value but does not work.This is my code in android fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v=inflater.inflate(R.layout.fragment_add_patient, container, false);
final EditText FirstName=(EditText) v.findViewById(R.id.FirstName);
final Button addPatientButton=(Button) v.findViewById(R.id.AddPatientButton);
addPatientButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isOnline()) {
try {
if (FirstName.getText().toString().trim().equals("")) {
FirstName.setError("First Name is required!");
return;
}
I receive error at this line :FirstName.getText().toString().trim().equals("")
The error is following: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference.
Here is my fragment_add_patient xml file which contains all items.
<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"
tools:context="com.example.ionut.myapplication.PatientController.AddPatientFragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:id="#+id/PatientFirstName"
android:hint="First Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="27dp"
android:id="#+id/PatientLastName"
android:hint="Last Name"
android:layout_below="#+id/PatientFirstName"
android:layout_alignLeft="#+id/PatientFirstName"
android:layout_alignStart="#+id/PatientFirstName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:layout_below="#+id/PatientLastName"
android:layout_alignRight="#+id/PatientLastName"
android:layout_alignEnd="#+id/PatientLastName"
android:layout_marginTop="25dp"
android:id="#+id/Email"
android:hint="Email" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_marginTop="22dp"
android:id="#+id/PatientAge"
android:hint="Age"
android:layout_below="#+id/Email"
android:layout_alignLeft="#+id/Email"
android:layout_alignStart="#+id/Email" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:layout_below="#+id/PatientAge"
android:layout_alignLeft="#+id/PatientAge"
android:layout_alignStart="#+id/PatientAge"
android:layout_marginTop="32dp"
android:id="#+id/PatientPhone"
android:hint="Phone" />
<Button
android:text="Add Patient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AddPatientButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />
<Spinner
android:id="#+id/cspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_below="#+id/PatientPhone"
android:layout_alignRight="#+id/PatientPhone"
android:layout_alignEnd="#+id/PatientPhone"
android:layout_alignLeft="#+id/PatientPhone"
android:layout_alignStart="#+id/PatientPhone" />
Thanks in advance!.

The FirstName EditText is null because the findViewById method is trying to find an editext with the id of FirstName but the correct id is PatientFirstName
To solve the issue, replace the
final EditText FirstName=(EditText) v.findViewById(R.id.FirstName);
with
final EditText FirstName=(EditText) v.findViewById(R.id.PatientFirstName);

Simply you are trying to query of non existing id in your layout .
the actual id for your "First Name" EditText is "PatientFirstName". also try to make your code just a little bit organized as follows
1- Declare your Button , EditText variables as a global variable to be accessible anywhere inside your fragment
2- if you gonna add more buttons with click events then you should make your fragment or activity implements the View.onClickListener

Related

NullPointerException Java android studio setMovementMethod(LinkMovementMethod.getInstance())

I'm still new with coding but I have a project with android studio right now. What I wanted to do was to add hyperlinks in a page. I've followed the steps and tutorials to do this very carefully but I can't seem to find the problem. In my Logcat it says
PID: 17644 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference
but I don't understand how it was caused. I have read some similar problems stating the findByViewId might be wrong but I don't think that is the case for my problem but I would love to be wrong. Here is my code. Any help is much appreciated.
public class LearnFragment extends Fragment {
private Button search;
private TextView faq_link_1, faq_link_2, faq_link_3, faq_link_4, faq_link_5;
EditText editSearch;
public LearnFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_account, container, false);
faq_link_1 = (TextView) viewRoot.findViewById(R.id.faq1);
faq_link_2 = (TextView) viewRoot.findViewById(R.id.faq2);
faq_link_3 = (TextView) viewRoot.findViewById(R.id.faq3);
faq_link_4 = (TextView) viewRoot.findViewById(R.id.faq4);
faq_link_5 = (TextView) viewRoot.findViewById(R.id.faq5);
faq_link_1.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_2.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_3.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_4.setMovementMethod(LinkMovementMethod.getInstance());
and this is the xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".boundary.LearnFragment">
<AutoCompleteTextView
android:id="#+id/search_bar"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginTop="28dp"
android:hint="Search..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="#+id/b_search"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/b_search"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="14dp"
android:layout_weight="1"
android:text="Search"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/search_bar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/faq1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:text="#string/link_period_pain"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
android:linksClickable="true"/>
<TextView
android:id="#+id/faq2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="27dp"
android:text="#string/menstrual_cramp_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq1"
android:linksClickable="true" />
<TextView
android:id="#+id/faq3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="#string/late_period_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq2"
android:linksClickable="true" />
<TextView
android:id="#+id/faq4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="#string/cervical_screening_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq3"
android:linksClickable="true" />
<TextView
android:id="#+id/faq5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:text="#string/pap_smear_link"
android:textSize="23sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq4"
android:linksClickable="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Not looking what you're looking for? Use the search bar to find out more!"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to assign an integer value to an imageView in Android Studio?

I am creating a feedback app where a user has to click one of the 5 imageViews (1-5 rating) based on his/her experience. My primary aim is to extract the integer value of this rating from the imageView click and push it to a SQLite database.
I am trying to use setTag() and getTag() but to no avail. Any help would be much appreciated. Thanks in advance.
activity_main.xml -
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="172dp"
android:layout_marginRight="172dp"
android:layout_marginBottom="151dp"
android:text="Name"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#+id/imageView1"
app:layout_constraintEnd_toStartOf="#+id/editTextPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="172dp"
android:layout_marginLeft="172dp"
android:layout_marginTop="54dp"
android:ems="10"
android:hint="Full Name"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="#+id/textViewName"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="151dp"
android:layout_marginEnd="47dp"
android:layout_marginRight="47dp"
android:tag="1"
app:layout_constraintEnd_toStartOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewName"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginBottom="64dp"
android:tag="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView3"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="64dp"
android:tag="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.833"
app:layout_constraintStart_toEndOf="#+id/imageView4"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginBottom="63dp"
android:tag="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView1"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="53dp"
android:layout_marginLeft="53dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="49dp"
android:layout_marginRight="49dp"
android:tag="3"
app:layout_constraintEnd_toStartOf="#+id/imageView4"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="333dp"
android:layout_marginLeft="333dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="340dp"
android:layout_marginRight="340dp"
android:layout_marginBottom="75dp"
android:text="Were you satisfied with our hygiene standards?"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java -
public class MainActivity extends AppCompatActivity {
EditText name;
ImageView oneStar;
ImageView twoStar;
ImageView threeStar;
ImageView fourStar;
ImageView fiveStar;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.editTextPersonName);
oneStar = (ImageView) findViewById(R.id.imageView1);
oneStar.setTag(1);
oneStar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String username = name.getText().toString()+"\n";
Toast.makeText(MainActivity.this, (Integer) oneStar.getTag(), Toast.LENGTH_SHORT).show();
}
});
}
}
Try to replace oneStar.getTag() with view.getTag()
If you want to get the tag, and display it as a Toast message, the instead of casting it into an Integer object convert it to String using .toString method.
Second parameter of Toast.makeText is interger, but you can't pass any integer. It must be the resource Id of a string (R.string.your_string). Remove "(Integer)" and it should solve your problem.
I don't know exactly what are your requirements and in case you missed it, there is a build in rating barin Android. You can check the tutorial here
it is less code and less error prone if you just add this to ImageView: android:onClick="imageViewClick", and create a handler in MainActivity like this:
public void imageViewClick(View view) {
String username = name.getText().toString()+"\n";
Toast.makeText(MainActivity.this, (Integer)view.getTag(),Toast.LENGTH_SHORT).show();
}

Android Cursor Adapter for Custom ListView Layout - Null Pointer Exception Error [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
First post on this site, so forgive me my sins. I am attempting to create a custom ListView layout that is populated by fields from my SQLite Database Adapter class. Below is the method I tried to use for this, and it is called in the onCreate method of its class, as well as the onClick method of a button to save to the Database:
//Method to re-populate custom list view songlist_layout when a new entry is added
private void populateSongList() {
//Cursor to navigate through records of the database
Cursor cursor = myDb.getAllRows();
//Need two arrays to work with the Cursor. First is from field names
String [] fromFieldNames = new String[] {DBAdapter.KEY_ROWID,
DBAdapter.KEY_SONGTITLE,
DBAdapter.KEY_SONGDURATION};
//Second is int array
int [] toViewIDs = new int [] {R.id.textViewSongNumber, R.id.textViewSongName,
R.id.textViewSongDuration};
//Cursor Adapter Object
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.songlist_layout, cursor, fromFieldNames, toViewIDs,0);
//Need to grab ListView from activity_add_song to set the adapter to it
ListView songList = (ListView)findViewById(R.id.songsListView);
songList.setAdapter(myCursorAdapter);
}
//Method to handle the click event of the Save button, adding the data into the database
public void onClickSaveSong (View v) {
//Song Title and duration are essential fields, so we want to check if they
// have text before saving to the database
if(!TextUtils.isEmpty(etSongTitle.getText().toString()) &&
!TextUtils.isEmpty(etSongDuration.getText().toString())) {
myDb.insertRow(etSongTitle.getText().toString(),
etSongKey.getText().toString(),
etSongTuning.getText().toString(),
etSongDuration.getText().toString());
//Pop-up to inform user the Data has been saved
Toast.makeText(getBaseContext(), "Song Added!", Toast.LENGTH_LONG).show();
}//if
//Otherwise a pop-up to tell the user to enter the essential info
else {Toast.makeText(getBaseContext(), "Enter Title and Duration",
Toast.LENGTH_LONG).show();}
//Call to repopulate songs ListView
populateSongList();
}//onClickSaveSong()
The custom XML Layout for the ListView contains three TextViews to hold the songNumber, the songName and the songDuration:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textViewSongNumber"
android:paddingRight="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textViewSongName"
android:paddingRight="60dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textViewSongDuration"
android:paddingRight="70dp"/>
I have read elsewhere that the problem could be because the ListView is grabbing from the wrong ListView ID
ListView songList = (ListView)findViewById(R.id.songsListView);
However comparing it to the XML Layout for the ListView, I don't see how this would be the case:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/songsListView"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_above="#+id/addSongButton" />
Finally, the logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blob.gigstrofinal/com.blob.gigstrofinal.AddSong}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.blob.gigstrofinal.AddSong.populateSongList(AddSong.java:121)
at com.blob.gigstrofinal.AddSong.onCreate(AddSong.java:45)
I cannot figure this out. I haven't been using Android for long, and a lot of this is over my head, so I would appreciate any help at all on this matter. The App is for a University Project, and it is due next week!
EDIT: I forgot to specify, the Null Pointer Exception is pointing to line 121 of AddSong.java which is: songList.setAdapter(myCursorAdapter);
I am calling the populateSongList()method from the onCreate method in the AddSong class:
public class AddSong extends Activity {
//Declare Database Adapter
DBAdapter myDb;
//Declare EditText objects to be used for each field in the SQLite Database
EditText etSongTitle;
EditText etSongKey;
EditText etSongTuning;
EditText etSongDuration;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_song);
//Initialise EditText objects by assigning them to the corresponding ID
etSongTitle = (EditText)findViewById(R.id.editSongTitle);
etSongKey = (EditText)findViewById(R.id.editSongKey);
etSongTuning = (EditText)findViewById(R.id.editSongTuning);
etSongDuration = (EditText)findViewById(R.id.editSongDuration);
//Call method to open Database
openDB();
//Call to populate songs ListView
populateSongList();
}
and again for the onClickSaveSong method of AddSong.java (see second code snippet of original post.
EDIT2: The full contents of activity_add_song.xml:
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.blob.gigstrofinal.AddSong"
android:background="#color/colorPrimary"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvSongTitle"
android:id="#+id/songTitle"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textStyle="bold"
android:textSize="13sp"
android:textColor="#color/colorPrimary2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/songTitle"
android:id="#+id/editSongTitle"
android:layout_alignParentStart="true"
android:editable="true"
android:inputType="text"
android:background="#e6e6e6"
android:textColor="#color/colorPrimary3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvKey"
android:id="#+id/songKey"
android:textStyle="bold"
android:textSize="13sp"
android:textColor="#color/colorPrimary2"
android:layout_marginTop="15dp"
android:layout_below="#+id/editSongTitle"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editSongKey"
android:layout_below="#+id/songKey"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/editSongTitle"
android:background="#e6e6e6"
android:inputType="text"
android:textColor="#color/colorPrimary3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvTuning"
android:id="#+id/songTuning"
android:textStyle="bold"
android:textSize="13sp"
android:textColor="#color/colorPrimary2"
android:layout_marginTop="13dp"
android:layout_below="#+id/editSongKey"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editSongTuning"
android:layout_below="#+id/songTuning"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/editSongKey"
android:background="#e6e6e6"
android:textColor="#color/colorPrimary3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvDuration"
android:id="#+id/songDuration"
android:textStyle="bold"
android:textSize="13dp"
android:textColor="#color/colorPrimary2"
android:layout_marginTop="15dp"
android:layout_below="#+id/editSongTuning"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editSongDuration"
android:layout_below="#+id/songDuration"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/editSongTuning"
android:background="#e6e6e6"
android:textColor="#color/colorPrimary3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvLyrics"
android:id="#+id/songLyrics"
android:textStyle="bold"
android:textSize="13sp"
android:textColor="#color/colorPrimary2"
android:layout_below="#+id/editSongDuration"
android:layout_alignParentStart="true"
android:layout_marginTop="15sp"
android:textIsSelectable="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editSongLyrics"
android:layout_below="#+id/songLyrics"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/editSongDuration"
android:background="#e6e6e6"
android:layout_above="#+id/saveButton"
android:layout_marginBottom="20dp"
android:textColor="#color/colorPrimary3"/>
<Button
android:id="#+id/saveButton"
android:text="#string/buttonSaveText"
android:background="#color/colorPrimary"
android:textColor="#color/colorPrimary2"
android:textStyle="bold"
android:textSize="11sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="22dp"
android:onClick="onClickSaveSong"
android:layout_alignParentBottom="true"
/>
<Button
android:id="#+id/backButton"
android:text="#string/backButtonText"
android:background="#color/colorPrimary"
android:textColor="#color/colorPrimary2"
android:textStyle="bold"
android:textSize="11sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickBack"
android:layout_alignBottom="#+id/saveButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="19dp"
/>
</RelativeLayout>
It is perhaps worth mentioning that the ListView itself is not located in Activity_add_song, it is in another Activity activity_songs. Would that cause a null pointer exception?
The problem is that the ListView is not in the layout xml that you're inflating in this Activity.
When you call this in onCreate():
setContentView(R.layout.activity_add_song);
You're inflating the activity_add_song.xml layout xml to the current Window.
Then when you call:
ListView songList = (ListView)findViewById(R.id.songsListView);
It's looking at the currently inflated layout for a ListView with ID songsListView. Since there is no songsListView in the currently inflated layout (activity_add_song.xml), it will be null.
Move all code dealing with songsListView to the Activity that inflates the layout with the ListView.
Make sure you call populateSongList() after setContentView().
And please be more specific about where the line 121 is.
Caused by: java.lang.NullPointerException
at com.blob.gigstrofinal.AddSong.populateSongList(AddSong.java:121)
Since line 121 is this:
songList.setAdapter(myCursorAdapter);
and that is where you are getting null, then either songList or myCursorAdapter is null. myCursorAdapter is clearly not null, so you likely have the wrong name for your findViewById method in this line:
ListView songList = (ListView)findViewById(R.id.songsListView);
Check that the listview is actually called "songsListView" in your XML - it probably isn't.
Your activity_add_song.xml should contain the ListView, which is missing in your code.
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/songsListView"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_above="#+id/addSongButton" />
and R.layout.songlist_layout should contain the layout for each row
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textViewSongNumber"
android:paddingRight="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textViewSongName"
android:paddingRight="60dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textViewSongDuration"
android:paddingRight="70dp"/>

I just cant see why edittext does not appear on checking a radio button

I have this XML which i using for my Java activity in Android. I want the contact number edittext to become visible when user selects female as a gender but it stays invisible when male is selected.
Code is in its primary stage but I want this functionality to work.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<EditText
android:id="#+id/editText3"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/contact"
android:inputType="phone"
android:visibility="invisible"/>
<EditText
android:id="#+id/editText1"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/entername" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="251dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enteruser" />
<EditText
android:id="#+id/editText5"
android:layout_width="258dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/age" />
<EditText
android:id="#+id/editText4"
android:layout_width="249dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/pswrd" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="101dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/male" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/female" />
</RadioGroup>
</LinearLayout>
</ScrollView>
I am trying to use this XML and make the contact number visible only when female radio button is selected.
Java file is:
public class RegistrationActivity extends Activity implements RadioGroup.OnCheckedChangeListener{
private RadioGroup radioGroup1;
private EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
//test listening to radio group
radioGroup1.setOnCheckedChangeListener(this);
et=(EditText)findViewById(R.id.editText5);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radio1){
et.setVisibility(View.VISIBLE);
}
}
You call
et=(EditText)findViewById(R.id.editText5);
but editText5 is visible in your XML Layout , i.e.,
<EditText
android:id="#+id/editText5"
android:layout_width="258dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/age" />
So please call the invisible editText3 and I think then it will work for you. So replace your code
et=(EditText)findViewById(R.id.editText5);
by this one
et=(EditText)findViewById(R.id.editText3);
your editText3 is
<EditText
android:id="#+id/editText3"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/contact"
android:inputType="phone"
android:visibility="invisible"/>
A little funny.
Your edittext3 is invisible and edittext5 is visible form start.
<EditText
android:id="#+id/editText3"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/contact"
android:inputType="phone"
android:visibility="invisible"/>
In your code you try to visible edittext5 which is already visible.
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radio1){
et.setVisibility(View.VISIBLE);
}
}
Just change et=(EditText)findViewById(R.id.editText5); to et=(EditText)findViewById(R.id.editText3);
Good programmings.. :)
You are getting the wrong edittext id in onCreate. The phone edittext is 3. Hope it helps.

Having trouble with findViewById - returning null

I have the activity_main XML like this:
<LinearLayout ...>
<EditText android.id="#+id/edit_text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button android.id="#+id/my_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
The main java codes are:
public class MainActivity extends Activity {
void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.edit_text);
if (editText == null)
Log.v("editText", "null editText");
else
Log.v("editText", "good editText");
}
}
The display is correct and I can see the EditText and Button fields. However, I always got NULL for editText. I read the previous problems. Most of them were due to the fact that setContentView(R.layout.active_main) was not used. But I can't find out why I am having problem with my codes. Any help is greatly appreciated.
Your XML has problem
android:id not android.id
You were using . instead of :. So please make your correction as below:
<LinearLayout ...>
<EditText android:id="#+id/edit_text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button android:id="#+id/my_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>

Categories

Resources