output for System.out.print("Hello") in Android program - java

I want to see any variable (String var) value in my android program.
You can say for debugging purpose.
When I am printing anything using, say System.out.print("Hello")
Then I am unable find this output any where.
Do anyone have idea where to find this output.
Here is my code-
package com.test1.nus;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.print("Hello");
....
}

By default, the Android system redirects stdout (System.out) output to /dev/null, which means your messages are lost.
Instead, the common pattern to log debug strings in Android is the following
import android.util.Log;
Then at the top of your class YourClass
private static final String TAG = YourClass.class.getSimpleName();
And to log debug strings you need to call
Log.d(TAG, "your debug text here");
which in your case results in
package com.test1.nus;
import android.util.Log;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Hello");
....
}
Finally you can see your debug strings in Eclipse via
Windows > Show view > Other and select LogCat
and if required filter by the tag of YourClass.
However, if you really need to see messages written by System.out.println you need to tell Android to route them to logcat via the following shell commands
$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start
and then you will be able to see your debug messages in Eclipse via LogCat view and the tag stdout.
You can get more details from the official documentation here http://developer.android.com/tools/debugging/debugging-log.html

Your output will be logged to logcat
Assuming you are using eclipse:
Window > Show View ---> Logcat (If this not visible, select other--Android--Logcat)

See this link: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html
It will show some screen shots related to logcat. There you can find out your output message.
Follow this: http://developer.android.com/tools/help/logcat.html

If you want to check your output in the android emulator use Toast messages.
The result with system.out.println("....") will be displayed in logcat.
To check in android emulator/device.just do like this
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();

I suggest you take a look at the android.util.log class. The Android developer pages has a good introduction for using it. (In fact, I just found this today since I'm learning Android programming myself.)

If you use the AVD s that will give you a more interesting experience than just print in the log. Here they have described all those things perfectly.

You are better off using Logs if it is for debugging purposes.
There are various methods available like Log.v() Log.d() Log.i() Log.w() and Log.e() for verbose, debug, info, warn and error logs respectively.
Then you can check the logcat while debugging the application.
For further reference, go here.

Related

Button type does not exist in Android Studio?

I am working on my first app in Android Studio and have created a button "buttonFish" in the xml creator that I now want to put an action onto in the mainactivity.java.
This is my code:
package com.example.acfaunapedia;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Private Button butf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butF = (Button) findViewById(R.id.buttonFish);
}
}
It can find my Button but it doesnt recognize the Type "Button".
Any ideas on how to fix this?
Error Message Link
EDIT:
I created an empty activity to start with, but I don't think thats relevant.
Alt-Enter is your friend. This Android Studio shortcut key will prompt you for solution(s) to error messages.
In this case, it will probably prompt you to add import android.widget.Button in your source, which should resolve the problem.
ALSO:
In the future, please copy/paste the error message text into your post. "Text" is generally more helpful than "screenshots" ;)
Here's the error (from your screenshot):
public class MainActivity extends AppCompatActivity {
Private Button butf; // <-- Cannot resolve symbol 'butf'
As nanofarad pointed out, the problem isn't "Buttom" per se (although that IS a problem), it's that you said "Private" instead of keyword private.
Alt-Enter should help with ANY error.
And once again: "Generally, text is better than screenshots"

WebView won't execute site commands

I have a WebView that loads this WebSite
If you try to put an identification and click out of the box, it shows a little loading and tells if the identification is correct it shows a virtual keyboard within the site, not a device keyboard, if its incorrect it shows a error message, like this
Site showing error
However, if i put the same website on my WebView, it loads the site perfectly, but it won't execute these commands, if i put any identification and click out it doesn't shows the loading, it won't do anything.
Funny part is, if i do the same WebView in Xcode for iPhone, it does everything normally, even the commands, it checks for the identification, etc...
What i've tried
Turning on/off:
Javascript
Plugins(even deprecated)
My code:
package com.example.viskee.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wbAba = (WebView) findViewById(R.id.wbAba);
wbAba.setWebViewClient(new WebViewClient());
wbAba.getSettings().setJavaScriptEnabled(true);
wbAba.loadUrl("https://ib.rendimento.com.br");
}
}
EDIT:
If i use
wbAba.setChromeClient(new ChromeClient);
it works fine, however i don't want to use ChromeClient because it adds an address bar, etc... I want my WebView to be FullScreen, show only the website
Have you try this:
WebView wbAba = (WebView) findViewById(R.id.wbAba);
wbAba.setWebViewClient(new WebViewClient());
wbAba.getSettings().setUseWideViewPort(true);
wbAba.getSettings().setLoadWithOverviewMode(true);
wbAba.getSettings().setJavaScriptEnabled(true);
wbAba.loadUrl("https://ib.rendimento.com.br");
and one final thing, don't forget to set internet permission on manifest.xml file:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Let me know, if this doesn't work.
I found a solution, all i had to do was:
wbAba.getSettings().setDomStorageEnable(true);

