Hi guys,
I'm new to android and java coding, but trying to learn. I have several years of experience in other languages though.
Anyway, I was just trying out a hello world app, and have run into problems.
I'm simply trying to display an image by using an ImageView defined in xml. This is my code:
main.java:
main.xml:
The code compiles fine but I'm getting a force close when running, I think the reason is a nullpointerexception at this line:
final ImageView image = (ImageView) findViewById(R.id.img1);
If I remove it, the app runs fine. img1 has the src of an existing file, icon.png which is located in res/drawable. Not really sure how that part works, though since the extension is not included in the code (it's set automatically by eclipse).
Help is really appreciated!
I'm new to android and java coding, but trying to learn. I have several years of experience in other languages though.
Anyway, I was just trying out a hello world app, and have run into problems.
I'm simply trying to display an image by using an ImageView defined in xml. This is my code:
main.java:
Code:
package com.hw;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image = (ImageView) findViewById(R.id.img1);
setContentView(image);
TextView tv = new TextView(this);
tv.setText("Hello, ffs Android");
setContentView(tv);
}
}
main.xml:
Code:
<?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"
/>
<ImageView
android:layout_height="wrap_content"
android:id="@+id/img1"
android:layout_width="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>
The code compiles fine but I'm getting a force close when running, I think the reason is a nullpointerexception at this line:
final ImageView image = (ImageView) findViewById(R.id.img1);
If I remove it, the app runs fine. img1 has the src of an existing file, icon.png which is located in res/drawable. Not really sure how that part works, though since the extension is not included in the code (it's set automatically by eclipse).
Help is really appreciated!