I have a TextView in my app and I want to change its textSize programmatically using the setTextSize method, but everytime I ran my code, I got an error message : "java.lang.NullPointerException". I tried to print the value of the TextView and it got NULL value.
Here's my code:
LoadActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text);
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
setContentView(R.layout.activity_load);
}
}
activity_load.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please Wait"
android:id="#+id/loadTextView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:typeface="monospace"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="#000000"/>
</RelativeLayout>
What do you think is the problem?
setContentView(R.layout.activity_load); should be declared before initializing your views(TextView) and System.out.println("TextView: "+text); is not correct form to print value. The correct form is System.out.println("TextView: "+text.getText().toString());
So your corrected code will be like this
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}
Use this in your onCreate Method
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
text.setTextSize(30);
In order for you to find views by their id, you need to write
setContentView(R.layout.activity_load);
This is because it essentially gets the layout resource file that defines your UI, and then allows you to retrieve other views within that layout (with findViewById();)
Look here for more information on how to properly set up Activites
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load); // Retrieves layout that defines your UI
// After setContentView is called, you can find views within the layout
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}
Related
MainActivity.java file
package com.example.sunshineexample_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
TextView toyListText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toyListText = findViewById(R.id.toys_list);
String[] toyNames = ToyBox.getToyNames();
for(String Toyname : toyNames){
toyListText.append(Toyname + "\n");
}
}
}
In for each loop, the line toyListText.append(Toyname + "\n"); , does it create a new textview for every ToyName(There are about 30 toys in String[] toysName) or just extends the size of existing textview. In xml file I have created a framelayout container view and a single textView.
Xml file
<TextView
android:id="#+id/toys_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:textSize="25sp" />
No. It creates a new String, but not a new TextView. Nothing but the call to inflate creates a TextView. However, nothing in your code is updating the textview so you'll never see the changes- to see the text you'd need to call textView.setText(toyListText) after you're done generating it.
I want to make a clock widget where in from its app user can set the textColor according to their wish.
So I want to change the TextClock's text color dynamically and programmatically on users click.
I tried using the simple setTextColor() method but there is no such method for setting text color of widget.
Below is the code I tried but it crashed the app on user click(touch).
I expect that the color of text changes on user's touch(that can be on any Button or ImageView).
package com.example.demowidget;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextClock;
public class MainActivity extends AppCompatActivity {
TextClock tctClock;
ImageView imgIcon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgIcon = findViewById(R.id.imgIcon);
tctClock = findViewById(R.id.tctClock);
imgIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tctClock.getBackground().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/widget_margin">
<TextClock
android:id="#+id/tctClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/widget_back"
android:fontFamily="#font/lato"
android:padding="10dp"
android:textColor="#11AB6B"
android:textSize="36sp" />
</RelativeLayout>
you can try for this..
first add you color in color.xml file
<color name="your_color">#094D92</color>
and replace this line--
tctClock.setTextColor(Color.parseColor("#094D92"));
to
tctClock.setTextColor(getResources().getColor(R.color.your_color))
I want to select multiple items in spinner but getting runtime exception due to java.lang.CastException in XML file
XML:
<com.example.lenovo.abc.MultiSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hobby"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_weight="0.91"
/>
MultiSpinner class:
Android Spinner with multiple choice
About class
package com.example.lenovo.abc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
TextView intro= (TextView) findViewById(R.id.introducing);
MultiSpinner hobby= (MultiSpinner) findViewById(R.id.hobby);
List<String> h = Arrays.asList(getResources().getStringArray(R.array.hobby));
hobby.setItems(h,"Hobbies", (MultiSpinner.MultiSpinnerListener) this);
}
}
hobby.setItems(h,"Hobbies", (MultiSpinner.MultiSpinnerListener) this);
You are not implementing MultiSpinner.MultiSpinnerListener in this activity and passing context casting to MultiSpinner.MultiSpinnerListener. Try implementing the listener in this activity.
I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.
I'd like to use the ImageViewTopCrop class described in this question:
ImageView scaling TOP_CROP
My custom view is based on this code:
Custom Image View class
Here is a layout file using this custom view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_background"
android:background="#000"
android:orientation="vertical">
<FrameLayout
android:id="#+id/details_frame"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6">
<com.grayraven.imagetest.ImageViewTopCrop
android:id="#+id/grid_item_image"
android:src="#drawable/hobbit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
Here's how I'm trying to display it in code:
com.grayraven.imagetest;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class DetailsActivity extends ActionBarActivity {
private TextView titleTextView;
private com.grayraven.imagetest.ImageViewTopCrop imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_view);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
String title = getIntent().getStringExtra("title");
String imageUrl = getIntent().getStringExtra("imageUrl");
titleTextView = (TextView) findViewById(R.id.title);
imageView = (com.grayraven.project1.ImageViewTopCrop) findViewById(R.id.grid_item_image);
//Exception thrown here:
//android.widget.ImageView cannot be cast to com.grayraven.imagetest.ImageViewTopCrop
//can't go any further
}
}
What simple thing am I missing?
I think you are type casting with wrong class.
You used
com.grayraven.imagetest.ImageViewTopCrop
in your xml but type casting with com.grayraven.project1.ImageViewTopCrop
I was missing a constructor in my custom view class. Never mind.