Android: Cannot cast from View to Chronometer - android

I try to follow this example ChronometerDemo.java, but I got some problem:
I can't import android.widget.Chronometer;
Error: conflicts with a type defined in a same file(import android.widget.Chronometer.OnChronometerTickListener)
I can't cast View to Chronometer
mChronometer = (Chronometer) findViewById(R.id.chronometer);
Anyone can help? Appreciate,
Daisy

There seems to be another Chronometer class present that does not extend from View, which is why you can't cast it. It's hard to tell without your code, but did you rename ChronometerDemo.java to Chronometer.java? ChronometerDemo is an Activity, and is not supposed to be returned by a findViewById call.
This is, of course, assuming you're talking about this.

Related

Logic behind the creation of an Instance of a Button vs the creation of an instance of a plain Class?

I can't understand the logic behind the following code in android studio when we create an instance of a Button:
Button btn = (Button) findViewById(R.id.btnRegister);
From what I know an instance of a class is defined like:
MyClass myInstance = new MyClass(MyParameter)
If we apply the above logic the button code should be like this:
Button btn = new Button(findViewById(R.id.btnRegister));
But in fact the above code does not work. Why is "button" code so complicated to understand and does not relate to the code of a mere class? What does "(Button)" in brackets mean in the above example????
Thank you
The find view line doesn't create a button. Your views are all created using the standard new syntax by the framework when you call setContentView. The setContentView function parses your xml and creates the appropriate classes. What findViewById does is search the views that it created in setContentView checking the ids for one with a matching id and returns it.
As for what (Button) does- its a standard Java cast. findViewById returns a View object. But some subclasses of View have advanced functionality not found on view. To use them, you have to have an object of the correct type. So you cast it to the correct type (which will throw an exception if it isn't the right type). For example you need to have a TextView to call setText, a normal View doesn't have that function.

TextView setText Cannot resolve type

Im using Android Studio, trying set text to Text View but every time i do this
TextView test = (TextView) findViewById(R.id.testView);
test.setText("fds");
on Android Studio i see Cannot resolve symbol 'setText'.
Can someone tell me what im doing wrong?
setText() will accept String which you have passed, so there is no issue in that.
One possible reason for this might be you have not written your code inside onCreate() or onCreateView().
If it is Activity you need to use these lines after setContentView() in onCreate().
If it is Fragment you need to use these lines after inflating your view in onCreateView().
Second reason, you might be having one more test variable of different type like String or something else

NullPointerException in my aboutButton I think

My second book for Android programming Hello, Android by Ed Burnette. I'm using eclipse. The code matches the book and it matches the code downloaded from the website of the book. But I know I'm doing something wrong here. I added a bunch of breakpoints where I figure (mostly guessing) where the problem might be happening. What I've come to is that this line of code is the culprit (SudokuActivity.java line 21) You can download the entire code here
http://kbsoftware.dlinkddns.com/Sudoku.zip
aboutButton.setOnClickListener(this);
but I just can't figure out why ? It must be the result of something I'm doing wrong somewhere else. I've deleted and recreated the avd and that made no difference so not it. I'm at a lost here.
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
I want to thank everyone who responded, it's all fixed and working and I could not have done it without your help. I've learned more working on this problem then I would of in weeks if not months of problem free programming.
Yout aboutButton is not getting bound properly.
Do something like
Button aboutButton = (Button) findViewById(R.id.about_button);
I downloaded your code and it runs correctly on my phone. So if your code is the same it should run.
It seems that findViewById didn't find the view and then calling a method on a null object caused the nullpointerexception.
My dumb question: have you tried a project clean up? You can even try saving your classes, deleting the project and creating a new one.
Hope it helps

getText() Not Responding

I'm trying to get the text from one XML to another but it just crashes when its supposed to happen so any help will be welcomed!
Here is the code by the way
package com.android.test1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class xmltwo extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xmltwo);
TextView one = (TextView) findViewById(R.id.textView1);
EditText two = (EditText) findViewById(R.id.editText1);
one.setText(two.getText());
}
}
That's from the java file that opens the XML where the textView is.
My guess?
The logcat will be showing a NullPointerException for the line one.setText(two.getText()); because the layout file referenced by R.layout.xmltwo doesn't actually contain a TextView with an id of R.id.textView1 so the TextView called one is null.
Either that or that layout file doesn't contain an EditText with an id of R.id.editText1 which will make that null and cause an NPE when trying to call two.getText().
Just as an extra bit of information, if you want to get the text from an EditText you need to use getText().toString()
In this article Android: edit textview defined in xml one of the members has a similar issue. They theorize that possibly setContentView hasn't finished running yet and that may be the issue. However, you're saying that it is just completely crashing so I'm not sure if that could be the problem.
Could there be another part of your in your super class that could be hanging up? Could you use the getString() function instead perhaps?
I hope some of this might be helpful! I am interested in finding a solution so I intend to keep on reading to try and help!
try
one.setText(two.getText().toString().trim()+"");
hope this help..

How do you get a view reference from a class that doesn't extend Activity?

I want to have a class "Utils", that will have several methods used all over my code. For example, I have a top bar with a textview and two ImageButtons that must display different texts and icons on different activities.
I find myself writing stuff like this on every activity:
(TextView) topBarText = (TextView) findViewById(R.id.topBarText);
topBarText.setText(R.id.mytextForThisView);
I'd like to findViewById once in my whole app, and call a method setupTopBar(String text, R.id.iconForImageButton1, R.id.iconForImageButton2), or even pass the current Activity's id and let the method figure out what to show in the text and images.
I created the class Util, but it doesn't extend Activity. The problem is that if it doesn't, findViewById isn't accessible, so I can't use it.
What's the pattern to do something like this in Android?
Your helper methods should look like
public static void setTopBarText(Activity act, int textId){
(TextView) topBarText = (TextView)act.findViewById(R.id.topBarText);
topBarText.setText(textId);
}
Then you can do a static import from Activity and call
setTopBarText(this, R.id.mytextForThisView);
The Answer is not good for some situation.
This is my method:
In your Activity:
YouCustomClassObject.passView((View)findViewById(R.id.aview));
Using parameter passing can solve this kind of problem.

Categories

Resources