I am new to Android Studio. I successfully created a Hello World app from the example in Android website. Now, I want to play around by using some statement to print in the logcat but it doesn't works. Below is my ApplicationTest.java code:
package com.example.abc.myloggingapplication;
import android.app.Application;
import android.test.ApplicationTestCase;
import android.util.Log;
/**
* Testing Fundamentals
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
Log.d("MyTest", "Here goes the output!"); // THIS IS THE NEW STATEMENT INSERTED BUT PRINTS NOTHING IN CONSOLE LOG IN ANDROID STUDIO SDK.
}
}
Other files are same as provided by the Hello World example in Android website. In Android Studio sdk, I entered the logcat to debug but still no luck. Can anybody point me out where I am going wrong.
Check your logcat:
Check View -> Tool windows -> Android Monitor.
Or Alt + 6
If that doesnt help, make sure you have an instance of your class. Otherwise the constructor is never called and therefor the log.d is never called.
Edit:
As other's have stated: Check if you are running the Test Application, if you want to do so. Otherwise make sure you code in your actual application and not your test application.
Related
I am just trying to run a simple test on my "hello world" programme in android studio , the programme is below:
package com.example.gautam.droidone.tests;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
import com.example.gautam.droidone.MainActivity;
import com.example.gautam.droidone.R;
import android.util.Log;
/**
* Testing Fundamentals
*/
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
MainActivity activity;
public MainActivityTest() {
super(MainActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
activity = getActivity();
}
public void testMainActivity() {
TextView textView = (TextView) activity.findViewById(R.id.hello_world);
Log.d("MainActivityTest", textView.getText().toString());
assertNotNull(textView);
}
}
When i click on the debug button, i get the following error in the console:
Error running MainActivityTest: Unable to open debugger port (127.0.0.1:58061): java.net.SocketException "socket closed".
Similar so threads:
Thread 1
Thread 2
Thread 3
I googled the error and found a few solutions to the problem, most of them did't work, then i saw THIS article and in the comments section a few people have suggested that you need to turn off build-process. how do you do that , on reading more on google , i found that since android 1+ this option is no longer available , so how do i turn off build process ? and once again why am i getting this error:
Error running MainActivityTest: Unable to open debugger port
(127.0.0.1:58061): java.net.SocketException "socket closed"
I had the same problem. My fault that in the tool bar the wrong target was selected.
It has to be "App". It can easily been selected wrong with a single unintended mouseclick.
So check if there is "App" or something like "assemble".
Lately, I have been trying to add static interstitial ads into my Unity game. For some reason, I could not get the system to show anything, or even react to me. After trying to work with the base Chartboost plugin, I tried to match a tutorial that I was following and purchased Prime31's Chartboost plugin and have been using that. However, neither the base plugin, nor Prime31's plugin, seem to be allowing me to show any ads. The code is pretty much done inside a single object, and it seems simple enough.
public class Advertisement : MonoBehaviour {
public string chartboostAppID = "5461129ec909a61e38b1505b";
public string chartboostAppSignature = "672b3b34e3e358e7a003789ddc36bd2bc49ea3b5";
// Use this for initialization
void Start () {
DontDestroyOnLoad(this.gameObject);
ChartboostAndroid.init (chartboostAppID, chartboostAppSignature, true);
ChartboostAndroid.cacheInterstitial(null);
}
void OnLevelWasLoaded(int level) {
ChartboostAndroid.cacheInterstitial(null);
if(Application.loadedLevelName == "Network Lobby") {
showAds();
}
}
public static void showAds() {
Debug.Log("Showing ad");
ChartboostAndroid.showInterstitial(null);
}
}
As you can see, it's pretty straightforward. This object is created at the game's splash screen, which appears only once, and it's never destroyed until the program ends. The goal is, whenever I enter the lobby scene, I want to see an ad before going to the lobby's menus. As it is, I do see the log printing "Showing ad", so I know the function is being called. However, nothing appears. Do I need to disable the GUI system first? Is there a step I'm missing?
I have already performed the following steps:
I have created and registered the app with chartboost, as well as double and triple checked the AppID and App Signature.
I have created a publishing campaign and registered it to the app.
I double-checked the orientation and confirmed that it's correct.
I registered this specific device as a test device.
The tutorial showed a call to ChartBoostAndroid.OnStart(), but there was no function like that for me to call. Perhaps that is from an older version?
I emailed Chartboost support and have not heard from them yet. I do not have that much time on this project, so if anyone can offer help, I'd appreciate it.
This question already has answers here:
capturing and sending logcat output by email or to server
(2 answers)
Closed 9 years ago.
I installed the .apk file in the device then run that application at this time suppose i am getting any exception then application will be crash. At this time i want to see that exception in the device without using Eclipse Logcat. It means i want send that exception to file(means some path in the device like sdcard/downloads/a.txt)in the device using Log.
Generally some applications are working properly in the emulator but in case of device we got some exceptions. so thats why i want see that particular exception in the device using Log.
Is it possible? How can i implement this? can anybody help me.
Myapplication classs:
package com.ibkr.roadbrake;
import org.acra.*;
import org.acra.annotation.*;
import android.app.Application;
import android.os.Bundle;
#ReportsCrashes(formKey = "dDJ5VFhURVNHakhSa3hfTndteFd6Smc6MQ")
public class MyApplication extends Application
{
#Override
public void onCreate()
{
// The following line triggers the initialization of ACRA
ACRA.init(this);
super.onCreate();
}
public Bundle getCrashResources()
{
Bundle result = new Bundle();
String RES_TOAST_TEXT = null;
result.putInt(RES_TOAST_TEXT, R.string.crash_toast_text);
return result;
}
}
thanks
Log4j or slf4j can also be used as logging frameworks in Android together with logcat. See the project android-logging-log4j and log4j support in Android. Configuring logging to a (rotating) file(s) is very easy.
static {
final LogConfigurator logConfigurator = new LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "myapp.log");
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel("org.apache", Level.ERROR);
logConfigurator.configure();
}
Im having a problem starting or running any activity unit tests from within eclipse.
Even i start a clean project and make a simple test class it always prints to the console:
[2010-10-05 13:10:24 - testAndroid] Collecting test information
[2010-10-05 13:10:25 - testAndroid] Test run failed: Test run incomplete. Expected 2 tests, received 0
Any ideas ?
Just for testing, I have created a fresh Android project called Demo with a test project called DemoTest
The main activity to test is called Main and I have created a simple testclass MainTest that looks like this:
package net.demo.test;
import android.test.ActivityInstrumentationTestCase2;
import net.demo.Main;
public class MainTest extends ActivityInstrumentationTestCase2<Main>
{
public MainTest()
{
super("net.demo", Main.class);
// TODO Auto-generated constructor stub
}
}
My tests used to run fine before, but suddenly I cant run any of them, they all fail with the same error, even I create new a project. It seems like it something to do with Eclipse or and not with the Code.
Update:
Seems like extending SingleLaunchActivityTestCase<Main> is working, but still got no clue about how to make ActivityInstrumentationTestCase2<Main> working.
I had no regression problems. I just couldn't get the example to work. I finally fixed it by defining two constructors:
public MainActivityTest(String pkg, Class<MainActivity> activityClass) {
super("com.myapp", MainActivity.class);
}
public MainActivityTest() {
super("com.myapp", MainActivity.class);
}
It turned out that most emulators before 2.3.3 were silently swallowing the error generated when construction went wrong.
You must put at least 2 methods (i.e 2 test cases) into the Test class. even methods without definition inside can do the trick
I am completely new to android, and pretty much a Java newb.
I have a simple app that I am building to get the hang of android's development environment - and things like click events, etc..
The app loads, and I am able to change the text in a textfield using a button handler. However, when I import the location class, and try to do a simple GPS call, the application crashes.
The problem is, everything looks good in Eclipse (error console) - and I'm not seeing any exceptions in the android emulator (DevTools). I have the logcat window open, but I haven't done anything in eclipse/code to send logcat anything (do I need to?)
Can anyone see something wrong with this? Is there a better way to troubleshoot?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.location.*;
public class locationDisplay extends Activity {
private EditText text;
private Location GPSLocation;
double dblLat;
double dblong;
String strLat;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // bind the layout to the activity
text = (EditText) findViewById(R.id.EditText01);
text.setText("No button pressed");
}
// Handler for each button -- Button01 is when it crashes
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.Button01:
dblLat = GPSLocation.getLatitude();
strLat = Double.toString(dblLat);
text.setText(strLat);
break;
case R.id.Button02:
text.setText("Button 2 was clicked");
break;
case R.id.Button03:
text.setText("Button 3 was clicked");
break;
}
}
You shouldn't need to write anything to get the default messages in LogCat; uncaught exception reports should appear automatically when your program crashes. However, sometimes LogCat and your emulator get disconnected from each other and the messages simply all disappear. Simply close Eclipse and the emulator, restart them both, and the messages should reappear. An easy way to tell whether the link has been re-established is during the boot-up of the emulator. Just as the flashing "ANDROID" text in the fancy font disappears bringing you to the lockscreen, you should see about a hundred lines of text flash by on LogCat. If that doesn't happen, then LogCat isn't getting its messages.
The way to display debugging messages in Android is to use the Log.d("some name for your log statements so you can filter the LogCat messages", "The actual debug statement here");. You'll often find people using things like a static final String LOG_TAG in their application so that they can make sure their logs always have the same tag, and hence, the filter never misses a message.
As for your actual code here, Rpond is right, you never initialised your GPSLocation object.
You GPSLocation object is null. You need to access the LocationService to get a current location. And with the emulator you will need to manually send locations.
Location Services
Sometimes LogCat 'forgets' you have a device/emulator connected and running. It seems like this happens after you have a device and an emulator online at the same time and then you disconnect one of them. If you are getting nothing from LogCat, go to Window>Show View>Other>Devices and then click the device that you want to log.