I'm a beginner with Java and Android Studio.
I try to implemet a OnEditorActionListener that listens for when the user presses the Done button in a softkeyboard.
There are many code fragments available, but I dont know where to put the code (onCreate ? Own class...)
While testing some of those code fragments I get an error "Cannot resolve symbol override", see screenshot.
I've found some articles about "cannot resolve symbol xy", but nothing about "override"
So my question is:
Why do I get "Cannot resolve symbol override" here ?
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.frm_main_layout);
//txtMeineZahl.setOnEditorActionListener(new classDoneOnEditorActionListener()); //doesnt work so far..OK for now
EditText txtMeineZahl = (EditText) findViewById(R.id.txtMeineZahl);
txtMeineZahl.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#override ---- ERROR here
public boolean onEditorAction(TextView view, int action,KeyEvent arg2event)
{
if (action == EditorInfo.IME_ACTION_DONE)
{
//view.setFocusable(false);
Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_LONG);
}
return false;
}
});
}
It should be #Override not #override. O must be capital in #Override.
Related
I am trying to make carousel with clickable images, made as a list. I want to make so using SetOnItemCLickListener, but when I try to rebuild the project an error pops up and tells me:
error: cannot find symbol method setOnItemClickListener(<anonymous OnItemClickListener>)
I searched the internet for a solution but I am still stuck.
I have tried so far:
Clean the project (obvious);
Searched for name illegal elements, there are none;
When I tried only with SetOnCLickListener it did not work; it is in red-colored letters and there is no method like this.
public class FinalSadMovies extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_sad_movies);
List<CarouselPicker.PickerItem> imageItems = new ArrayList<>();
imageItems.add(new CarouselPicker.DrawableItem(R.drawable.joker));
imageItems.add(new CarouselPicker.DrawableItem(R.drawable.starwars_resized));
imageItems.add(new CarouselPicker.DrawableItem(R.drawable.test));
CarouselPicker.CarouselViewAdapter imageAdapter = new CarouselPicker.CarouselViewAdapter(this, imageItems, 0);
carouselPicker.setAdapter(imageAdapter);
carouselPicker.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("MainActivity", "ListView item clicked.");
}
});
If you're using carouselPicker from GoodieBag then setOnItemClickListener is not supported.
The only thing you can rely on is the addOnPageChangeListener but this requires you to change your app UI and flow (because you need to use a button to confirm the selection).
Another option is to create a custom CarouselViewAdapter class where you can handle the click event on the instantiateItem method.
I have this weird problem when showing an activity which consists (only) out of a webview.
The first time I start this Activity B by clicking on something in activity A, B is properly loaded and functional. I press the return button, go back to A, then click on the same button to go to B and my webview doesn't show anything. Debugged it tons of times and nothing seems out of the ordinary. There are also no crashes in the stack trace log... This problem by the wway occurs always in debug mode and only sometimes in run mode.
Here is the code for the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin);
myItem = (Item) getIntent().getSerializableExtra("Item");
initView();
initHandlers();
}
private void initView()
{
webView = findViewById(R.id.webview_spin);
//TODO : webViewClient vs webChromeClient ?
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
String json = new Gson().toJson(myItem);
webView.loadUrl("file:///android_asset/item.html?item="+json);
}
private void initHandlers()
{
OrientationEventListener orientationEventListener = new OrientationEventListener(this) {
#Override
public void onOrientationChanged(int orientation) {
webView.reload();
}
};
orientationEventListener.enable();
}
FYI this problem occured before setting the orientationlistener.
That looks like somehow the render process of the WebView is failing. You could store the WebViewClient you created on a variable and check the result of its method onRenderProcessGone() to check if it is rendering the second time you load the activity. Here you have all the WebViewClient methods: https://developer.android.com/reference/android/webkit/WebViewClient
I'm fairly new to Android, and Javascript. But, I'm actively learning by watching tutorials, and asking questions. Now, I'm trying to code an app that changes certain system values. I know this can only be done by granting my app the WRITE_SECURE_SETTINGS permission. Through my research I've realized I cannot declare the permission in the manifest file itself. Obviously, the only respectable answer is to check if the permission has been granted by the user through ADB. I can't find anything related to this topic and I'm hoping you guys can help me come to a conclusion on how to properly do this. So far this is the code that I have.
package com.datastream.settingschanger;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String requiredPermission = "android.permission.WRITE_SECURE_SETTINGS";
int checkVal = getContext().checkCallingOrSelfPermission(requiredPermission);
if (checkVal == PackageManager.PERMISSION_GRANTED) {
}
Toast.makeText(getActivity(), "WRITE_SECURE_SETTINGS has been granted to the application. You may now continue!",
Toast.LENGTH_LONG).show();
if (checkVal == PackageManager.PERMISSION_DENIED) {
}
Toast.makeText(getActivity(), "WRITE_SECURE_SETTINGS has not been granted to the application. Please grant access to continue.",
Toast.LENGTH_LONG).show();
}
}
But, when ever I run this in Android Studio I get the following errors:
error: cannot find symbol method getContext()
error: cannot find symbol method getActivity()
error: cannot find symbol method getActivity()
Can anyone help me resolves these errors? I have no clue how to fix this. Thank you!
The getContext() and getActivity() method is used in Fragment to get Context
Remove getContext() and getActivity() And use this and MainActivity.this to get Context in your activity
Use this
int checkVal = checkCallingOrSelfPermission(requiredPermission);
instead of this
int checkVal = getContext().checkCallingOrSelfPermission(requiredPermission);
SAMPLE CODE
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String requiredPermission = "android.permission.WRITE_SECURE_SETTINGS";
int checkVal = checkCallingOrSelfPermission(requiredPermission);
if (checkVal == PackageManager.PERMISSION_GRANTED) {
}
Toast.makeText(this, "WRITE_SECURE_SETTINGS has been granted to the application. You may now continue!",
Toast.LENGTH_LONG).show();
if (checkVal == PackageManager.PERMISSION_DENIED) {
}
Toast.makeText(this, "WRITE_SECURE_SETTINGS has not been granted to the application. Please grant access to continue.",
Toast.LENGTH_LONG).show();
}
}
If anyone wants to break down this code and explain it to me. I'd be thankful.
I run into an error on view(cannot resolve symbol), not sure if I'm supposed to replace that with a specific view ?
Btw, this is an onClick method.
"else if(view.getId()==R.id.Button9){}"
What I understand from this code is it says when "if" "view" whatever the viewid is goes in here ()?? == <--is related to R.id.button9
then run this block of code. Am i even close? Thanks.
A little backstory, I have created an ImageButton and when it's clicked, I'd like to to clear the screen. I've built on onclicklistener and implemented view.OnClickListener on my user public class.
CLEARCANVAS = (ImageButton) findViewById(R.id.button9);
CLEARCANVAS.setOnClickListener(this);
#Override
public void onClick(View v) {
if (view.getId()==R.id.button9);
}
Your View parameter is v, not view.
Change it to v and it will compile:
#Override
public void onClick(View v) {
//if (view.getId()==R.id.button9){
if (v.getId() == R.id.button9){
//handle button9 click
}
}
Another common way to do it is to define a separate click listener for each clickable element, for example:
clearCanvas = (ImageButton) findViewById(R.id.button9);
clearCanvas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Handle button9 click
}
});
If the error is related with class R, then you need to rebuild the project or else if its still not working for you, you will have to restart the IDE you are using. Basically if you are using Eclipse, I suggest to change to Android Studio as google has deprecated android support to eclipse. Cannot resolve symbol error basically means that the project does not have particular class reference or whatever you are trying to use.
It is like this.
You have 10 buttons from button1 to button10. Everything has an id stored in R.java file to uniquely identify it.
Suppose you want to perform a onClick event for button9. You create a generic onClick event for all the buttons.
#Override
public void onClick(View v) {
//now all the 10 buttons have the same onclick event.
}
Now to differentiate between which button was clicked, you need to use it's id.
#Override
public void onClick(View v) {
if (v.getId()==R.id.button9){
Toast.makeText(context, "Button 9 Clicked!", Toast.LENGTH_SHORT).show();
}
if (v.getId()==R.id.button8){
Toast.makeText(context, "Button 8 Clicked!", Toast.LENGTH_SHORT).show();
}
...
}
So, R.java is a link between your UI objects in the XML and the java code.
You'll need the R.java to identify each object from the UI.
This is automatically done. If you get an error saying cannot resolve R. You might want to re-build or clean your project.
you just need to change your view.getId() with the v.getId().
The view which is passed in the OnClick method is the view which is clicked, so It will check which Id is clicked.
so I have a working webview that is simple, but most of the work is done in the main activity. I am trying to break it out to its own little class so I can load other classes into the main activity on the fly. I am stuck on how to do this. I have tried this a few times and been debugging it but I just dont know if I am using the right findByValue(R.id.webView); thing here. it keeps giving me a
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference
issue. I have tried passing the reference in as a parameter and also hard coded it. I am missing something but dont know what. There is a WebView in my activity_main that is called wevView that is what I am trying to find out where i'm did mistake.
Thanks for any help with this.
So this is my webview class:
public class NewWebView {
//variables need for webview
private final String myPage = "http://www.google.com";
WebView myWebView = null;
//consturctor
public NewWebView(){}
//This method creates the WebView
public void createWebView(WebView webView)
{
//WebView code
//Find the Webview in the Activity.xml file
//myWebView = webView;
myWebView = (WebView)myWebView.findViewById(R.id.webview);
// myWebView = (android.webkit.WebView)findViewById(R.id.webview);
//Use the custom WebViewClient for internal app browsing
myWebView.setWebViewClient(new MyWebViewClient());
//now for enabling the settings
WebSettings webSettings = myWebView.getSettings();
//Enable JavaScript
webSettings.setJavaScriptEnabled(true);
//Enable andriod zoom features
webSettings.setBuiltInZoomControls(true);
//Loading the Url
myWebView.loadUrl(myPage);
}
//Methods Used for WebViewer
//custom WebViewClent needed for internal app navigatiion
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverRideUrlLoading(android.webkit.WebView view, String url)
{
if(Uri.parse(url).getHost().equals(myPage))
{
//this is Apivita remain in the APP
return false;
}
/*
//if it is not in my site redirect it to mobile browers
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
*/
return true;
}
}
//Enable the use of the system back button for navigation
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//if the back button was pushed
if((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
{
myWebView.goBack();
return true;
}
//if not
return false;
}
}
and here is the main activity
public class MainActivity extends AppCompatActivity {
WebView myWebView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create the webview
myWebView = (android.webkit.WebView)findViewById(R.id.webview);
NewWebView apWebView = new NewWebView();
apWebView.createWebView(myWebView);
}
}
It looks like you are finding your webview in the main activity and then passing it into your class. In your custom class, you're then trying to find another view, which you technically already have. So its trying to find a webview, inside of the webview. It obviously can't find this, so it's null.
In theory, you should be able to just use myWebView = webView;, however, you need to make sure that it is finding it in the first place.