Null Pointer at Textview - android

The answer may be staring right at me but I just can't see it!
Am getting a null pointer exception when setting the text on a Textview with a string from a ArrayList of Strings. Have initialised everything and checked the ArrayLiist has data, everything seems to check out. But when the textview is been declared in onCreate() I can see in the debugger the value remains null.
OnCreate():
private TextView title;
//onCreate()
setContentView(R.layout.gallery);
imageNames = new ArrayList<String>();
title = (TextView) this.findViewById(R.id.tv_imageDescriptionGallery);
setImageAndTitleArrays();
//check size of arrays and build correctly
Log.d("GALLERY VIEW:" , "IMaegNaem array Size: "+imageNames.size() + " Image Array Size: "+ bitmapArray.size());
Log.d("GALLERYVIEW: ", "First Image Name of Array: "+ imageNames.get(0));
//set default image title and description, we will update every tome a image clicked
//exception throne here as title=null on debugger
title.setText(imageNames.get(0));
The textview :
<TextView
android:id="#+id/tv_imageDescriptionGallery"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Image Name"
android:textColor="#55bbfa"
/>....
Gallery XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Astro Gallery"
android:textColor="#55bbfa"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Gallery
android:id="#+id/gallery1"
android:layout_marginTop="100dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/splitter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sample_note"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/splitter" />
<ImageView
android:id="#+id/imageView1"
android:layout_marginTop="100dp"
android:layout_width="420dp"
android:layout_gravity="center_horizontal"
android:layout_height="350dp"
android:src="#drawable/camera1" />
<TextView
android:id="#+id/tv_imageDescriptionGallery"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Image Name"
android:textColor="#55bbfa"
/>
<Button
android:id="#+id/send"
android:layout_marginTop="10dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Set Wallpaper"
android:background="#drawable/button_bg"
android:textColor="#fff" />
<ImageView
android:id="#+id/shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#drawable/shadow" />
</LinearLayout>
</ScrollView>

Only 2 hypothesis :
Maybe you tried to import R.java and you imported the wrong one ?
Or try to remove "this" in front of findViewById?
The 2 only ideas that come to mind, hope it can help.

Although I checked all the imports, removed all the ref to 'this, rename the class and all refs to it, cleaned and build, could not resolve the NPE.
I deleted the class and xml from the project, then made a new class and xml file with same code and worked fine.
I had a class with exact same name in a separate project, not sure if this has anything to do with it.

Related

why error is not going from R.java in android

