I have the following code and I can't find a way to get rid of these errors:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
This applies to the lines 17, 18, 19, 20, 21, 22, 23, 24, 25 containing:
findViewById(R.id.imageButton9).setOnClickListener(this);
In line 31 (the line where the new class is created), I get:
The nested type MainActivity cannot hide an enclosing type
This is the code I'm working with:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imageButton1).setOnClickListener(this);
findViewById(R.id.imageButton2).setOnClickListener(this);
findViewById(R.id.imageButton3).setOnClickListener(this);
findViewById(R.id.imageButton4).setOnClickListener(this);
findViewById(R.id.imageButton5).setOnClickListener(this);
findViewById(R.id.imageButton6).setOnClickListener(this);
findViewById(R.id.imageButton7).setOnClickListener(this);
findViewById(R.id.imageButton8).setOnClickListener(this);
findViewById(R.id.imageButton9).setOnClickListener(this);
}
class MainActivity extends Activity implements View.OnClickListener {
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.R.id.imagebutton1:
startActivity(new Intent(telefoonnummers.class));
break;
case R.id.R.id.imagebutton2:
startActivity(new Intent(telefoonnummers.class));
break;
//-- more cases --
case R.id.R.id.imagebutton9:
startActivity(new Intent(telefoonnummers.class));
break;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Remove the errant class definition:
class MainActivity extends Activity implements View.OnClickListener {
And add implements View.OnClickListener to the real class definition:
public class MainActivity extends Activity implements View.OnClickListener {
// Add this to the "real" MainActivity ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take a moment to make sure you have properly closed every brace ({}).
Related
I have Action bar and a lot of different Activity, its working, but I don't want to add code in every Activity. How to add one code in one Activity that will work in every Activity? I expect to write only one code in one Activity for all activities.
My main activity:
package ua.in.masterpc.technoguide;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
public class IconsMain extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icons_main);
ImageButton myImageButton = findViewById(R.id.iconPC);
myImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(IconsMain.this, PCProblems.class);
startActivity(intentLoadNewActivity);
}
});
}
#Override//start menu code
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.bar_menu, menu);
return true;
}//end menu code
}
Thank you for answers!
You can create class named BaseActivity which will extend AppCompatActivity and put there the code and functionality you want to use in every other Activity. Then in every of your activities, you will extend BaseActivity instead of AppCompatActivity. By doing that you can avoid duplicating your code. Remember that you will have to put ActionBar in every activity .xml layout file.
create a baseActivity
add function inside it to initialize the toolbar and customize it as you like
open class BaseActivity : AppCompatActivity() {
protected fun initializeToolbar() {
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
}
}
create toolbar_main.xml
extends the baseActivity in any activty after including the toolbar_main.xml as <include layout="#layout/toolbar_main" />
class MainActivity : BaseActivity(){
}
and call the function you created in baseActivity.
Create a layout say, toolbar_layout, and add the the code for your layout, then include the layout in all your activities:
package com.example.nimehr.streaming;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.videostream.VideoViewActivity;
public class MainActivity extends Activity {
Button button;
public MainActivity() {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(2130903040);
this.button = (Button)this.findViewById(R.id.myButton);
this.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, VideoViewActivity.class);
MainActivity.this.startActivity(intent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
this.getMenuInflater().inflate(2131165184, menu);
return true;
}
}
I'm getting the error "Expected resource of type layout/menu" at 2130903040 in this.setContentView(2130903040); and 2131165184in this.getMenuInflater().inflate(2131165184, menu); respectively. I fixed it here this.button = (Button)this.findViewById(2131230720); by changing it to R.id.MyButton
DO not do it this way:
this.setContentView(2130903040);
R class exists for a reason to avoid explicitly using ids like this, and to replace it with a more readable cleaner code:
this.setContentView(R.layout.layout_name_here);
From the docs:
When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory.
Could someone please explain why I am getting this error?
Java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.View$OnKeyListener
Below is the java code. I am trying to follow a tutorial and I can't get it started. The RAZR emulator only shows Unfortunately JimYamba quit working. The error is from Eclipse LogCat.
package com.example.jimyamba;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.util.Log;;
public class MainActivity extends Activity implements OnClickListener{
Button buttonUpdate;
EditText editStatus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
buttonUpdate = (Button)findViewById(R.id.button_update);
editStatus = (EditText) findViewById(R.id.edit_status);
buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
String statusText = editStatus.getText().toString();
Log.d("StatusActivity","onClicked with text:" + statusText);
}
}
The error is in the line buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);. Just as the error says, you cannot cast a button to an OnKeyListener. Explicit casting requires a relationship via class hierarchy - Button and OnKeyListener are not related (OnKeyListener is an interface).
If you want to maintain your set up you can create a class that extends Button and Implements OnKeyListener. You will have to edit you xml to have your custom view in place of the button.
buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);
should be
buttonUpdate.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
});
Look at http://developer.android.com/reference/android/view/View.OnKeyListener.html
What are you trying to accomplish? You have implemented OnClickListener so if all you need is to execute what's inside of the onClick method with an OnClickListener, just change
buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);
With
buttonUpdate.setOnClickListener(this);
Or if you want OnKeyListener do what techiServices suggested
I'm making an app and when it launches it starts Mainactivity.java
Mainactivity.java opens a layout with 9 Imagebuttons.
How can I implement in my code in Mainactivity.java that once one is clicked it opens another activity (like telefoonnummers.java)?
Sorry for my bad English but I'm dutch and a non-native speaker.
I have this code in Mainactivity.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Very clean as you see, but how can I add the implementation, would you guys please help???
My Imagebuttons are all just called imagebutton1, imagebuttton2 etc. btw.
After
setContentView(R.layout.activity_main);
add for each ImageButton:
findViewById(R.id.imagebutton1).setOnClickListener(this);
Make class implement OnClickListener
class MainActivity extends Activity implements View.OnClickListener {
and add this method:
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.R.id.imagebutton1:
startActivity(new Intent(telefoonnummers.class));
break;
case R.id.R.id.imagebutton2:
startActivity(new Intent(telefoonnummers.class));
break;
//-- more cases --
}
}
I'm extremely new to all of this coding stuff, and I've gotten pretty far on my own, but I can't seem to figure out this error. All help is appreciated.
"No enclosing instance of the type DonationsActivity is accessible in scope"
package com.ganttbros.shadowui;
import org.donations.DonationsActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class DonateActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
final Button donate = (Button) findViewById(R.id.donatebutton);
donate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.donate, menu);
return true;
}
}
I should tell you that I am attempting to implement this library: https://github.com/dschuermann/android-donations-lib#readme
I have set up the button, but I just need to get it to launch the "DonationsActivity" when pressed.
use
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));
OR
startActivity(new Intent(v.getContext(), DonationsActivity.class));
instead of
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
for starting DonationsActivity Activity from DonateActivity Activity
Change this:
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
to:
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));