Displaying String on textview is not working - java

I'm new to android studio, bear with any misunderstandings on my part. I want to display a string of musical lyrics to a text view called lyrics_view which resides in my fragment_lyrics.xml file. I've looked around a lot and followed some similar examples but am not having any luck solving my issue. Here is some of my code. When I hit the play button, a song plays via the Spotify api and I also want to display those lyrics to lyrics_view using the musixMatch api. I can provide more code if needed. Am I even going about displaying this string correctly? Or is there a completely different way I should be doing it? Thank you for making effort with a long post.
playSongButton() which is in my MainActivity class. This is responsible for playing songs and making a call to a calculateLyrics() method.
public void playSongButton(View view) {
try {
mPlayer.playUri(null, "spotify:track:15vzANxN8G9wWfwAJLLMCg", 0, 0);
//where i believe i am having issues
TextView textView = null;
TextView displayTextView = (TextView) findViewById(R.id.lyrics_view);
String lyricString = songLyrics.calculateLyrics();
displayTextView.setText(lyricString);
} catch (Exception e) {
e.printStackTrace();
}
}
CalculateLyrics() which is in SongLyrics class
public SongLyrics(String songName, String artistName) {
this.songName = "Paris";
this.artistName = "The Chainsmokers";
}
public String calculateLyrics() {
try {
musixMatch = new MusixMatch(key.getKey());
track = musixMatch.getMatchingTrack(songName, artistName);
trackData = track.getTrack();
trackId = trackData.getTrackId();
lyrics = musixMatch.getLyrics(trackId);
lyricString = lyrics.getLyricsBody();
} catch (Exception e) {
e.printStackTrace();
}
return lyricString;
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:orientation="horizontal"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0">
<EditText
android:id="#+id/songName"
android:layout_width="0dp"
android:layout_height="56dp"
android:ems="10"
android:hint="#string/enter_song"
android:inputType="text"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="6dp" />
<EditText
android:id="#+id/artistName"
android:layout_width="0dp"
android:layout_height="55dp"
android:ems="10"
android:hint="#string/enter_artist"
android:inputType="text"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Display1"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintTop_toBottomOf="#+id/songName" />
<TextView
android:text="Enter Song Info"
android:layout_width="wrap_content"
android:layout_height="69dp"
android:id="#+id/enterSongInfo"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="80dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="80dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="1.0"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/playButton"
android:onClick="playSongButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pauseButton"
android:onClick="pauseSongButton"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/playButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp" />
<fragment
android:id="#+id/lyrics"
android:layout_width="353dp"
android:layout_height="329dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginTop="208dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.71"
class="com.example.joeberg.jams.LyricsFragment"/>
/>
</android.support.constraint.ConstraintLayout>
fragment_lyrics.xml
<android.support.constraint.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:text="Lyrics"
android:layout_width="93dp"
android:layout_height="59dp"
android:id="#+id/lyrics_heading"
class="com.example.joeberg.jams.LyricsFragment"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="280dp"
android:textColor="#color/common_google_signin_btn_text_dark_focused" />
<TextView
class="com.example.joeberg.jams.LyricsFragment"
android:layout_width="335dp"
android:layout_height="271dp"
android:id="#+id/lyrics_view"
app:layout_constraintTop_toBottomOf="#+id/lyrics_heading"
android:textAppearance="#style/TextAppearance.AppCompat"
android:layout_marginStart="32dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="32dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintHorizontal_bias="0.52" />
</android.support.constraint.ConstraintLayout>

You need to filter the results of your query to musixmatch. When I run a curl command with artist The Chainsmokers and track Paris you recieve 20 tracks returned from musixmatch (mostly club remixes). You have to write some code to find/filter the results to the one you want to display using the filter parameters mentioned in user documentation (f_ input parameters)
curl:
curl --url "http://api.musixmatch.com/ws/1.1/track.search?q_artist=The%20Chainsmokers&q_track=Paris&apikey
partial output:
{"message":{"header":{"status_code":200,"execute_time":0.019639015197754,"available":20},"body":{"track_list":[{"track":{"track_id":173322884,"track_name":"Paris (Mixed)","track_name_translation_list":[],"track_rating":1,"commontrack_id":96669304,"instrumental":0,"explicit":0,"has_lyrics":0,"has_subtitles":0,"has_richsync":0,"num_favourite":0,"album_id":32901374,"album_name":"Let's Dance, Vol. 2 (DJ Mix)","artist_id":26490468,"artist_name":"The Chainsmokers","track_share_url":"https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed?utm_source=application&utm_campaign=api&utm_medium=","track_edit_url":"https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed/edit?utm_source=application&utm_campaign=api&utm_medium=","restricted":0,"updated_time":"2019-06-12T15:47:28Z","primary_genres":{"music_genre_list":[]}}},{"track":{"track_id":126251071,"track_name":"Paris (Sbm X Geru Remix)","track_name_translation_list":[],"track_rating":5,"commontrack_id":69979021,"instrumental":0,"explicit":0,"has_lyrics":1,"has_subtitles":0,"has_richs

Related

Nested Spinner Android Design Issues ConstraintLayout

I'm working with spinner for the first time, and I don't understand why the second spinner doesn't look exactly like the first one even though they were created practically the same (the only difference is the data). These are nested.
The design implemented it with "constraint layout"
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/categorieText"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spProduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/productText"
app:layout_constraintTop_toBottomOf="#+id/spCategorie" />
This is how it looks in the emulator
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, productsCat);
arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
categorie.setAdapter(arrayAdapter);
categorie.setOnItemSelectedListener(new SpinnersEvents());
product.setOnItemSelectedListener(new SpinnersEvents());
private class SpinnersEvents implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spCategorie){
String[] productsName = getProductName(productsCat[position]);
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
arrayAdapterChild.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
product.setAdapter(arrayAdapterChild);
}else{
price.setText(String.valueOf(tempList.get(position).getPrice()));
imgPrd.setImageResource(tempList.get(position).getImage());
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Please try this in your code may help you, you can also set wrapcontent in spinner or increase the size to view text proper
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints">
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlCompat" />
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
<TextView
android:id="#+id/productText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="Product:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/categorieText"/>
<Spinner
android:id="#+id/spproduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="#+id/spCategorie"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The solution was in the code, not in the layout.
The second spinner must be:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, productsName);
instead:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
In this case, calling the getBaseContext() method generated that particular problem.
Sorry for the confusion.

How can I change the color of my edit text which is entered by user?

I have created a signup page and I select the hint color blue now I want to change the color of text which is entered in the edit text please tell me how I can change the color of text
Here is the code of my 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Signup"
android:background="#color/purple_500">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="660dp"
android:layout_marginTop="80dp"
android:background="#drawable/linear_round"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Please Signup..."
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="29sp"
android:gravity="center"
android:layout_marginTop="16dp"
android:id="#+id/textView">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#color/black"
android:hint="Enter your name"
android:layout_marginTop="250dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:textColorHint="#color/purple_500"
android:id="#+id/name"/>
<EditText
android:id="#+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your password"
android:inputType="textPassword"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/name" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your valid e-mail"
android:inputType="textEmailAddress"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pass" />
<EditText
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your cell number"
android:inputType="phone"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/submit"
android:layout_width="445dp"
android:layout_height="163dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/number"
android:layout_marginBottom="100dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/submit" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/trial"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/trial" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/signup_animation"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toTopOf="#+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="70dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/signup" />
</androidx.constraintlayout.widget.ConstraintLayout>
And how can I add the marquee effect on my text view? Can I make text view scrolling from right to left?
If TextView fill whole width you need to add these lines of code
<TextView
.
.
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
also do not forget to set selected for TexrtView as below
textview.setSelected(true);
and if TextView does not fill width the only thing is to call this method and pass TextView to it.
Kotlin
fun addMarquee(textView: TextView) {
textView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val pixels = textView.measuredWidth - 1
val params = textView.layoutParams
params.width = pixels
textView.layoutParams = params
textView.isSelected = true
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSingleLine = true
textView.marqueeRepeatLimit = -1
textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
Java
public void addMarquee(TextView textView) {
textView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int pixels = textView.getMeasuredWidth() - 1;
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.width = pixels;
textView.setLayoutParams(params);
textView.setSelected(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine(true);
textView.setMarqueeRepeatLimit(-1);
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
NOTE: this method only work when textView width is wrap_content.
textColor attribute changes color of text which user entered in.
android:textColor="#color/black"
HorizontalScrollView has one chid view, and it can be scrolled horizontally.
There's many ways to move view.
Search with keyword like
move view animation android or move view programmatically android

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>

Attempt to invoke ListView.setAdapter on a null object reference

I am trying to show am Integer Arraylist on a ListView. But I fail due to a null object error. My object is not null. Basically I am trying to make a scoreboard for 2 teams.
Here is my class:
public class PointsList extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.points_list);
Bundle b;
b = getIntent().getExtras();
ArrayList<Integer> scoreA = new ArrayList<Integer>();
ArrayList<Integer> scoreB = new ArrayList<Integer>();
ListView ListA, ListB;
scoreA = b.getIntegerArrayList("scoreA");
scoreB = b.getIntegerArrayList("scoreB");
ArrayAdapter arrayAdapterA = new ArrayAdapter<Integer>(
this, R.layout.points_list, R.id.scoreAtext, scoreA );
ArrayAdapter arrayAdapterB = new ArrayAdapter<Integer>(
this, R.layout.points_list, R.id.scoreBtext, scoreB );
ListA = (ListView) findViewById(R.id.scoreAList);
ListB = (ListView) findViewById(R.id.scoreBList);
ListA.setAdapter(arrayAdapterA);
ListB.setAdapter(arrayAdapterB);
}
}
And here is my 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/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:fillViewport="true">
<ListView
android:id="#+id/scoreBList"
android:layout_width="189dp"
android:layout_height="571dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.927"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.593" >
<TextView
android:id="#+id/scoreBtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ListView>
<ImageView
android:id="#+id/imageView2"
style="#style/custom_image"
android:layout_width="376dp"
android:layout_height="619dp"
android:background="#drawable/dragon"
android:contentDescription="#string/logoDescription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/teamNameA"
android:layout_width="150dp"
android:layout_height="62dp"
android:autoSizeTextType="none"
android:background="#drawable/red_button"
android:gravity="center_horizontal|center_vertical"
android:inputType="textShortMessage"
android:maxLines="1"
android:singleLine="true"
android:text="#string/team_a"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.103"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<TextView
android:id="#+id/teamNameB"
android:layout_width="150dp"
android:layout_height="62dp"
android:autoSizeTextType="none"
android:background="#drawable/red_button"
android:gravity="center_horizontal|center_vertical"
android:inputType="textShortMessage"
android:maxLines="1"
android:singleLine="true"
android:text="#string/team_b"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<View
android:id="#+id/divider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="#+id/scoreAList"
android:layout_width="189dp"
android:layout_height="571dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.072"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.593" >
<TextView
android:id="#+id/scoreAtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried entering just a blank xml with a textview as I saw on other threads that this is what ArrayAdapter expects. But failed. Any help is greatly appreciated!

Make my custom view update text fields in editor

So im currently working on a custom card view and my goal is pretty easy. I want to update the textfield to the string I set in the custom card view xml.
But somehow I can't get it working after many hours of trying. Sometimes the editor won't load my xml with the custom card view... sometimes it gives me a extremely long error message... and sometimes it just stays black...
Therefore I have the following xml file :
<LinearLayout 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/CallEntry_Holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="0">
<com.test.larsmatthaus.projectnurser.customs.Custom_CardView
android:id="#+id/Call_Entry"
android:layout_width="match_parent"
android:layout_height="50dp"
app:cardBackgroundColor="#color/colorCardBackgroundR"
app:cardCornerRadius="5dp"
app:personName="test"
app:room="100"
app:ward="test"
tools:layout_editor_absoluteX="1024dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="#+id/WardName"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="200dp"
android:layout_marginLeft="200dp"
android:layout_marginRight="200dp"
android:layout_marginStart="200dp"
android:layout_marginTop="10dp"
android:autoSizeTextType="uniform"
android:fontFamily="#font/comfortaa_light"
android:gravity="center"
android:text="Louisa Burgess"
android:textColor="#color/colorOverlayTC"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/icon_2"
android:layout_width="31dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/icon_1"
app:layout_constraintHorizontal_bias="0.85"
app:layout_constraintStart_toEndOf="#+id/WardName"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/teddybear" />
<ImageView
android:id="#+id/icon_1"
android:layout_width="31dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toEndOf="#+id/WardName"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/bed" />
<TextView
android:id="#+id/RoomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="#font/comfortaa_light"
android:gravity="center"
android:text="105"
android:textColor="#color/colorOverlayTC"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/WardName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/seperator_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/comfortaa_light"
android:text="/"
android:textColor="#color/colorOverlayTC"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/RoomText"
app:layout_constraintEnd_toStartOf="#+id/WardName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/RoomText"
app:layout_constraintTop_toTopOf="#+id/RoomText" />
<TextView
android:id="#+id/WardText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/anaheim"
android:gravity="center"
android:text="Ward 2"
android:textColor="#color/colorOverlayTC"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/seperator_C"
app:layout_constraintEnd_toStartOf="#+id/WardName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/seperator_C"
app:layout_constraintTop_toTopOf="#+id/seperator_C" />
</android.support.constraint.ConstraintLayout>
</com.test.larsmatthaus.projectnurser.customs.Custom_CardView>
It contains just one card view entry.
My custom card view class looks like this :
public class Custom_CardView{
public Context context;
public Map<Integer, Object> vars = new HashMap<Integer, Object>();
public Custom_CardView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
setWillNotDraw(false);
View view = getRootView();
for(int index = 0; index<((ViewGroup)view).getChildCount(); ++index) {
View nextChild = ((ViewGroup)view).getChildAt(index);
vars.put(nextChild.getId(), nextChild);
}
Log.println(Log.DEBUG,"Custom_CardView","<Custom_CardView : Instantiated => ["+vars.size()+"] children>");
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.elements_call_entry, this, true);
TextView wardText = (TextView) findViewById(R.id.WardName);
wardText.setText("TEST");
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
}
}
I want to update the TextView "wardName" to the attribute value i did set in the xml file...
But somehow the editor view crashes once I load it... Due to the Custom CardView Class... Theres no real error log...
The only one I received was this : Preview timed out while rendering the layout. This typically happens when there is an infinite loop or unbounded recursion in one of the custom views
What did I forgot ? What else could I try to modify the editors textview ?
Well it was pretty easy... just added a new layout activity which will inflate once the CardView is pressed. I just copy the CardView element and place it inside the inflated view => problem solved :)

Categories

Resources