public class Activity01 extends Activity implements OnClickListener,
ViewFactory {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main_view = new LinearLayout(this);
m_Switcher = new ImageSwitcher(this);
main_view.addView(m_Switcher);
m_Switcher.setId(SWITCHER_ID);
m_Switcher.setFactory(this);
m_Switcher.setOnClickListener(this);
setContentView(main_view);
...
}
public void onClick(View v) {
...
}
}
Above code is from an Android project, and below function's argument is set as 'this', why?
m_Switcher.setOnClickListener(this);
According to the javadoc, here should be like below:
public void setOnClickListener (View.OnClickListener l)
That means the argument should be this kind: View.OnClickListener
So why 'this' can be there? Thanks!
Note: According to the answers, I gave a more complete code above.
In the class declaration you will find it either extends or implements OnClickListener. That means that the class can be used as an OnClickListener (because it is one, amongst other things). That is why you can use this here.
Related
I was wondering one thing today. Whenever we are setting up a ClickListener, we run this code.
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I Opened the View Class and saw the OnClickListener interface and found that it is not static. The method is like this:
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* #param v The view that was clicked.
*/
void onClick(View v);
}
Then how are we able to access OnClickListener method directly by using class name?
For java, "A nested interface is implicitly static." jls (9.1.1.3)
You can implement class View.OnClickListener and then you will able to Override its method just like shown below :
public class YourActivity implements View.OnClickListener {
#Override
public void onClick(View v) {
}
}
I am trying to extend Main Activity in another class so that i can use objects already created in the Main Activity.
if i have a main activity:
public class MainActivity extends AppCompatActivity {
public ImageView jani;
public Context main;
public classextended janitest = new classextended();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jani = findViewById(R.id.jani);
main=this;
janitest.janiaha();
Log.d("jani","FAK");
}
}
enter code here
And then a new class that extends MainActivitiy:
enter code here
public class classextended extends MainActivity {
public void janiaha(){
jani.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(main, "YES YOU KNOW YOUR JAVA!!!", Toast.LENGTH_SHORT).show();
}
});
}
}
How can i acces the public void janiaha() ? Or am i doing it all wrong : ) ?
Al i get is crashes--- yes i could use static classes but as far as i know memory leaks would be a reall problem.
You can't call methods of a subclass that aren't defined on the main class. If you mean to have multiple classes descended from MainActivity, make it a protected function on MainActivity that's either abstract or has a default implementation, then override it in the subclass. If you aren't planning on having multiple child classes, then I question the value of even having one.
U cant call a method from child class to a parent class. If u want to have a function do similar work like it then, U can create a normal class to do it. or u can do this in this class. u just have to make the method 'static'..
public static void janiaha(ImageView jani){
jani.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(main, "YES YOU KNOW YOUR JAVA!!!", Toast.LENGTH_SHORT).show();
}
});
}
I declare a button in the Main Activity, but get NullObjectReference when I run the app. No matter where I try to move the declaration (outside of the class, inside the onCreate method, etc.), it redlines either the declaration or the button reference. I see posts recommending status versus non-static classes/methods, but I'm new to OOP and not exactly sure how to implement that. Can anyone tell me what I'm doing wrong?
public class MainActivity extends AppCompatActivity {
…
// Declare button in the MainActivity
Button btnWrong = (Button)findViewById(R.id.btnWrong);
...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// WHEN USER TAPS ITEMS ON APPBAR
switch (item.getItemId()) {
case R.id.mnuLoad: // If use taps "Load"
final Dialog dialog = new Dialog(MainActivity.this); /
…
btnLoad.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deckNumber = deckPicker.getValue();
if … }
dialog.dismiss();
btnWrong.setEnabled(true); // Trying to reference the button here
You have to do this:
outside onCreate() (under public class MainActivity extends AppCompatActivity)
Button btnWrong; //Declare button as private
in onCreate()
btnWrong = (Button)findViewById(R.id.btnWrong);
Now you can use btnWrong anywhere in its class
Try this:
public class MainActivity extends AppCompatActivity {
// Declare as class field
Button btnWrong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT_XML);
btnWrong = (Button)findViewById(R.id.btnWrong);
}
....
}
I have a main activity class which instantiates the variables for my widgets and makes references to them with findViewById. I would like to be able to set the text on a textview from a separate class, however eclipse complains that the variables cannot be resolved. I have set the main activity widget variables to public. Sorry if this is a silly question, I'm new to android.
public class MainActivity extends Activity {
public TextView player1ScoreView;
public TextView player2ScoreView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player1ScoreView = (TextView) findViewById(R.id.player1ScoreTextView);
player2ScoreView = (TextView) findViewById(R.id.player2ScoreTextView);
}
}
public class GamePlay {
...
player1ScoreView.setText(num.format(player1score));
}
player1ScoreView isn't recognized in the separate class. Does GamePlay have to be an activity to be able to set the text on a widget? Please explain. Thanks in advance
Your instance of the GamePlay class must have a reference to your MainActivity object in oder to access its members.
public class GamePlay {
...
MainActivity main = ...
....
main.player1ScoreView.setText(num.format(player1score));
}
Of course it would be better to access is via a getter method.
I'm not sure how you'll pass the reference of the MainActivity to your GamePlay object. It depends on how you create that object.
You would need to send an instance of the TextView to the other class or return the value to set and set it in the Activity.
So, you could get an instance of that class then send the TextView to a method of that class and set it there.
gamePlay.setScore(player1ScoreView);
and in the class
public void setScore(TextView tv)
{
tv.setText(num.formatplayer1score);
}
Or get the text to set from the gameplay class with something like
player1scoreview.setText(gameplay.getScore());
then have a method getScore() in your GamePlay class which would return a String to set the text as.
Simple option is to pass TextView object(player1ScoreView) as a parameter in GamePlay class method. By passing this object you can set value for this object through this method of GamePlay class.
Refer code below:
public class MainActivity extends Activity {
public TextView player1ScoreView;
public TextView player2ScoreView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player1ScoreView = (TextView) findViewById(R.id.player1ScoreTextView);
player2ScoreView = (TextView) findViewById(R.id.player2ScoreTextView);
GamePlay gamePlay=new GamePlay();
gamePlay.setViewValue(player2ScoreView);
}
}
public class GamePlay {
...
public void setViewValue(TextView player2ScoreView)
{
player2ScoreView.setText(num.format(player1score));
}
}
Assume that i have a activity class named MainActivity.java. But this activity has about 3000 lines code for example.
I want to seperate code parts of this file to an external java file(class) named NecessaryThings.java. But if i run my project on emulator it stops itself after this operation.
Is there a way to seperate some lines of this activity?
I wrote mini example for better..
Also what do you think about;
Using this method is beneficial or harmful in terms of performance?
This is my MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I want to call these lines from NecessaryThings.java
TextView genderResult = (TextView) findViewById(R.id.genderText);
genderResult.setText("Cinsiyet:");
TextView calorieResult = (TextView) findViewById(R.id.remainCalorie);
String getGenderSTR = getIntent().getStringExtra("GENDER");
genderResult.setText(getGenderSTR);
String calorieResultSTR = getIntent().getStringExtra("CALORIECHOOSED");
calorieResult.setText(calorieResultSTR);
/*
.....
.....
*/
}
Aftet I take above code, then I want to store it in NecessaryThings.java
like this..
//All necessary imports here. There is no problem about those.
public class NecessaryThings extends Activity {
public void myPersonalMethod() {
TextView genderResult = (TextView) findViewById(R.id.genderText);
genderResult.setText("Cinsiyet:");
TextView calorieResult = (TextView) findViewById(R.id.remainCalorie);
String getGenderSTR = getIntent().getStringExtra("GENDER");
genderResult.setText(getGenderSTR);
String calorieResultSTR = getIntent().getStringExtra("CALORIECHOOSED");
calorieResult.setText(calorieResultSTR);
}
}
If I rearrange my MainActivity.java It will be like this...
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NecessaryThings showMyMethod = new NecassaryThings();
showMyMethod.myPersonalMethod();
/*
the rest of the codes...
*/
}
But it is not working if I seperate code. Why and How can I do it?
public class MainActivity extends NecessaryThings {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPersonalMethod();
}
NecessaryThings extends Activity so MainActivity no long needs to extend from Activity, it should extend from NecessaryThings. One thing I need to point out is that super.onCreate(savedInstanceState); will call the onCreate(); from NecessaryThings. Since my myPersonalMethod(); is from super class, you can just call it.
All activities are regular Java classes and you can - of course, have many non-UI classes like Application, you can have helpers etc. Looking into your question, I would like to tell you that the Activity doesn't have user defined constructor and can be created only indirectly by calling startActivity method, but in other aspects it is a common Java class.
Hence, what you'll have to do is, let your NecessaryThings.java be a normal class, to which you can pass the context from your MainActivity and do all that is required.
Hope this helps.