Android setImageResource NullPointerException - android

I have the following code:
public class PainLogger extends Activity implements OnClickListener, OnItemClickListener{
ImageView image;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.imageView1);
image.setImageResource(R.drawable.acetaminophen); //**Null Pointer Here**
....
}
}
Here is the image in the items.xml file. (I think this is why it is null-- instead of being in the main.xml file, the imageView is in my items.xml file, although it does show up in R.id, and R.drawable)
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:src="#drawable/icon" android:id="#+id/imageView1"></ImageView>

You do indeed get a NullPointerException because that widget does not exist in your main.xml file. R.* lists every single ressource in your application, so that's why you see R.id.imageView1 in it.
You should either copy the contents of your items.xml file in your main.xml, or use < include /> to include items.xml or main.xml, or just change your code logic.
If you are trying to access items in a list, you should be using a ListView in main.xml, and use an adapter. You'll find a nice tutorial about this here : http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

Related

findViewById not finding my button

I'm new to Android programming, and I'm trying to make a simple button which displays a toast notification when clicked.
I tried to initialize a button called "button" in my MainActivity like so:
public class MainActivity extends Activity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button =(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v){
Toast toast = Toast.makeText(this, "Click Me", Toast.LENGTH_LONG);
toast.show();
}
}
Also, here is activity_main.xml:
<RelativeLayout 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">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button1">
</Button>
</RelativeLayout>
UPDATE: findViewById now works fine after importing android.R. However, this created a new error when I call setContentView(R.layout.activity_main);
Okay a bit of background here. strings.xml is for defining pieces of text. For instance the text that might go on a button (but this does not define the button itself).
The button is going to have to be defined in a layout file in the res/layout/ directory. Yours is likely called activity_main.xml.
You'll need to create a button element in this file and assign the id to something descriptive. This kind of id is what findViewById is going to search on.
An example:
<Button
android:id="#+id/thebuttonsid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="#string/button1" />
You can get a handle to this button like so:
Button button = (Button)findViewById(R.id.thebuttonsid);
Notice how I set the text to #string/button1? This is what the strings.xml file is for. However the layout files in the layout directory are where you define controls like buttons.
The button's ID name is same to the button's text name. You'd better change any one to other name.It must be work.
I would clean the project and build again. Or reimport the project. Eclipse is ... just hard to use. Try to use Android Studio.
When there is some "mismatch" with resources cleaning a project is a good advice. IN the past it was even more so.
Next what I would check if your resources compile as such. You might have an error somewhere in your resources, therefore the R does net get compiled and any reference to R.id.* is not valid.

about a program "HelloWorld" of android application

Why can't I use setContentView(R.layout.main) in last line without setContentView(tv)? Please explain this to me.
package com.mue.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorldActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android i am suvankar");
setContentView(tv);
}
}
You have to define the textview in the R.layout.main ( a xml file) this file contains the information of the objects in the activity.
if you are using eclipse you can simple drag and drop a textview, just open the main file.
(folder res -> layout -> main.xml)
then yo have to call it in your program:
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.tv); //<-- yo have to use the same ID that is in the main.xml file
and then you can set the text. all of this in the oncreate function.
tv.setText("Hello, Android i am suvankar");
Well, I hope I've helped. is my first response here
You shouldn't call setContentView() twice in your onCreate. Either call setContentView(R.layout.main) or call setContentView(tv) but not both. I would prefer the first of the two... but you need to make sure the TextView is declared in your layouts XML.
Lets first try to understand what setContentView() method does. Basically setContentView() places your UI on your Activity. Now to create a UI component for your Activity, you can either use xml resource (e.g. R.layout.main) or you can obtain an instance of the UI component in your code and dynamically add it to your Activity. For example
TextView tv = new TextView(this);
tv.setText("Hello, Android i am suvankar");
setContentView(tv);
In your case you created an instance of the TextView tv, set some text to it and added that to your Activity. Here you dont need to use setContentView(R.layout.main). However if you have a xml layout (main.xml) in your layouts folder like the following:
<?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="Hello World"
/>
</LinearLayout>
and at the bottom of your application you write setContentView(R.layout.main), your should see a black screen with Hello World written on it. This is because here you created a TextView instance, set some text to it, but did not place it to your activity by calling
setContentView(tv), rather you added a completely different layout resource. If you use setContentView(tv) and at the end of your onCreate() you add setContentView(R.layout.main) then again you will see Hello World instead of "Hello, Android i am suvankar" because at the end you replaced your UI resource. However, if you forget to add your xml resource and call 'setContentView(R.layout.main)' compiler will issue an error for not finding the xml resource specified.

