Android activity & import issue - android

Trying out some android programming that has gone fine up until this point. I'm getting the following errors:
Activity cannot be resolved to a type
The import Activity cannot be resolved
on the top two imports (activity and bundle) of the following class:
package com.example.sqlite;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
public class Input extends Activity {
private EditText txtTitle;
private EditText txtURL;
private Button btnOK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
txtTitle = (EditText) findViewById(R.id.editText1);
txtURL = (EditText) findViewById(R.id.editText2);
btnOK = (Button) findViewById(R.id.button1);
}
}
I've done the standard ctrl + shift + O, which does automatically add these imports but the errors remain. I've also rebuilt it over and over with no effect.
Ca anyone see were I'm going wrong here?

Do right click on the project node in Eclipse and select Android Tools->Fix Project Properties. It looks like you are missing link to your Android libraries. Good way to check on that is to open properties-> Java build path -> Libraries and verify that Android link is there. If not - do Properties->Android and select project target you would like to use

You may occasionally see strange erros if any of your XML files fail to build. Check your console to see if any of your layout or resources have errors. Do the following:
Project > Clean
If this does not fix it, verify that pour souce actually exists in the project path:
com.example.sqlite

Check your manifest xml if the activity is included.. This can sometimes cause the error.

Related

Gradle Error: cannot find symbol variable Settings_main

I currently have a problem changing the layout of my Android app. It doesn't work like usual.
When I want to change the layout to the settings layout with the press of a button, it gives the error: cannot find symbol variable Settings_main.
The code where this happens is
final ImageButton settings = findViewById(R.id.Settings_button);
settings.setOnCLickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.Settings_main);
}
});
This is the error it's giving me
I have imported
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageButton;
Can someone please help me solve this?
Every name and Id does match according to IntelliJ as well, so I have no idea what could possibly be going wrong.
File-based resource names must contain only lowercase a-z,0-9 or underscore.
Please change file name Settings_main to settings_main

Android studio project could not find android.view.View

My Android project show that it is not able to find the android.view.View class but still it is being compiled and run on simulator and devices.
apart from android.view.View All other class of the package is accessible like
This is not available ==> import android.view.View;
this is available => import android.view.ViewGroup;
I have tried many things like Clean, Rebuild, restart and Invalidate cache and restart but not fixed this issue.
Please check the code and help me to fix this issue.
import android.view.View;
public class AppSettingsActivity extends BaseActivity implements View.OnClickListener, AdListener {
//Other objects
private static final String TAG = AppSettingsActivity.class.getSimpleName();
//Data objects
// //Design
private LinearLayout banner_container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
initViews();
}
private void initViews() {
//Actionbar init
//Action bar
TextView idTvTitle = findViewById(R.id.idTvTitle);
findViewById(R.id.idTvSelectDefaultActivity).setOnClickListener(this);
findViewById(R.id.idTvSelectAppLang).setOnClickListener(this);
findViewById(R.id.idIvBack).setOnClickListener(this);
findViewById(R.id.idTvCalcSetting).setOnClickListener(this);
//Set data
//Set Actionbar Title
idTvTitle.setText(R.string.app_settings_text);
//Load Facebook ads
banner_container = findViewById(R.id.banner_container);
if (fbAdView != null) {
banner_container.addView(fbAdView);
fbAdView.loadAd();
fbAdView.setAdListener(this);
logs.e(TAG, "Face book load add called....");
}
int selectedLanguage=SessionManagement.getIntValue(AppSettingsActivity.this, AppConstant.SELECTED_LANG, AppConstant.INT_ONE);
int selectedScreen=SessionManagement.getIntValue(AppSettingsActivity.this, AppConstant.DEFAULT_SCREEN_INDEX, AppConstant.INT_ONE);
String[] languages = getResources().getStringArray(R.array.language);
String[] screens = getResources().getStringArray(R.array.screens);
((TextView)findViewById(R.id.idTvSelectDefaultActivity)).setText(screens[selectedScreen]);
((TextView)findViewById(R.id.idTvSelectAppLang)).setText(languages[selectedLanguage]);
}
I am giving this answer to my question.
I simply uninstall the Android studio and reinstall it. My previous Android studio was running with 3.5 version (Latest) and the fresh install Android studio version was also 3.5 so there was no versioning difference in it. But I don't know some How issue fixed when I reinstall AS.
After half an hour of searching, I could not found a solution that can fix my issue. Then I pass the same code to other PC and it was working well on that PC.
So This is final that no issue in the source code.
Then 2nd suspect is the android studio
I uninstall Android Studio and install it again, it took me 1 hour to set-up and make the code workable but it saves 30GB of my hard disk space so it was worth it.
But still, Android studio manage SDK and build tools very badly