public static final class id {
public static int Phone Number=0x7f050043;
error shows in a above link Phone Number=0x7f050043.
What could be the problem? I have already changed the XML id from #+id/Phone Number to #+id/phoneNumber.
I've done many things related with this like clean the project, delete gen folder, but the error persists. My project submission is tomorrow. Please help me find a Solution.
my XML file is
<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"
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="com.example.pgfinder.Add_PG" >
<RelativeLayout
android:layout_width="400dp"
android:layout_height="470dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<EditText
android:id="#+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/house"
android:layout_below="#+id/house"
android:layout_marginTop="20dp"
android:ems="10"
android:gravity="center"
android:hint="Pg For Male or Female" />
<EditText
android:id="#+id/landmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/city"
android:layout_below="#+id/city"
android:layout_marginTop="17dp"
android:ems="10"
android:gravity="center"
android:hint="Landmark" />
<EditText
android:id="#+id/phone_number"
android:gravity="center"
android:hint="Phone Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/landmark"
android:layout_below="#+id/landmark"
android:layout_marginTop="23dp"
android:ems="10" />
<EditText
android:id="#+id/house"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:gravity="center"
android:hint="House Address" />
<EditText
android:id="#+id/meal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gender"
android:layout_below="#+id/gender"
android:layout_marginTop="25dp"
android:ems="10"
android:gravity="center"
android:hint="Pg With Meal or Not" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/meal"
android:layout_below="#+id/meal"
android:layout_marginTop="14dp"
android:ems="10"
android:gravity="center"
android:hint="City" />
<Button
android:id="#+id/add_pg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Phone Number"
android:layout_below="#+id/Phone Number"
android:layout_marginTop="24dp"
android:text="Add New PG" />
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/add_pg"
android:layout_alignBottom="#+id/add_pg"
android:layout_alignRight="#+id/Phone Number"
android:text="Cancel" />
</RelativeLayout>
</RelativeLayout>
rename into
#+id/phoneNumber
save Ctrl s
try manual allocating by yourself in R.java
public static int phoneNumber=0x7f050050;
save ctrl s
Same thing happen to me , and i passed a day to fix this. really this is a stupid error. i am just shearing my experience, may be it would helpful to you . First look carefully when you type this setContentView(R.layout.activity_to_do_list); hear your starting point R.l then eclipse should open a suggestion for you look like this
you import the second one not layout-android.R . the second one is created in your project hear com.example.gridtest is the project package name . then focus your imported section in your code
ok look this import com.example.gridtest.R; its important . If you already import this android.R then remove it. Thank you hope it works . ( you don't need to do this always but if you face this kinda problem then do this )
There are more option you can try this click
Step 1: Delete build folder from project, and recompile your code. Recompilation will recreate build folder (and R file). It will solve the issue.
Step 2: If above step doesn't work rename id i.e. #+id/phoneNumber to #+id/phoneNumber1 and perform step 1 again.
Update
Replace each instance of Phone Number with phone_number like in
android:layout_below="#+id/Phone Number" or android:layout_alignLeft="#+id/Phone Number". Do it in both buttons i.e. add_pg and back.
See:
<Button
android:id="#+id/add_pg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Phone Number"
android:layout_below="#+id/Phone Number"
android:layout_marginTop="24dp"
android:text="Add New PG" />
Replace it with :
<Button
android:id="#+id/add_pg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/phone_number"
android:layout_below="#+id/phone_number"
android:layout_marginTop="24dp"
android:text="Add New PG" />

View over ListView leads to ClassCast Exception

I am using a Relative Layout(RL) to Hold a List View(lv) and a few bunch of other widgets like Text View(tv), Edit Text(et), then a few Buttons(btn) and finally one Progress Bar(pb).
Scenarios
1. When i add child Views to the Parent Container View viz. Relative Layout(rl), in the order: RL(Btn1->Btn2->tv1->tv2->LV->pb). Then we have no problem, i mean No Exception, No Problem.
2. But when i do the ordering change like RL(tv1->tv2->LV->Btn1->Btn2->pb). then i am getting a classcast Exception.
Note: the two buttons should be over the listview, as both of them are social website buttons, i mean Facebook and Twitter.
XML Code:
<RelativeLayout
android:id="#+id/rlfornews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:background="#drawable/newsbg"
android:orientation="vertical"
android:paddingBottom="5dip" >
<Button
android:id="#+id/btnbasetw"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="10dip"
android:background="#drawable/twitter_icon"
android:visibility="gone" />
<Button
android:id="#+id/btnbasefb"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerVertical="true"
android:layout_marginBottom="10dip"
android:layout_toLeftOf="#id/btnbasetw"
android:background="#drawable/facebook_icon"
android:visibility="gone" />
<TextView
android:id="#+id/tvnewscon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:background="#drawable/newstitle"
android:gravity="center_vertical"
android:paddingLeft="8dip"
android:shadowColor="#color/black"
android:shadowDy="1"
android:shadowRadius="1"
android:text="#string/news"
android:textColor="#color/white" />
<TextView
android:id="#+id/tvfornews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvnewscon"
android:layout_marginTop="10dip"
android:gravity="center"
android:text="#string/no_data_found"
android:visibility="gone" />
<ListView
android:id="#+id/lvdashboardnews"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tvnewscon"
android:padding="4dip"
android:visibility="visible" >
</ListView>
<ProgressBar
android:id="#+id/pbfornews"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Note: The Progress Bar might be below, but it is just to user information, that some data is loading. and when i get that data, i am making its Visibility to VIEW.GONE state, and the List View being already there !
My question is when i follow the First Scenario, even though i have a Progress Bar from the entire time, i am not getting a Class Cast Exception. But i am getting the exception, when i change to Scenario 2. So why i am i getting it?
I am not sure about these but may be the reason is you have used the android:layout_below = "#id/textview" instead of use these android:layout_below = "#+id/textview" all UI.
If you can do these UI using the LinearLayout.

View disappearing when inflated

I have a custom ListActivity in which i have a custom ListAdapter that inflates a layout for each item in the list. In eclipse, in the visual editor the layout looks like this
This is made by the following code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/largeFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_marginRight="3dp"
android:layout_marginTop="5dp"
android:background="#color/mainBack"
android:orientation="vertical" >
<View
android:id="#+id/imageView1"
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_marginLeft="2dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/newtimeslot_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imageView1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/black" />
<View
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignRight="#+id/newtimeslot_class"
android:layout_below="#+id/newtimeslot_class"
android:layout_toRightOf="#+id/imageView1"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/newtimeslot_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/newtimeslot_class"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#646464" />
<TextView
android:id="#+id/newtimeslot_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView2"
android:layout_marginRight="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#646464" />
<TextView
android:id="#+id/newtimeslot_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/newtimeslot_location"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#646464" />
</RelativeLayout>
</FrameLayout>
Unfortunately though, when i run this on an android device, the line to the left completely disappears. It instead looks like
I have just done the usual in terms on inflating it, and i only ever access the TextView objects. So i'm just wondering what could possibly be going on and if someone could help me out of this mess?
Cheers
Edit: The inflation code
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.timeslotlayout_new, parent, false);
return view;
}
I suspect the reference #android:color/darker_gray to be the problem.
It might be that on some devices, this value does not exist ?!? (it may be under another name for instance)
Try setting the background attribute to a COLOR value (instead of an android color reference) like this #00000 which represents black (or choose another custom color like this #A9A9A9 for DarkGray ) :
android:background="#A9A9A9"
/OR/
Create an XML file for most basic colors like done HERE, in this manner you are sure that the colors will not change among devices, and most of all, that they will exist on all devices for your application.
So if you do so (see the link), you can create a color like this (the name of the XML file does not matter at all) :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black">#000000</color>
<color name="DarkGray">#A9A9A9</color>
</resources>
And reference it in the following way :
android:background="#color/DarkGray"
Note :
No matter if this solved your issue or not, I strongly advise you to follow this manner to be on the safe side.
Maybe you're missing to set this:
android:layout_alignParentLeft="false"
to this:
android:layout_alignParentLeft="true"
And, by the way, android:orientation="vertical" doesn't apply to a relative layout. It applies only to LinearLayout.
And to the newtimeslot_time TextView change the
android:layout_toRightOf="#+id/imageView1"
to
android:layout_toRightOf="#id/imageView1"
Hope it helps!
Cheers!

How to increase size of posted pictures from facebook api?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip"
android:background="#drawable/greybox"
>
<ImageView
android:id="#+id/profile_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/profile_pic">
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Kumar"
android:textSize="18dp"
android:textColor="#4c3f38"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_below="#+id/name"
android:textColor="#7b7674"
/>
<LinearLayout
android:id="#+id/picpostlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="5dp"
android:layout_below="#+id/msg"
android:orientation="vertical" >
<ImageView
android:id="#+id/picpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#null" />
</LinearLayout>
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/picpostlayout"
android:layout_alignParentRight="true"
android:textColor="#000000"
/>
</RelativeLayout>
</RelativeLayout>
Here is my complete xml.I am using the linearlayout to show the posted picture,but it is still showing very small size.I used two relative layouts, one for complete xml and another for name,message,postpicture.
Here is also my screenshot to the above given xml file.I am getting the large image but I cant able to show the same size,it is showing very small size.
I got an post picture from facebook api,I am getting very large image as I used _n.jpg,but when I tried to display using imageview it is showing very small size.
][1]
[1]: http://i.stack.imgur.com/63nTO.png
Here is the code of getting posted picture from facebook api.If the post object has picture field then I get that picture and changed that into large image and I added that to my ArrayList pic.
if(jsonObject.has("picture"))
{
String fetchedURL = jsonObject.getString("picture");
String getFeedPicture = fetchedURL.replaceAll("_s.jpg", "_n.jpg");
pic.add(getFeedPicture);
}
Then I send that ArrayList to my Adapter where I set that image to imageview by sending to imageloader class.
Here is my loader class reference imageloader and I am setting image to imageview.I am setting this in my adapter class.
imageLoader.DisplayImage(postimage.get(position).replace(" ", "%20"), postimg) ;
Make this modification to your current code:
String fetchedURL = JOFeeds.getString("picture");
String getFeedPicture = fetchedURL.replaceAll("_s.jpg", "_n.jpg");
pic.add(getFeedPicture);
This is the simplest way to get a better quality picture from your Feeds. The whole point of this piece of code is that by default, the Facebook API returns a small version of the image. By replacing the _s.jpg with _n.jpg gives you a bigger image.
I use this code throughout my application and it works like a charm every single time.
And the corresponding code for this is:
<LinearLayout
android:id="#+id/linlaFeedPicture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_margin="1dp"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgvwFeedPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#null" />
</LinearLayout>

String types not allowed (at 'id' with value ' #id/bAdd')

I am getting an error saying
String types not allowed (at 'id' with value ' #id/bAdd')
I've cleaned the project.
Everything was working earlier, I don't what happened, causing this error.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Your total is 0"
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"
tools:context=".StartingPoint" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="20dp"
android:id="+#id/bSub"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="20dp"
android:id="+#id/bAdd"
/>
You can give your id as #+id/bAdd
instead of +#id/bAdd
Also edit in
android:id="#+id/bSub"
^^
instead of
android:id="+#id/bSub"
^^
+ Symbol must be after # Symbol.
android:id="+#id/bAdd" to android:id="#+id/bAdd"
and also change
android:id="+#id/bSub" to android:id="#+id/bSub"
OR
you can declare is it
android:id="#id/bSub"
but you have to declare bSub value in ids.xml file located in values folder.
Example
<resources>
<item type="id" name="bSub" />
please use #+id/yourId instead of #id/yourId

Categories

Resources