Why is my variable null?

I have the following code:
public class Level extends Activity{
public static String TAG="Level";
Level_Score_bar score_bar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level_layout);
score_bar=(Level_Score_bar) findViewById(R.id.Score_Bar);
Log.d(TAG,"score_bar="+score_bar);
}
}
The XML code looks like this (R.layout.level_layout) (Non-important stuff removed)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<pearsonartphoto.AJEG.Level_Score_bar android:id="#+id/Score_Bar" android:layout_height="wrap_content" android:layout_width="fill_parent">
</pearsonartphoto.AJEG.Level_Score_bar>
</LinearLayout>
The log statement reads "score_bar=null".As you can see, there is a resource named R.id.Score_Bar. Am I missing something? I need to use the function for some other stuff. Thanks!
There are a number of things that can cause this. The most common being that for one reason or another R.id was not generated correctly. Usually doing a simple 'clean' will fix this. If that doesn't work another thing to check would be the constructor for the custom view. You can find this and other suggestions from this question.

Custom ListView layout crashes my activity

I am starting out on Android and trying to make a custom ListView layout. I've followed some guides and have made the following code:
public class CheckInList extends ListActivity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
...
mAdapter = new ArrayAdapter<String>(this, R.layout.checkinlist_item, R.id.checkinlist_item_text, mNames);
setListAdapter(mAdapter);
...
}
}
This is the code for checkinlist_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/checkinlist_item_bg">
<TextView android:id="#+id/checkinlist_item_text"
style="#style/RegisterText" />
</RelativeView>
If I use android.R.layout.simple_list_item_1 instead of my above template then everything is fine and my ListView works, however whenever I use the above code my activity crashes. I am running things on Android 1.5.
Any ideas why things are crashing?
Could it be because RelativeView does not exist?
I might suggest adding the attributes android:layout_width="fill_parent" and android:layout_height="fill_parent" or set them to whatever you want, but those are required. And yes, use RelativeLayout as Matthew said, since RelativeView doesn't exist.
at first sight there is no width/height values for your textView ?

textView.setText(); crashes

The setText() method returns null in my application why?
public class GetValue extends Activity {
char letter = 'g';
int ascii = letter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView)findViewById(R.id.txt_1);
textView.setText(ascii);
}
}
It doesn't matter what text i put in, it crashes anyway. Why does setText() keep returning null?
Thank you, in advance
Solution: My error was in the xml file. I wrote:
android:text="#+id/txt_1"
When it should say:
android:id="#+id/txt_1"
Thanks a lot for all the answers and comments!
You tried to pass an integer as parameter to setText, which assumes it is a resource ID. To display computed text, pass it as a string: textView.setText("g");
Edited: Check your XML file, I have test with something very basic and it works
<?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:id="#+id/txt_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="myTextView"/>
</LinearLayout>
Maybe try to clean your project (Project->Clean in Eclipse), I recently have some trouble with R generation on the last ADT version.
Try this:
textView.setText(Integer.toString(ascii));
Also make sure that your layout xml has TextView txt_1
I've tried :
Integer.toString(char) and String.valueOf(char)
both didnt work.
The only solution is :
txt.setText(""+char);
This is not very efficient from optimization point of view but it does the trick :)

Categories

Resources