This question already has answers here:
Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs
(9 answers)
Closed 9 years ago.
Having some trouble with my app.
First some code:
package activities;
import prototype.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class Doodle extends Activity implements OnClickListener {
//buttons
private ImageButton colorBtn;
//dialogs
private Dialog brushDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doodle);
//color button
colorBtn = (ImageButton)findViewById(R.id.color_btn);
colorBtn.setOnClickListener(this);
//dialogs
brushDialog = new Dialog(this);
brushDialog.setTitle("Color chooser:");
brushDialog.setContentView(R.layout.doodlepaintpalette);
Log.i("<------------------------->", "Hello - 3");
}
#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;
}
//user clicked paint
public void paintClicked(View view){
//Code here
brushDialog.dismiss();
}
//Top bar buttons
#Override
public void onClick(View view){
if(view.getId()==R.id.color_btn){
brushDialog.show();
}
}
"doodlepaintpalette" is a linear layout file containing various clickable imagebuttons with different colour codes. when I click the "color_btn" it correctly brings up the dialog, but when I click on one of the imagebuttons, the app crashes with this logcat:
E/AndroidRuntime(1486): java.lang.IllegalStateException: Could not find a method paintClicked(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton
The code worked fine when I had the image buttons in the "doodle" layout but since moving them to their own layout for use with the dialog, its created problems.
any help?
can post more code if need be
You paintClicked() method is not found as the layout is inflated in the dialog, and it looks for the method in there, but your method is in your activity.
You'll find an answer here:
Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs
Related
I have a bottom navigation bar with 4 buttons. One of them is a back-button that just navigates back to the previously displayed fragment (I use a one-activity multiple-fragments approach). Basically everything works fine. Now I wanted to know whether it is possible not to navigate back to a certain fragment A? So basically whenever this Fragement A was the last fragment that was being displayed and the user clicks on the back-button, it should not navigate back but rather stay on the current fragment (and maybe display a small toast message).
So here you can see my main activity where the navigation is implemented:
package com.example.td.bapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import com.example.td.bapp.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
public static DataBaseHelper myDB;
public static boolean veryFirstCreation = true;
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
final NavController navController = Navigation.findNavController(this,
R.id.navHostfragment);
NavigationUI.setupWithNavController(binding.bottomNavigation, navController);
myDB=new DataBaseHelper(this);
if(veryFirstCreation) {
navController.navigate(R.id.FR_LanguageSelection);
veryFirstCreation = false;
}
// Define the bahaviour when clicking on items of the bottomNavigationBar; Overwrites the JetpackNavigation behaviour (automatic navigation using the id specified in the BottomNavigation XML menu file)
binding.bottomNavigation.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId()== R.id.BottomNavigation_Back) {
//Here I navigate back to the previous fragment
navController.navigateUp();
}
if (item.getItemId()== R.id.BottomNavigation_Language) {
navController.navigate(R.id.FR_LanguageSelection);
}
if (item.getItemId()== R.id.BottomNavigation_MyItems) {
navController.navigate(R.id.FR_MyItems);
}
if (item.getItemId()== R.id.BottomNavigation_Menu) {
navController.navigate(R.id.FR_Menu);
}
return true;
}
});
};
}
I'd appreciate every comment and would be quite thankful for your help.
You could use the SupportFragmentManager, with this you could know if your back stack contains at least one fragment. Use this in your back function (or in your onBackPressed if necessary)
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() ==1) {
Log.d("ppl","This is the last fragment. Show your message, poppup or whatever you want here")
} else if (fm.getBackStackEntryCount() >1) {
Log.d("ppl","the backstack still have more to show")
fm.popBackStackImmediate();
}
to the point, i am working in a library for android app's, all programing part is almost finish, i am testing a idea.
the idea is put some interfaces in the librery that i am working, and this GUI can be loaded in the main app.
i try to do it but i get to error the first was i did not declare the activity in the AndroidManifest in the main app, the second one is the one a i cant solve, the autogenerated Class R don't capture the GUI that it is in the librery.
it is anyway i can do this or it is imposible.
code to see what i am trying todo
in my librery (movilsecure)
EmisionActivity.java (have the activity_emision.xml in the res of my librery)
in the Main Android App (
import ve.com.idyseg.movilsecure.EmisionActivity;
import ve.com.idyseg.movilsecure.MSMasterControllerTEST;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
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.main, menu);
return true;
}
public void captureEvent (View v){
Intent intento = new Intent(this,EmisionActivity.class);
startIntent(intento);
}
}
In Eclipses, right click the project, the click Build Path then click Add Library. Please comment if you're still stuck.
In the library's manifest, in the <activity> node, did you set android:exported="true"?
Example:
<activity
android:name="com.example.app.EmisionActivity"
android:exported="true">
</activity>
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 am trying to extract photos from my drawable folder into an ImageView array and add the views from the array as childs to the linearlayout which is part of the horizontalscrollview.
However when I launch the app, it stops unexpectedly. Would anyone know if there is any problem with my code. Thanks a mil for your help.
package com.example.myfirstapp;
import java.lang.reflect.Field;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class NewGallery extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_gallery);
LinearLayout gallery = (LinearLayout) findViewById(R.id.mygallery);
Field[] field = R.drawable.class.getFields();
final int arraylength = field.length;
ImageView[] images = new ImageView[arraylength];
for (int i=0; i<=arraylength; i=i+1)
{
images[i] = new ImageView(getApplicationContext());
images[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("icon"+i, "drawable", getPackageName())));
gallery.addView(images[i]);
}
}
#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_new_gallery, menu);
return true;
}
If you are using Eclipse, refer to logcat error messages. I think you got a ClassCastException due to incorrect casting :
LinearLayout gallery = (LinearLayout) findViewById(R.id.mygallery).
Are you sure you are casting the correct view ?
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 --
}
}