• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Beginner issue

lazure

Lurker
Apr 5, 2011
4
0
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:
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!
 
Well... First of all it doesn't make much sense to call setContentView three times within the onCreate. setContentView sets the view of the entire screen, every time, so, even if your app didn't crash at the image part, the resulting screen would only be the textview...

And therefore I don't see why you would do what you are doing?

Why it is crashing I am not 100% sure, but I've got a hunch. I think it has to do with the same mechanism, setContentView. Which is why it would make more sense that it is in the line that says setContentView(image) the nullPointer is, image being the null reference. Because image is, set by the findViewById, a reference to a View object only available as long as that particular layout-file is loaded as content. And as you go on to use setContentView again, that layout file, and it's references is trashed.

So to have all those things shown, you should simple put only the setContentView(main) in your code, as the layout and background mechanisms then takes care of adding all the elements to the screen.
 
Upvote 0
Ahh I see. I thought the setContentView only refreshed a single element. I removed all the setcontentviews except the R.layout.main and the image is displayed!

However, the tv.setText("...") does not work anymore, the text is now set only from xml. Is it not possible to combine programmatical elements with elements set from xml?

Thanks alot!
 
Upvote 0
To answer you question, the idea is to have the xml build the interface and to have your code manipulate the elements of the main view during runtime.

So you only do setContentView once in you onCreate method (which you eventually did). Now you need to create a link to your textView like so:

TextView tv = (TextView)findViewById(R.id.TextViewId01);
tv.setText("I am a genius");


NB: Remember to assign a id to your textview that you want to reference in your xml

<TextView android:id="@id/TextViewId01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
 
Upvote 0
Hi all, I am new to android I have also same problem. I tried in lot of way. and as suggested above also. but o use. Here is my code.

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:eek:rientation="vertical" >

<TextView android:id="@+id/TextViewId01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

</LinearLayout>
_____________________________________________________________________

java file:



package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.helloandroid.R;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
super.onCreate(savedInstanceState);
// TextView tv = new TextView(this);
TextView tv = (TextView)findViewById(R.id.TextViewId01);
// TextView tv = new TextView(this);
tv.setText("Hello, Android");
// setContentView(tv);
setContentView(R.layout.main);


}
}

pls help me on tis. any suggestion or comment. would be appreciated.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones