I was working fine. My p.c powered off due to light. When I start again my android studio, there came a lot of errors in my all java files. How can I solve them?
public class MainActivity extends Activity {
private boolean detectEnabled;
private TextView textViewDetectState;
private Button buttonToggleDetect;
private Button buttonExit;
FlashLight flashLight;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewDetectState = (TextView) findViewById(R.id.textViewDetectState);
buttonToggleDetect = (Button) findViewById(R.id.buttonDetectToggle);
buttonToggleDetect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setDetectEnabled(!detectEnabled);
}
});
buttonExit = (Button) findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this , FlashLight.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void setDetectEnabled(boolean enable) {
detectEnabled = enable;
Intent intent = new Intent(this, CallDetectService.class);
if (enable) {
// start detect service
startService(intent);
buttonToggleDetect.setText("Turn off");
textViewDetectState.setText("Detecting");
}
else {
// stop detect service
stopService(intent);
buttonToggleDetect.setText("Turn on");
textViewDetectState.setText("Not detecting");
}
}
}
Here is a screenshot of my issue:
Because we don't have enough information, we can't tell you. This has happened to me before though, so I can give you a few tips:
Just wait until the gradle build is done, often times there are random errors which resolve themselves after a few minutes. This especially happens with resources that you are getting based on an id, R from R.id shows as an error until the gradle build is finished
Clean your project. Build->Clean Project
Rebuild Project. Build->Rebuild Project
Restart Android Studio
Restart Computer
Honestly, we can't give you more information than that until we get more details. Chances are it's number 1 that is the problem, so just wait.
{Rich}
Build--> Clean Project
Build-> Rebuild Project
Go to File
Then Invalidate Cache/ Restart
After that hit Invalidate and Restart
It's not a pleasant moment when your computer suddenly turns off and corrupting your project files. Sadly, when using Windows, you have a small hope that your files integrity are still intact when the Windows suddenly turn off due to power failure or having a friendly BSOD. I've experienced project error twice in a month because of it. Windows can't be counted for reliability, tried Linux instead.
To prevent another files error (or should I says, corrupted files?), you should always back up your project. Use a Version control systems (VCS) like git. Then copy/clone your project to USB flash drive. Or you can use a free private repository for git:
Bitbucket
GitLab
Always, always, always backup.
Build->Clean Project. This will solve your problem.
Related
I can't get the context of my activity for some reason. Note - It was working before but now Android Studio shows an error but does not stop my app compiling and running as expected. I've added my code further down but ultimately I think the problem is somewhere else because if I try to get the activity context in a new, empty activity, I get an error.
package com.example.myapp
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Context context = this; // Error here
}
}
The error is:
Incompatible types.
Required: android.content.Context
Found: com.example.myapp.TestActivity
This error only happens for this project. My searches for an answer have yielded no positive results. In fact I can't find anything on the exact issue I'm facing.
What I have tried to fix this:
this instead of MainActivity.this - same error as above
getApplicationContext() - cannot resolve method error
getActivity().getApplicationContext() - same error as #2
Clean & Rebuild project / Sync Project with Gradle Files
Restarting Android Studio
Android Studio versions 2.3.3 & 3.0 - same issue
I'm new to Android development so if you have a solution for me, please phrase it as simply as possible. Thanks in advance. Here is my code - I get the error in the onClick method where it says MainActivity.this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the Numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);
// If View is present, set a click listener on that View
if(numbers != null) {
numbers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
startActivity(numbersIntent);
}
});
}
}
}
I found a solution and that was to add google() to my project's build.gradle file like so:
allprojects {
repositories {
jcenter()
google()
}
}
I also deleted the project and cloned it again so it could build from scratch. Not sure whether that helped or not.
I'm trying to get my application to save some data when the orientation of the screen is changed using the onSaveInstanceState to save a boolean value mCheated.
I've set numerous break points and am getting an error for the mCheated boolean value in the variables view
mCheated= No such instance field: 'mCheated'
I have no idea why as I declare it with a value false when the activity is started and change it to true if a button is pressed. Can anyone help me out?
package com.bignerdranch.android.geoquiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Chris on 20/02/2015.
*/
public class CheatActivity extends Activity {
public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.geoquiz.answer_shown";
private static final String KEY_INDEX = "index";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private boolean mCheated = false;
private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
if (savedInstanceState != null){
mCheated = savedInstanceState.getBoolean(KEY_INDEX, mCheated);
}
setAnswerShownResult(mCheated);
mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
}
else {
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
mCheated = true;
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
//Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putBoolean(KEY_INDEX, mCheated);
}
}
It turns out there wasn't a problem with the code and that Android Studio required a restart. I think it was down to the fact I had cloned the project and was possibly using an incorrect file from the previous version.
Check if your build variant in Android Studio has
debuggable as true
proguard is disabled or commented out.
I had the same error.
The solution to the error is to disable the Proguard in build.gradle file.
debug {
minifyEnabled false
}
I got the same error and I wasted my 3-4 hours to resolve same error finally I got to know why that happened and it was interesting
In my case, I changed the code in one file (I declared one variable and initialized it)
I run the apk from my device and attached debugger from android studio
I set debug point to that newly added variable where I assigned data to it
but during debugging it shows me same error
Then I got to know I changed the code in file but I run the apk from device, and I attached debugger I need to compile and run the changes instead of it how it will reflect in apk that was the actual issue
So I compiled and ran the code and installed newly compiled apk on device then I attached debugger and it worked for me
hope this will save someone's time
If you're using pro-guard and obfuscation is true.
you have to comment out obfuscation in build gradle
eg: add this in the pro-guard -dontobfuscate
An internal error occurred during: "Launching New_configuration".
Path for project must have only one segment
I only just got through a previous problem to straight away come to the next one , also when i try and created AVD , when i press the button "okay" , it does nothing , the box does not even close , any help ? cheers ,
This should fix the probelm:
Project -> Properties -> Run/Debug Settings: 1. select "Launching New_configuration" 2. Delete 3. OK
Ok Beefydeadeye, The Code that is in the activity you start with is:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//put code between here
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
It may look a little differnt for you,
This code defines the layout (Gui for the Activty) and the menu xml, if you create another activty in the project it will not start you with this code.
I am able to display a map and update my location. However I added this button to take a screenshot of the current map. I believe I have all the proper imports and the google play library because the map is displaying properly. Eclipse tells me in this:
new GoogleMap.OnMapLoadedCallback()
"GoogleMap.OnMapLoadedCallback cannot be resolved to a type"
Here is the code.
Button screen = (Button) findViewById(R.id.button5);
screen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "ScreenShot in sdcard",
Toast.LENGTH_SHORT).show();
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
public void onMapLoaded() {
mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
public void onSnapshotReady(Bitmap bitmap) {
// Write image to disk
FileOutputStream out = new FileOutputStream("/mnt/sdcard/map.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
}
});
}
});
What do I need to do to make this resolve to a type and what does that mean?
An update to the Google Play services Library was released November 2013 (Google Play services 4.0.30). The previous library did not include the OnMapLoadedCallback interface.
Instructions for installation of the update for incorporation into your android project can be found at
http://developer.android.com/google/play-services/setup.html
I'm trying to use Google Analytics in my Android application with
Google Configuration
Add .jar in my project
Insert this in AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Add this in my java file
public class MainActivity extends Activity {
GoogleAnalyticsTracker tracker;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession("My-UA–XXXXXXXX", this);
setContentView(R.layout.main);
Button createEventButton = (Button)findViewById(R.id.NewEventButton);
createEventButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tracker.trackEvent(
"Clicks", // Category
"Button", // Action
"clicked", // Label
77); // Value
}
});
setContentView(R.layout.main);
Button createPageButton = (Button)findViewById(R.id.NewPageButton);
createPageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Add a Custom Variable to this pageview, with name of "Medium" and value "MobileApp" and
// scope of session-level.
tracker.setCustomVar(1, "Navigation Type", "Button click", 2);
// Track a page view. This is probably the best way to track which parts of your application
// are being used.
// E.g.
// tracker.trackPageView("/help"); to track someone looking at the help screen.
// tracker.trackPageView("/level2"); to track someone reaching level 2 in a game.
// tracker.trackPageView("/uploadScreen"); to track someone using an upload screen.
tracker.trackPageView("/testApplicationHomeScreen");
}
});
Button quitButton = (Button)findViewById(R.id.QuitButton);
quitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
Button dispatchButton = (Button)findViewById(R.id.DispatchButton);
dispatchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Manually start a dispatch, not needed if the tracker was started with a dispatch
// interval.
tracker.dispatch();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
// Stop the tracker when it is no longer needed.
tracker.stopSession();
}
}
==> And it's ok, no error, compiling and executing but i have created my ua account yesterday (more 24h) and i have nothing in my google analytics panel.
My Question : is there an error in my code or i want to wait again ? Live trafic works for Android application (like tradicional website) ???
I have no information about Live trafic (when i play my app, i would like to show the number of person using my application) and Saved trafic (with viewed pages, time)
Thank you for your replies and excuse my poor english :)
bye
UPDATE 1 :
i've used this tuto : http://www.xatik.com/2012/03/27/how-to-use-google-analytics-in-android-applications/ and i've got this in my Logcat :
04-07 14:21:59.669: INFO/GoogleAnalyticsTracker(864): Host: www.google-analytics.com
04-07 14:21:59.669: INFO/GoogleAnalyticsTracker(864): User-Agent: GoogleAnalytics/1.4.2 (Linux; U; Android 2.2; en-us; sdk Build/FRF91)
04-07 14:21:59.669: INFO/GoogleAnalyticsTracker(864): GET /__utm.gif?utmwv=4.8.1ma&utmn=235327630&utme=8(1!Navigation%20Type)9(1!Button%20click)11(1!2)&utmcs=UTF-8&utmsr=240x320&utmul=en-US&utmp=%2FtestApplicationHomeScreen&utmac=BLIBLUBLIBLO–1&utmcc=more_and_more
in progress but nothing in my Live Analytics panel....
i've added EasyTracker .jar in my project
Here my Activity Code:
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import com.google.android.apps.analytics.easytracking.EasyTracker;
import com.google.android.apps.analytics.easytracking.TrackedActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends TrackedActivity {
GoogleAnalyticsTracker tracker;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button quitButton = (Button)findViewById(R.id.QuitButton);
quitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EasyTracker.getTracker().trackEvent("ButtonClick", "MyButtonName", "", 0);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
//How can i stop the tracking onDestroy ???
}
}
I know this is a couple months old but I'll give a response to potentially help others. I am the person who wrote the guide that was referenced in Update 1 of the OP. One issue I originally ran into was the fact that I was using a custom ROM on my phone. A lot of custom ROMs have modified 'hosts' files that block an apps access to display ads or in this case blocks the sending of data to Google Analytics. If you do have a custom ROM on your phone, you can check the 'hosts' file to see if Google Analytics is listed in there. The fastest way to do this is to open the file in a text editor on your computer. To do this:
Get a file explorer app on you android device (I use 'ES File Explorer').
Navigate to '/etc'.
Locate and copy the 'hosts' file to a known location on your SD card.
Connect phone/SD card to computer and open the 'hosts' file in a text editor (Notepad++ is nice and free).
Search through file for anything that relates to Google Analytics and delete it. I first searched for 'analytics', went through all results, and deleted everything that had something to do with Google attached to the name (there are other analytic sites). Then I searched for 'google', went through all the results, and deleted anything that still related to Analytics.
Save 'hosts' file.
Disconnect from computer and use file explorer to copy the 'hosts' file from SD card back to '/etc' and overwrite.
This should allow your phone to send data to Google Analytics. I will update my guide to include this somewhere.