What do i do to be able to use a handler to update my TextView every 2 secs because right now it comes up with 'cannot resolve symbol"Handler"'
int noStart = 20;
int minus = 5;
public void number(View view) {
final TextView tx = (TextView) findViewById(R.id.number);
if(noStart<0){
new Handler().postDelayed(new Runnable() {
public void run() {
noStart -= minus;
tx.setText(String.valueOf(noStart));
}
}, 2000);
}
}
Check your activity imports and add this line to the class:
import android.os.*
In my case, i refer to a constant of class Handler in outer calss, see as below:
import static *.MyHandler.MSG_WHAT_PING_NETWORK;
public class OuterClass {
// use MSG_WHAT_PING_NETWORK...
// ...
}
class MyHandler extends Handler {
public static final int MSG_WHAT_PING_NETWORK = 99;
// ...
}
remove the import in outer class can solve the problem.
Related
I want to update this num variable from different classes through this Methods .
but it show '0' when i display it
public class ResultNum extends AppCompatActivity{
private int num;
public int ResultNum1(){
return num;
}
public void yesMethod(int i){
num = i+num;
}
Here is Another class
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.yesMethod(2);
}
});
and also I've some other classes as well which passes different numbers
here's code for displaying number
TextView t = (TextView) findViewById(R.id.text_number);
int x = result.ResultNum1();
t.setText(""+x);
i also tried
TextView t = (TextView) findViewById(R.id.text_number);
t.setText(""+result.ResultNum1());
so What exactly i need to do now?
As already stated, you should either declare int num as static or make sure that all your methods across application use the same instance of ResultNum class.
I am pretty new at android/java development and I am trying to process onClick events in an array of view from a "library" class but I can't figure out how it should work.
What I have so far is :
For the library :
public class WordArea {
private ImageView[] imageViewArray;
public boolean createWordTemplate(Activity myActivity, int layoutId, int numberOfLetters, int[] imageArray) {
imageViewArray = new ImageView[10];
// .....
for (int i = 0; i < 10; i++) {
// ...
imageViewArray[i].setClickable(true);
layout.addView(imageViewArray[i], params);
imageViewArray[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
thisIsWhereIwouldLikeTheCallbackToWork( v );
}
});
Then in my activity class I am defining objects like this :
private WordArea myWordArea = new WordArea();
And I would like to define in the activity class the method to be called by the setOnClickListener
Can anyone please help me with the code I should put in the library and in the activity class to make it work ?
Sorry if this question might be stupid for you, but I'm new to Android programming and I can't wrap my head around Java syntax.
Can you explain what is happening with this line of code step by step?
View.OnClickListener ourOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v){
ourMessage.setText(“The button got tapped”);
}
};
There is an interface declared inside View class, and it's OnClickListener, it looks like this in View.java source:
/**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* #param v The view that was clicked.
*/
void onClick(View v);
}
Normally you would create a class, and have it implement this interface:
public void MyClass implements View.OnClickListener {
#Override
public void onClick(View view) {
// do stuff
}
}
But sometimes you don't need this class in a separate file. Instead, you can create anonymous inner class, it's like creating new class, which only methods are the one from the interface specified:
new View.OnClickListener() {
#Override
public void onClick(View v){
ourMessage.setText(“The button got tapped”);
}
}
You can then use instance of this class everywhere the View.OnClickListener interface is needed.
What's also worth mentioning is that anonymous inner class will hold a reference to the class in which you're creating it. So this will be legal and valid:
public class MyClass {
private int clicksCount = 0;
private View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
clicksCount += 1;
}
}
}
Here you can access clicksCount field, which is field of MyClass even from the inner class that implements OnClickListener. Side note - if you want to access a variable, you need to add final modifier to it:
public void testMethod(final int canAccess, int cantAccess) {
final String test = otherView.getText().toString();
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Cannot access cantAccess, because it's not final
if (test.length == 0) { // can access
// do something
}
}
}
I'm trying to pass something from one class to my MainActivity, but it doesn't seem to work, I don't understand why.
I have my GPS Tracker on another class (not the MainActivity) in order to reuse it.
When the location changes, I want my other class to call a method from within the MainActivity to update my UI.
I summarized my code like that :
My MAIN ACTIVITY :
public class MainActivity extends Activity implements OnClickListener {
TextView tv;
EditText et;
Button btun;
int arg0;
int stuff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
et = (EditText) findViewById(R.id.et);
btun = (Button) findViewById(R.id.btun);
btun.setOnClickListener(this);
}
private void setter(int stuff) {
tv.setText(stuff);
}
public void setText(int _stuff) {
_stuff = stuff;
setter(_stuff);
}
#Override
public void onClick(View v) {
Getter get = new Getter();
get.getInfo(Integer.parseInt(et.getText().toString()));
}
The other Class :
public class Getter {
int _getString;
MainActivity main = new MainActivity();
public void getInfo(int getString) {
_getString = getString * 8;
main.setText(_getString);
}
}
I end up having a NullPointerException in my LogCat
at :
- tv.setText(stuff);
- setter(_stuff);
- main.setText(_getString);
- get.getInfo(Integer.parseInt(et.getText().toString()));
and I don't really know why, and above all, how to fix it.
I'll appreciate any help !
(PS : My GPS tracker thingy is working fine, it's just about invoking my setter() method.
Instantiaing an Object of MainActivity doesn't automatically call onCreate method but this method is called when you start an activity using Intent; And using the same intent you can pass extra values. For example:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("key", value);
context.startActivity(intent);
and then in your main activity onCreate method:
String value = getIntent.getStringExtra("key");
Edit:
In your case why don't you change your void getInfo(int getString) to return a String value i.e.
public class Getter {
...
...
public String getInfo(int getString) {
_getString = getString * 8;
return Integer.toString(_getString);
}
}
and then in onClick event of MainActivity bind this returned text to TextView
It's maybe because the MainActivity's onCreate()-Method hasn't been called. Therefore the tv is still null causing the NullPointerException
One problem is here. main is an Activity, but it should be the MainActivity calling this object.
public class Getter {
int _getString;
MainActivity main = new MainActivity();
public void getInfo(int getString) {
_getString = getString * 8;
main.setText(_getString);
}
}
I cannot really make out what you are trying to achieve in the Getter class, but either:
1: Pass the Activity instance to the object
public class Getter {
int _getString;
MainActivity _main = null;
public Getter(MainActivity main) {
_main = main;
}
public void getInfo(int getString) {
_getString = getString * 8;
_main.setText(_getString);
}
}
#Override
public void onClick(View v) {
Getter get = new Getter(this);
get.getInfo(Integer.parseInt(et.getText().toString()));
}
or
2: set the text in the Activity and only get the value from the Getter (My choice)
public class Getter {
int _getString;
MainActivity main = new MainActivity();
public void getInfo(int getString) {
return getString * 8;
}
}
#Override
public void onClick(View v) {
Getter get = new Getter();
int info = get.getInfo(Integer.parseInt(et.getText().toString()));
setText(Integer.toString(info));
}
Use Application Class or create a separate Class and declare a static variable in it. Use getter & setter methods to get the value. To update the Textview in mainacivity from other class pass the texview reference variable from main activity and put null check condition in other class if textview is not null then update the value.
I am trying to get a method from the file Duality.java to be run in Min.Java when a button is clicked. Below are the two files and what I am currently trying to do, which is not working. How do I get the method duality() to run when the button is clicked within Min.java?
Duality.java
package com.android.control;
import android.util.Log;
import com.map.AppName.R;
public class duality {
public void duality(){
Log.e("Did It Run","Yes it ran");
}
}
Min.java
package com.android.control;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.map.AppName.R;
public class Min extends LinearLayout {
Button but;
private final int ELEMENT_HEIGHT = 60;
private final int ELEMENT_WIDTH = 80;;
private final int TEXT_SIZE = 30;
public Min( Context context, AttributeSet attributeSet ) {
super(context, attributeSet);
this.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
LayoutParams elementParams = new LinearLayout.LayoutParams( ELEMENT_WIDTH, ELEMENT_HEIGHT );
createBut( context );
addView( but, elementParams );
}
private void createButton( Context context){
but = new Button( context );
but.setTextSize( TEXT_SIZE );
but.setText( "Go" );
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Duality duality = new duality();
}
});
}
}
You're only constructing an instance of the duality class - you're not calling the duality() method on it.
This might be because you wanted that method to be a constructor - but it's not, because you specified a void return type, so it's just a conventional method.
(By the way, it's conventional in Java to give classes names that start with uppercase characters. If you called your class Duality, there may be less chance that you'd get the two confused; though the problem with accidental non-constructors would still stand.)
Both Min and duality are in the com.android.control package so they should be able to see eachother without imports.
It's recommended to capitalize class names. In fact, since your method has the same name as the class it might be conflicting with the constructor name. I suggest this:
public class Duality {
public void duality(){
Log.e("Did It Run","Yes it ran");
}
}
...
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Duality d = new Duality();
d.duality();
}
});
make sure that the class name and file name use same case combination in names.
if u want to call the constructor, remove the void from:
public void duality()
if it is supposed to b a function and not constructor, call it using:
object_name.duality();
u r calling createBut() and have given code for createButton().. is that a mistake in copy pasting?