Confused by "Sorry! The application __ (process __)...&

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Confused by "Sorry! The application __ (process __)...&

Postby illiniwatcher » Mon Nov 03, 2008 8:16 am

Hi, folks:

I am really confused about Android at this point. I'm writing a very simple application called "Counter" (a starting point) that uses a drawabl PNG image and no matter what I do, when I run it in the emulator, I get this annoying message:

"Sorry! The application Counter (process mine.counter) has
stopped unexpectedly. Please try again. [Force close buttton]"

All I want my application to do is to display a Drawable called "n0.png" which I have defined to my "rsc\drawable" folder.

I've looked at other sample applications, including the ones in the SDK and tried emulating the coding, without success. I'm very confused about how the Java, XML, and other files interact. I just don't see why the program is freezing when I've practically followed the structure of other applications to the letter.

Below, I've replicated the code. Thanks for any help you can provide.

Charles

***************

FILE STRUCTURE

Counter
...res
......drawable
.........n0.png
......layout
.........bkcounter.xml
.........main.xml
......values
.........colors.xml
.........strings.xml
...src
......mine
.........counter
............Counter.java
............CounterView.java
............R.java


In BKCOUNTER.XML:

<?xml version="1.0" encoding="UTF-8"?>

<!-- Demonstrates using a relative layout to create a form -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10px"
android:background="@color/blueback">

<!-- My connection to the view; THIS TIES IN ANDROID CLASSES (!) -->
<!-- "id" value will show up when using auto-complete -->

<mine.counter.CounterView
android:id="@+id/cview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<ImageView android:id="@+id/firstdigit" android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>

</FrameLayout>


In MAIN.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


In COLORS.XML:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="blueback">#0071BC</color>
</resources>


In STRINGS.XML:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Counter</string>
<string name="app_name">Counter</string>
</resources>


In COUNTER.JAVA:

package mine.counter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class Counter extends Activity {


// MY CLASSES
private CounterView myCounterView;

// MY ELEMENTS


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.bkcounter); // this should always come first
myCounterView = (CounterView) findViewById(R.id.cview);

}


}


In COUNTERVIEW.JAVA:

package mine.counter;

import android.content.Context;
import android.view.View;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;


public class CounterView extends View {

private Drawable imgDigit0;


// CONSTRUCTOR FOR THIS CLASS - "NEW" INSTANCE

public CounterView(Context ctx) {
super(ctx);

// Set up all images
imgDigit0 = ctx.getResources().getDrawable(R.drawable.n0);

// Turn on focus
setFocusable(true);

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

imgDigit0.draw(canvas);

}

}
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Top

Postby MrSnowflake » Mon Nov 03, 2008 11:47 am

Look at your Log(Cat) it'll give you a better error report.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby illiniwatcher » Mon Nov 03, 2008 6:58 pm

MrSnowflake wrote:Look at your Log(Cat) it'll give you a better error report.


Sorry, I don't know where that is. Where is it?

I tried running ADB.exe and couldn't figure it out. Does that have anything to do with Log(Cat)?
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Postby Quetzalcoatl » Mon Nov 03, 2008 8:52 pm

Have you tried putting some breakpoints in your code? the most logical places to start with are when something is being de-referenced (sorry, don't know Java terminology I've come from a C++ background). Such as in your

Code: Select all
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

imgDigit0.draw(canvas);

}


stick a breakpoint at imgDigit0.draw(canvas); and see if it gets that far, if it doesn't then start to work backwards putting in breakpoints at sensible places. Will help us if we know what exactly is causing the problem (the closer to a single line of code the better :D)
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby Quetzalcoatl » Mon Nov 03, 2008 9:36 pm

In CounterView.Java replace this
Code: Select all
public CounterView(Context ctx) {
super(ctx);

// Set up all images
imgDigit0 = ctx.getResources().getDrawable(R.drawable.n0);

// Turn on focus
setFocusable(true);

}


with this

Code: Select all
public CounterView(Context context, AttributeSet attrs) {
      super(context, attrs);
      
      // Set up all images
      imgDigit0 = context.getResources().getDrawable(R.drawable.n0);

      // Turn on focus
      setFocusable(true);
   }
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby MrSnowflake » Mon Nov 03, 2008 11:27 pm

illiniwatcher wrote:
MrSnowflake wrote:Look at your Log(Cat) it'll give you a better error report.


Sorry, I don't know where that is. Where is it?

I tried running ADB.exe and couldn't figure it out. Does that have anything to do with Log(Cat)?

Code: Select all
adb logcat
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby illiniwatcher » Tue Nov 04, 2008 12:09 am

Quetzalcoatl wrote:In CounterView.Java replace this
Code: Select all
public CounterView(Context ctx) {
super(ctx);

// Set up all images
imgDigit0 = ctx.getResources().getDrawable(R.drawable.n0);

// Turn on focus
setFocusable(true);

}


with this

Code: Select all
public CounterView(Context context, AttributeSet attrs) {
      super(context, attrs);
      
      // Set up all images
      imgDigit0 = context.getResources().getDrawable(R.drawable.n0);

      // Turn on focus
      setFocusable(true);
   }


Quetzalcoatl:

Thanks for the help. Your suggestion to add "attrs" helped unstick the program. Out of curiosity, though, would you happen to know why "attrs" made the difference?

Also, now that the program runs, the screen only shows a blue background (the blue I defined in colors.xml) but not the drawable n0.png image. I call the onDraw method:

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

imgDigit0.draw(canvas);

}

Your astute fix of this instruction:

imgDigit0 = context.getResources().getDrawable(R.drawable.n0);

seems like it should have resolved it. Any ideas?

Thanks,
Charles
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Postby Quetzalcoatl » Tue Nov 04, 2008 12:27 am

To be honest I can't give you an quick answer on either the 'attr' addition or the onDraw method, if I have chance I'll give it a try and see. I have not had a look into drawing images to the screen yet so someone else might be better placed to answer this. Possibly make a new thread as people will be thinking this one is still about the older error possibly.
I can only assume that the Android framework is looking for the newer constructor (with attr) for some reason and the constructor you had wasn't suitable. Maybe there's some documentation somewhere about which type of constructor is expected when creating a view :?: :?:
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby Quetzalcoatl » Tue Nov 04, 2008 3:26 am

Code: Select all
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.n0);

//bitmap, X-coord, Y-coord, Paint = null
canvas.drawBitmap(image, 0, 50, null);

}


Definately works, also I think you can take the

Code: Select all
<ImageView android:id="@+id/firstdigit" android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView>


out of bkcounter.xml as it just sits in the same are as CounterView (unless you're planning to use it for something?)
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Perfect! Thank you!

Postby illiniwatcher » Tue Nov 04, 2008 3:44 am

Perfect, Quezalcoatl! Thanks very much! That did the trick!

Cheers,
Charles
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests