Console of my Android Studio does not print the log message - android

I created a Hello, World project in Android Studio to test using System.out.println(). I believe the log message should be printed in the console, but it wasn't. Im using Android Studio AI-130.737825 with JRE:1.7.0_25. The test code follows:
package com.example.consoletest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("please print me in the console");
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;
}
}

The console is not "connected" to the running app because it is run on a different system (be it an emulator, or physical device). The only "connected" part in Android Studio is the LogCat, which can be accessed using the Android tab at the bottom of the IDE.
You should rather print output to LogCat using the Log.* methods, which provides a lot more control and information, in almost the same simplistic way. Additionally, the logcat can be filtered to find exactly what you want.

As #free3om hinted at, Log.* can be used to print out many different outputs to the Logcat. If you want to see just errors, you can use Log.e(String s1, String s2) to see what and where something went wrong. Here's a link to the doc's for Log. http://developer.android.com/reference/android/util/Log.html

Related

After migrating to AndroidX: Cannot instantiate class: androidx.appcompat.widget.ShareActionProvider

I migrated to AndroidX (using the wizard in Android Studio), and I'm having problems with the share action provider. The wizard changed (among many other things) app:actionProviderClass="android.support.v7.widget.ShareActionProvider" to app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider", in my detailactivity.xml file.
The app compiles fine, and runs fine, too -- so long as I install it on my device over USB. However, if I compile a signed APK, and install that, I get the following runtime error (when starting the detailfragment):
W/SupportMenuInflater: Cannot instantiate class: androidx.appcompat.widget.ShareActionProvider
I didn't notice this problem while developing, since I run/test the app on my device through USB. However, when I'm now testing the (signed/minified) APK, the SHARE button does not work. How can I troubleshoot and fix this? For instance, why does it fail on the signed/minified APK fail, while it works fine when installing on same device through USB?
It's hard to tell specifically where (in the code) the warning occurs, since the code in the APK is minified. Perhaps I could create an APK where the code is not minified, so I'd get proper references to lines in the source code (in Android Studio LogCat)?
For reference, here is an excerpt from the class where the warning occurs. I'm assuming the warning occurs somewhere here, as this is what's referencing the shareActionProvider?
import androidx.appcompat.widget.ShareActionProvider;
public class ScreenSlidePageFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private ShareActionProvider mShareActionProvider;
public ScreenSlidePageFragment() {
setHasOptionsMenu(true); // only the share button
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detailfragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if (mShareActionProvider != null && mImageData != null) {
mShareActionProvider.setShareIntent(createShareImageIntent());
}
}
}
I got some help on #android-dev (IRC), and it seems the problem was that the minifier (for some reason) removes the androidx.appcompat.widget.ShareActionProvider class. Turning off minification produced a working APK.
The fix was to update my proguard-rules.pro file with a new "keep" line, to prevent it from removing the class. From before, I had a similar rule for the old Android Support Library, so I added the second line below and now it works.
-keep class android.support.v7.widget.** { *; }
-keep class androidx.appcompat.widget.** { *; }
thanks #melatonina!

Errors starting with Eclipse

I am new with JavaScript and Developement on Android. I installed Eclipse but I can't even run the "Hello World" program, included when you created a new project !
Please help me, I spent the two last days installing this IDE (even tried Android Studio),
but there is always the same error :
Could not find Chwet.apk!
And there seems to be an error with the "R", like in
setContentView(R.layout.activity_main)
Here is my code :
package com.example.chwet;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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;
}
}
Look at following tutorial to get start with Android application development.
Hello world example.
Instead of importing android R class , import R class with your package name.
Press Ctrl+shift+o to add missing packages. It will automatically remove your errors.

R cannot be resolved to a variable without doing anything to the project

I am getting the error "R cannot be resolved to a variable" that many people seems to be getting. However, none of the answers that I have seen has worked for me. I've gone to the extreme basic and created a helloworld project, with all default settings. The "R cannot be resolved to a variable" error is created instantly, and I cannot see what is wrong... no xml errors, all lowercase in /res/ folder, no difference in AndroidManifest.xml.
For reference, the code on MainActivity.java is:
package com.test.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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;
}
}
Is there something wrong with eclipse setting?
I'm using Windows 7 64-bit
Whenever I had a problem with R not been generated, or even disappeared, first clean and rebuild the project, if this is not worked then it was due to some problem in the XML layout file that prevented the application from being built. Check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.

Android: R cannot be resolved to a variable

Ok, I know there are several questions like this, but I've been through all of them and nothing seems to work! I have been using eclipse for almost a year now. I installed new softwares from the Help menu, and since then nothing is working! I re-installed eclipse and now I'm getting this "R cannot be resolved to a variable", even when I create a brand new project. Here's the code:
package test.pro;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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;
}
}
I am getting an error in the setContentView and getMenuInflater lines. I looked in the 'gen' folder, there's nothing there! I also checked the Java Build Path, it's fine. The ADT is updated. Can someone help me out here!!? I really need it!
Usually this error is because of some error in layout, strings, dimensions or any other xml file. Look for such xml error, resolve it and you should be good to go.
Also do perform a clean of your project. This error is very common with me too.

Android R & layout error

I am a beginner Android app developer. I created an XML layout called "splash" and I have changed setContentView(R.layout.activi­ty_main); to setContentView(R.layout.splash­); but got an error. I tried to do everything all over again but I got the same error in the R. When I did a "quick fix" a new error appears. It was caused by activity_main and splash.
When I started all over again, I just changed the background in activity_main but but I got the same error although I haven't modified anything in MainActivity.java.
Here is my code so far:
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;
}
You need to be careful when importing R because the Android OS provides one with system defaults etc.
Have a look in your imports to make sure you're getting the R from your package.
You might also see issues if your layout doesn't compile.

Categories

Resources