-1

I am a beginner in android..Can anyone tell me how to debug android script and how breakpoints and logcat works? What are the things in the logcat? I tried searching for this...But i couldnt find any!! this is my logcat

05-07 13:22:31.058: I/dalvikvm(609): Could not find method 
android.content.pm.PackageManager.getActivityLogo, referenced from method 
android.support.v7.internal.widget.ActionBarView.<init>
05-07 13:22:31.058: W/dalvikvm(609): VFY: unable to resolve virtual method 318:           
Landroid/content/pm/PackageManager;.getActivityLogo   
(Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
05-07 13:22:31.058: D/dalvikvm(609): VFY: replacing opcode 0x6e at 0x008b
05-07 13:22:31.058: I/dalvikvm(609): Could not find method     
android.content.pm.ApplicationInfo.loadLogo, referenced from method    
android.support.v7.internal.widget.ActionBarView.<init>
05-07 13:22:31.068: W/dalvikvm(609): VFY: unable to resolve virtual method 314:     
Landroid/content/pm/ApplicationInfo;.loadLogo     
(Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
05-07 13:22:31.088: D/dalvikvm(609): VFY: replacing opcode 0x6e at 0x0099
05-07 13:22:31.108: D/dalvikvm(609): VFY: dead code 0x008e-0092 in     
Landroid/support/v7/internal/widget/ActionBarView;.<init> 
(Landroid/content/Context;Landroid/util/AttributeSet;)V
05-07 13:22:31.108: D/dalvikvm(609): VFY: dead code 0x009c-00a0 in     
Landroid/support/v7/internal/widget/ActionBarView;.<init> 
(Landroid/content/Context;Landroid/util/AttributeSet;)V
05-07 13:22:31.458: D/AndroidRuntime(609): Shutting down VM
05-07 13:22:31.458: W/dalvikvm(609): threadid=1: thread exiting with uncaught exception     
(group=0x4001d800)
05-07 13:22:31.478: E/AndroidRuntime(609): FATAL EXCEPTION: main
05-07 13:22:31.478: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start     
activity ComponentInfo{newapp.com/newapp.com.MainActivity}:     
java.lang.NullPointerException
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
android.os.Handler.dispatchMessage(Handler.java:99)
05-07 13:22:31.478: E/AndroidRuntime(609):  at android.os.Looper.loop(Looper.java:123)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
android.app.ActivityThread.main(ActivityThread.java:4627)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
java.lang.reflect.Method.invokeNative(Native Method)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
java.lang.reflect.Method.invoke(Method.java:521)
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-07 13:22:31.478: E/AndroidRuntime(609):  at dalvik.system.NativeStart.main(Native     
Method)
05-07 13:22:31.478: E/AndroidRuntime(609): Caused by: java.lang.NullPointerException
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
newapp.com.MainActivity.onCreate(MainActivity.java:32)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-07 13:22:31.478: E/AndroidRuntime(609):  at     
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-07 13:22:31.478: E/AndroidRuntime(609):  ... 11 more
05-07 13:22:41.409: I/Process(609): Sending signal. PID: 609 SIG: 9

this is the main activity code

package newapp.com;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

int counter;
Button add,sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    counter=0;
    add=(Button) findViewById(R.id.badd);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter++;
        }
    });
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

And here is the xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="newapp.com.MainActivity$PlaceholderFragment" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="The sum is" 
    android:textSize="45dp"
    android:gravity="center"
    android:id="@+id/tdisplay"
    />
<Button 
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="add"
    android:id="@+id/badd"
    />

<Button
    android:id="@+id/bsub"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="subtract" />

</LinearLayout>

thank you

Sai Shiva
  • 17
  • 7

3 Answers3

0

You need to add log statements in the code to see details in the Logcat.

In your logs line

05-07 13:22:31.478: E/AndroidRuntime(609): FATAL EXCEPTION: main
05-07 13:22:31.478: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start     
activity ComponentInfo{newapp.com/newapp.com.MainActivity}:     
java.lang.NullPointerException

states that a NullPointerException was found due to which Android framework was not able to start activity component. Then you will find the root cause of this exception in the trailing logs

05-07 13:22:31.478: E/AndroidRuntime(609): Caused by: java.lang.NullPointerException
05-07 13:22:31.478: E/AndroidRuntime(609):  at 
newapp.com.MainActivity.onCreate(MainActivity.java:32)

Above line says that a NPE occurred in onCreate() of MainActivity at line 32. So need to figure out what wrong is happening there.

A nice tutorial can be found here

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
  • it's more important to understand what NPE is before solving it. When you get NPE, it means some reference is pointing to `null` and you are trying to operate on that reference. That is, you are operating on null (something like null.somemethod()). this leads to NPE. Solution your reference variable should point to appropiate value, and you need to figure out that. Post the code for exact solution – Gaurav Gupta May 07 '14 at 13:02
0

Debugging in Android is very simple. After running your project you can click Logcat for viewing log information. there are five types of logs are available. Kindly find the below details.

Log.d(TAG,"message"); --- Used to Print the Debug Messages ( blue color strings in Log cat)

Log.v(TAG,"message"); --- Used to Print the verbose Messages ( black color strings in Log cat)

Log.i(TAG,"message"); --- Used to Print the information Messages ( green color strings in Log cat)

Log.e(TAG,"message"); --- Used to Print the error Messages ( red color strings in Log cat)

Log.w(TAG,"message"); --- Used to Print the warning Messages ( yellow color strings in Log cat)

To find the crash then it will list the class hierarchy of crash(means where its get crashed). It will list with line numbers. for ex from your question

Caused by: java.lang.NullPointerException
 05-07 13:22:31.478: E/AndroidRuntime(609):  at 
  newapp.com.MainActivity.onCreate(MainActivity.java:32)

The crash occured at the line no 32 in Main Activity. You can click on that then it will redirect you to that line.

For More information kindly visit the below sites:

http://developer.android.com/tools/debugging/debugging-log.html

How to open LogCat in Eclipse (for Android Debug)

Community
  • 1
  • 1
Sivakumar
  • 633
  • 4
  • 9
0

You might just wanna look for the first message that is an error (E/...). In your case E/AndroidRuntime(609) and read downwards.

05-07 13:22:31.478: E/AndroidRuntime(609): FATAL EXCEPTION: main
05-07 13:22:31.478: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start     
activity ComponentInfo{newapp.com/newapp.com.MainActivity}:     
java.lang.NullPointerException
....

As you can see here it states that quite early on that you have a NullPointerException, meaning that some object you were using had null as a value - for whatever reason (there are plenty of possibilities).

Looking further down you find ...

... at newapp.com.MainActivity.onCreate(MainActivity.java:32)

telling you filename (MainActivity.java) and line number (32)

There are also plenty of tutorials out there on how to actually use the debugger, stepping, etc. you might consider searching for "debug java" or "debug eclipse" instead of looking for android to start with.

Debugging Eclipse

Levite
  • 17,263
  • 8
  • 50
  • 50