Log.d not showing log.d in Logcat Android Studio

Android studio does not show any Logs I wrote in my code. I have tried putting the log to verbose and debug. I am using 'No filters'. Why is 'Oncreatetestlog' not showing up in my logcat?
package com.example.keydown;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
Log.d("Oncreatetestlog", "onCreate() Restoring previous state");
/* restore state */
} else {
Log.d("Oncreatetestlog2", "onCreate() No saved state available");
/* initialize app */
}
}
}
Go to File -> invalidate caches / Restart. And let Android Studio index your project again. It works for me.
After hours of searching and trying, I found out it did not have to do with Android Studio, but that my phone didn't allow Logging. See this answer for more information.
Alternatively, you could use System.out.println() to show the text in logcat
Replace
Log.d("Oncreatetestlog", "onCreate() Restoring previous state");
With
System.out.println("Oncreatetestlog onCreate() Restoring previous state");
If Log.d is not showing in your Logcat just replace Log.d with Log.wtf Like this:
Replace:
Log.d("tag",""+catstr);
With:
Log.wtf("tag",""+catstr);
It Works..happy coding;)
Please check in "Run" on the right side of "Debug" on the bottom of the IDE.
in my case i actually did everything what they told but yet it's not working. so i checked there and i fount all the log over there.
thanks

Manually registering a new activity in manifest.xml

tl;dr Look at title + netbeans
I've been writing a game as I have been learning both Java and Android*. Most of it has gone without a hitch- but here we go.
To my understanding- each screen (main menu, gameplay, highscores, ect) is a separate Activity and therefore needs a different layout.
Problem 1:
EDIT: Solved. Thanks Daniel.
Problem 2: Then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
package lolfighter.notriot;
import android.app.Activity;
import android.os.Bundle;
import lolfighter.notriot.nojoke.R.*;
// #author DEVELOPMENT
public class GameActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(layout.main_1);
}
Also why is the bundle icicle? I have not specified this anywhere.
Edit- Not solved
*- I am in a partnership of sorts with a friend- the original plan was that he would handle all the code and I would handle all the graphic resources (He took an actual class for the Java language) But he has ran into a roadblock. I can't ask him because he has limited internet access (lunch breaks only) and lives in a different state (we go to college together)
Problem 2: then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
Change layout.main_1 to R.layout.main_1.
Also why is the bundle icicle? I have not specified this anywhere.
You can rename the variable to be whatever you want, it doesn't matter.

How to use a button to within a app to display the phone's contact list

Hello everyone I am completely new to programing no experience at all so please treat me as a 2 year old when you respond. I have alot of time on my hands as I sit in an office with nothing to do so I figure I should do something productive, and since I have a android tablet I figure I'll try to learn how to make apps for it. This is my first attempt and I'm getting an error in the .java portion of the code, I've been reading and reading on various sites and can't seem to get a clear understanding of what I need to do to correct it. PLEASE HELP
Now down to business, in the app I am trying to learn how to display an image its that simple, I'm taking baby steps. So I started a project, read a few tutorials on how to code ImageView and then I got stuck. I have the error that says "The public type TestImages must be defined in its own file" so I ask how to define/create this file? where to put it?
Here is the .java portion of the code where the error lies.
package com.example.myproject;
import com.example.myproject.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class TestImages extends Activity {
/**Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
ImageView image = (ImageView) findViewById(R.id.test_image);
}
}
Please help, and if you know of any other resource out there that can help a newbie/dummy like myself who don't know anything about coding and need every detail explained please point me in that direction. I've tried the Android Developers site but the information presented on there are not explained in detail it looks as if they expects everyone who tries to do this have a background in coding and I don't. Oh and please don't discourge encourage, because I'm eager to learn.
use on click listener on the button and then check this
https://github.com/android/platform_packages_apps_contacts

Categories

Resources