Error from import android.R or from layout.main?

I keep either removing import.R or adding mypackage.R or deleting all imports and shift-control O
or deleting R.java.
I still get the error: main.xml cannot be resolved or alternately, R cannot be resolved to a variable. I have cleaned each time. How do you get this to work?
package com.hga;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Hga extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_hga );
Yeah i used to run into those type of problems when i was starting programing for android. The R folder updates itself everytime code is written or saved and when a error occurs in your code for whatver reason it may be the R folder gives you that error report. At first clean but if that doesnt work as it usually wont you want to completely coment out your code /* .... */
then clean and the error should go way now you can pin point which bit of code is causing the error to occur in the R folder.
PS. this was my main source of frustration in coding android apps

OnClickListener() must override a superclass method?

With this code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
. . .
Button buttonAuthorizeUsers = (Button) findViewById(R.id.buttonAuthorizeUsers);
buttonAuthorizeUsers.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent configure = new Intent(OnDemandAndAutomatic_Activity.this, Configure_Activity.class);
OnDemandAndAutomatic_Activity.this.startActivity(configure);
}
});
I'm getting:
The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
It would seem that this problem is sometimes caused by a Project | Properties | Java Compiler being set to 1.5.
Although I'm virtually I'd had this problem before, and changed it to 1.6, somehow it WAS 1.5 again.
HOWEVER, that (changing it to 1.6) did not solve the problem. I'm still getting that same err msg, after cleaning, building, and F11ing...???
I would recommend that you uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above. Otherwise you would have to specify it every time.
If you need a specific compliance level for a specific project, you need to verify every other project that need compliance level 1.6 or above is set to this.
After everything is correctly setup - clean projects and restart Eclipse. Eclipse can be such a bitch some times - this often solves problems for me.
Two things to consider:
1) Take a look at your imports - are you sure that View.OnClickListener is imported, but not lets say DialogInterface.OnClickListener
2) OnClickListener is actually an interface, that you are instantiating anonymously. So after all when writing the onClick method you are actually not overriding a super class method, but instead implementing an interface method. Annotating interface methods with #Override is a good practice, but this has been introduced in JDK 6, which means that by the time Android 1.5 or 1.6 was developed this may not has been yet introduced to the java language and hence making it an invalid syntax.
Right below the "Compiler Compliance Level", there are a few options grayed out if the "Use default compliance settings" checkbox is checked: Namely, "Generated .class files compatibility" and "Source compatibility". Verify that both of those are set to 1.6 - If not, either change the default compliance settings, or uncheck that box and tweak them directly.
Button buttonAuthorizeUsers = (Button) findViewById(R.id.buttonAuthorizeUsers);
buttonAuthorizeUsers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent configure = new Intent(OnDemandAndAutomatic_Activity.this,Configure_Activity.class);
OnDemandAndAutomatic_Activity.this.startActivity(configure);
}
});
try to replace this line
buttonAuthorizeUsers.setOnClickListener(new View.OnClickListener() {});
this error you got happened when your trying to assignee the On-click to unexpected type !
So, beleive me Eclipse IDE most of time will import DialogInterface instead of View so write it by your self.
daigoor is right. Eclipse always try to do this 'import android.content.DialogInterface.OnClickListener' instead of the doing this -> 'import android.view.View.OnClickListener'. That solves my problem.

Missing Something Easy (Button OnClick Causing Null Pointer)

Suddenly my buttons cause null pointer exceptions. I have not changed any code in this activity class. I have been up most of the weekend trying to pin this down. I get null pointer exceptions in both the emulator and on device.
This is a simple screen with buttons. I could easily scrap this and write new code, but would like to know what has caused this issue. If not, I will just write new code.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Button b1 = (Button) findViewById(R.id.MainActBtn);
b1.setOnClickListener(new View.OnClickListener() { //Error is here
public void onClick(View v) {
Intent intent = new Intent(StartScreen.this, MainActivity.class);
startActivity(intent);
}
});
Any help?
Try a fresh clean and build of your project. I got this error too in the past, caused by a changed R.class Resource class, but the static references in the using classes weren't properly replaced by the incremental java compiler, so the resources are no longer found.
I think You are not getting Null Pointer Exception because of the Button click just because of the declaration of the button..
You can check before writting the onClickListener..
if(b1==null)
Log.i("Null","Null");
The Problem is may be in setting the XML view or also check the button you are using is defined in the same xml you are using in setContentView.
After this all, also Clean and Build your project.

Categories

Resources