I'm trying to make a mini mobile wallet which stores users' details on firebase. I'm getting the error
Attempt to invoke virtual method 'void
android.widget.TextView.setText(int)' on a null object reference
while using the setText() method to show user's balance. I've referred to many other similar questions on stackoverflow, but nothing helped me out.
Here are code files I think I have problem with.
The below code if for a fragment (wallet_info.xml)
<?xml version="1.0" encoding="utf-8"?>
<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=".PassBook">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_marginTop="250dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="25dp"
android:id="#+id/walletText"
android:text="Wallet Balance"/>
<TextView
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="40dp"
android:layout_below="#id/walletText"
android:id="#+id/balanceText"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/balanceText"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Request Balance"
android:onClick="onRequest"/>
</RelativeLayout>
The android:onClick="onRequest" shows a red underline asking me to create a public method even though I've created the method in the class on which the fragment is attached.
The below code is for home.java (a class on which wallet_info.xml fragment is attached.) I'm pasting the part where I'm getting the error.
public void onRequest(View view){
tv = (TextView)findViewById(R.id.balanceText);
wallet.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
tv.setText(u.getBalance());
//tv.setText(u.getBalance());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
The line tv.setText(u.getBalance()); throws the mentioned error.
Thank you in advance.
This is because you can't use the following attribute:
android:onClick="onRequest"
inside a layout for a Fragment. It's only work for an Activity. Hence, you need to bind the balanceText view and create the View.onClickListener for button2 button.
Though questions like this on stackoverflow didn't have the answer I needed, I came up with the solution. I don't know why it shows a NULL POINTER EXCEPTION even when I created an object of TextView. The error got solved by changing the data type of parameter in setText() to string.
The problem got resolved after changing the code as follows.
wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
tv.setText(u.getBalance());
The above lines of code were changed to -
wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
g = String.valueOf(u.getBalance());
tv.setText(g);
This solved my problem.
Related
I know this has been posted countless times and im sorry but I have tried to follow the solutions but can't seem to fix it.
Basically I'm trying to read records from Android SQLite Database, but I keep getting the error at setText.
public class Landing extends AppCompatActivity{
public Button buttonProducts;
public void countRecords(){
int recordCount = new TableControllerAppointments(this).count();
ScrollView textViewRecordCount = findViewById(R.id.textViewRecordCount);
textViewRecordCount.setText(recordCount + " records found.");
}
I'm following a tutorial and I added this code to my XML file,
<ScrollView
android:id="#+id/textViewRecordCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:padding="1dp"
android:text="#string/textViewRecordCount"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.182"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/buttonCreateAppointment"
app:layout_constraintVertical_bias="0.146" >
<LinearLayout
android:id="#+id/linearLayoutRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Sorry if im not posting right this is my first time. I think its something to do with my ScrollView but not sure, any help would be much appreciated
Thanks
How to resolve method 'setText(java.lang.String)
FYI
setText() is method of textview not a method of Scrollview because ScrollView does not have any setText()
You can not use as Scrollview.setText("Nilesh");
textview.setText("Nilesh");
It must be like this I hope
public Button buttonProducts;
public void countRecords(){
int recordCount = new TableControllerAppointments(this).count();
TextView textViewRecordCount = findViewById(R.id.textViewRecordCount);
textViewRecordCount.setText(String.ValueOf(recordCount) + " records found.");
}
You can't set text to Scrollview, first you have to understand this, it's basic
You're trying to set text for "Scrollview", which is absolutely wrong. Scroll view is a view which is using for scroll the layout while the application runs. to full fill your requirement you have to declare a TextView. Then you can set the text for it.
I am trying to set the visibility of a Textview depending on the response returned by Retrofit . onFailure, I am setting the visibility to visible tv_no_cat.setVisibility(View.VISIBLE); However, this does not work. I am doing the same with a ContentLoadingProgressBar and it's working appropriately. Here is my code
public void onFailure(Call<List<MoviesCategory>> call, Throwable t) {
tv_no_cat.setVisibility(View.VISIBLE); ////This does not work
Boolean x = tv_no_cat.getVisibility() == View.VISIBLE;
Toast.makeText(getContext(), "Network Error "+x, Toast.LENGTH_LONG).show(); //This shows true
tv_no_cat.setTextColor(Color.BLUE);
videoLoadingPb.setVisibility(View.GONE); //This works
}
My guess is that am trying to set the textview which is on UI thread from another thread. If so, why is it working for the ContentLoadingProgressBar?
Here is my xml for the 2 views
<android.support.v4.widget.ContentLoadingProgressBar
android:id="#+id/loading_categories"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:visibility="visible" />
<TextView
android:layout_below="#id/loading_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Movie Category Found"
android:id="#+id/tv_no_cat"
android:textAlignment="center"
android:layout_gravity="bottom"
android:visibility="gone"
/>
Any help on how I can set the textview Visibility to VISIBLE is welcome.
From the docs:
On Android, callbacks will be executed on the main thread...
So this is not your issue, and I'm doubtful that it's related to Retrofit, especially when it works for another view. Please make sure that there are no problem in your xml design, and that there are no other methods that affect this view's visibility.
are registering Textview inside Oncreate or some where else ?
And this code you are using inside activity of fragment ?
Please check this ...
tv_no_cat=(TextView)findViewById(R.id.tv_no_cat);(Inside Activity)
tv_no_cat=(TextView)rootView.findViewById(R.id.tv_no_cat);(Inside Fragment )
I have got an idea to get rid of some coding when we do initializion of views.
When we create an xml layout in android. At that movement same name class is created with UpperCase Letters with a dialogue with permission. And as i created views with ids. It should create Views and initialize them automatically.
for e.g
close.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/close_img" />
<TextView
android:id="#+id/closeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing Text" />
</LinearLayout>
And automatically generated java code should be.
public class Close extends Activity
{
TextView CloseText;
Button CloseBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseText = (TextView)findViewById(R.id.closeText);
CloseBtn = (Button)findViewById(R.id.closeBtn);
}
}
I can read xml and do the other stuff as I explained. But how could i add this module to eclipse and How could i create a service which work in Background all the time in eclipse.
Suggest me how to add this module(Plugin) to eclipse and i am going to use JAXB to generate Java Objects from XML document. Is there any better option.
Here in this website i find that just paste the xml and you will get your java code ready for activity class.
i had attached the link
I am new to android development, and have been trying to use the beginner's tutorial as my starting point for developing a simple app. There is one screen, with an image, a row of four buttons, a textbox for the user to enter a pin, and a textview to display results.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/skytrek"
/>
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_today"
android:onClick="today_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_tomorrow"
android:onClick="tomorrow_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_this_week"
android:onClick="this_week_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_next_week"
android:onClick="next_week_click" />
</LinearLayout>
<EditText android:id="#+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/edit_message" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/init_message"/>
</LinearLayout>
I have code to deliver the results I want based on the button pressed - all fine. But then I wanted the user to enter a PIN as well as pressing a button, so I added the EditText control. This threw the following error:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
My Java class:
private String content = null;
private TextView textView1;
private EditText editText1;
public void today_click(View view) {
getPage("today");
}
public void tomorrow_click(View view) {
getPage("tomorrow");
}
public void this_week_click(View view) {
getPage("thisweek");
}
public void next_week_click(View view) {
getPage("nextweek");
}
public void getPage(String strParam) {
editText1 = (EditText) findViewById(R.id.editText1);
String message = editText1.getText().toString();
if (message.equals("4567")) {
content = "PIN recognised";
} else {
content = "PIN not recognised";
}
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(content);
}
I thought I had done something silly, using the name of a TextView instead of an EditText control, but I can't find it if I have.
The error is being thrown at the line
getPage("thisweek");
I didn't understand how this line involved views of any sort, but of course the function heading
this_week_click(View view)
does, and when I changed the order of the TextView and the EditText in the XML file (so that the TextView comes first), the error disappeared. It is as if the "view" being passed is not the button, but the nearest widget to the button. I have read
existence of parameter (View view)
but it only seems to confirm my (mis)understanding that a button should be passed as the view parameter. I have also tried cleaning the project, and building a completely new project. What on earth is causing the casting error?
If you're using Eclipse, go to the menu voice "Project" and select "Clean"
Sometimes Eclipse has some problem with ids, by cleaning the project you regenerate them..
Everytime if you make any changes in xml or any interchange of position of views in xml or change of id's,you need to clean build your project.If not you will get this exception.
Hello I have tested your code, your code is fine. Please clean your project from
select your project then click on Project and then Clean and also check the Build Automatically . Your auto generated class R is not generated properly.
Try cleaning your project and run it again..
Eclipse -> Project menu ->Clean
It is because eclipse gets confused when we play around in the xml file ;)
How do i make a image clickable? I have tried some ways, but without success.
Here's the last code i tried (it's clickable but gets error):
ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
btnNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff
}
});
and here's the part from xml:
<ImageView
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickImage"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
When running this code, and clicking the image i get this error:
01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity
HERE'S THE SOLUTION:
The XML:
<ImageButton
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickNew"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#null" />
The code :
public void clickNew(View v)
{
Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show();
}
As other said: make this an ImageButton and define its onClick attribute
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:onClick="scrollToTop"
android:src="#drawable/to_top_button"
/>
The image is here encoded in a file res/drawable/to_top_button.png. If the user clicks on the button, the method scrollToTop() is called. This method needs to be declared in the class that sets the Layout with the ImageButton as its content layout.
public void scrollToTop(View v) {
...
}
Defining the OnClick handler this way saves you a lot of typing and also prevents the need to anonymous inner classes, which is beneficial for the memory footprint.
Does an ImageButton do what you want?
The error message you get implies that you do not have a method in your activity that matches your onClick handler.
You should have something like clickImage(View view) in your activity with the click handling implementation.
You could just use the ImageButton class...
http://developer.android.com/reference/android/widget/ImageButton.html
Use a ImageButton ;)
You've set the onclick method to call "clickImage" when the image is clicked in your XML, but you haven't created a clickImage method in your code. You shouldn't need to set the onclick listener at all. Just implement the method from your XML and you should be set.