Adding Things within a Layout causes a Crash - android

So There was a layout I wanted changed. So I added a linear layout to it and somehow it crashed code that wasn't even slightly related to it. So here Ill post some code.
Here is my main file and Ill point where the crash happens.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.layoutLogin);
gbtnSignUp = FindViewById<Button>(Resource.Id.btnSignUp);
gbtnSignIn = FindViewById<Button>(Resource.Id.btnSignIn);
gbtnSignUp.Click += gbtnSignUp_Click;
gbtnSignIn.Click += gbtnSignIn_Click; // here is where it crashes
}
Now my layout. Ill pinpoint which part I add adds the crash.
<?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:weightSum="100"
android:layout_height="fill_parent">
<ListView
android:layout_weight="80"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/ListViewHighlight"
android:id="#+id/companyListView" />
<ProgressBar
android:layout_weight="10"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/ListViewHighlight"
android:id="#+id/progressBar1" />
<LinearLayout// just from this linear layout existing it causes a crash
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:weightSum="100"
android:id="#+id/linearLayout1">
<TextView
android:text="Text"
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="match_parent"
android:id="#+id/textView1" />
<ImageButton
android:src="#drawable/defaultAdd"
android:layout_width="0dp"
android:layout_weight="90"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:id="#+id/imageButton1" />
</LinearLayout>
</LinearLayout>
This layout isnt even linked to the view that is causing the crash. Heck I've even removed all references to this view from within the project and it still crashed.
here is the error message forcing me to break at the line I specified.
System.NullReferenceException: Object reference not set to an instance of an object
Also I can comment this line out, yet itll make it able to run but ill still be able to press the button which then does some very bizzare behaviour, which I shouldn't be able to press it to begin with.
xml of login button
<Button
android:text="Sign In"
android:layout_width="match_parent"
android:layout_weight="15"
android:layout_height="0dp"
android:id="#+id/btnSignIn"
android:textSize="25sp"
android:background="#drawable/ButtonSignInStyle"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />

Your layout does not contain buttons with the ids btnSignUp and btnSignIn. You need to add them or remove the 4 lines in your activity code.
Missing in your layout
<Button
android:id="#+id/btnSignUp"
.../>
<Button
android:id="#+id/btnSignIn"
.../>

Actually this problem was some messed up situation that happened when I exported my program in order to move it to another folder. I copied and pasted my files to my old version in its original folder and it worked perfectly. So if anyone has moved files in a not so great way it may cause unforeseen problems that don't make sense like this one.

Related

#id is not working inside Relative Layout

As far as I know, the difference between #+id and #id is to create a resource id first time and reuse that already existed resource id in different places. For instance, If we have a Relative layout having two textViews one below another, we shall use #resourceId for the second textView which refers to the first TextView.
The problem is, after updating the android studio to 3.0, #resourceId is not working anymore.To place second textView below the first one, I need to use #+firstTextViewId instead of #firstTextViewId. More specifically I need to use,
android:layout_below="#+id/totalQty"
instead of
android:layout_below="#id/totalQty"
Here is the code
<RelativeLayout
android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>
Is it an understanding issue? or a problem from any end? Can anyone please explain?
I just remove + sign at #+id from your code. Here's the updated code
<RelativeLayout android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>

imageview in xml causing app to crash on launch

First off let me apologize for any layout/formatting errors, I'm doing this on my cell.
That being said, I cannot for the life of me figure out what is wrong with the below xml. With it in my layout file, the app crashes on load, but if I cut it out, the app loads and runs fine. Any help in figuring out the problem would be greatly appreciated.
Note: The drawables are all .jpg files in the drawable folder.
Note: This section is cut from within another vertical linearlayout
Not sure if can/how to copy logcat from Aide. However, the logcat finally popped and it is an out of memory exception. I will try reducing the image size and come back.
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="Current Favorites"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_height="100dp"
android:layout_width="wrap_content"
android:src="#drawable/mia_sollis"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Mia Sollis"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_height="100dp"
android:layout_width="wrap_content"
android:src="#drawable/pepper_kester"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Pepper Kester"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_height="100dp"
android:layout_width="wrap_content"
android:src="#drawable/jayme"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Jayme Langford"/>
</LinearLayout>
</LinearLayout>
The possible answer is,
<ImageView
android:layout_height="100dp"
android:layout_width="wrap_content"
android:src="#drawable/mia_sollis"/>
image "mia_sollis" is too big and hence is giving out of memory error.
Try out with smaller size image, it will solve your problem.
Possible reasons based on your XML layout:
1) You forgot to add this on top of your layout file
xmlns:android="http://schemas.android.com/apk/res/android"
2) Second and the most important
You have multiple root tag, TextView and LinearLayout both of them is your root. However, you MUST have one root layout and inside it, do what ever you want.
Cheers!
You have to post your Log
I guess
maybe the size of images is too big for your app memory.
add this to your Application tag in Manifest
<application
.......
android:largeHeap="true"
........ >

ListView in RelativeLayout not displaying

I'm writing an application and have come across a bit of an issue.
When working with a layout file, I have a 2 RelativeLayouts inside of a ViewFlipper.
The idea is that this page in particular is a welcome screen. The first RelativeLayout "welcomes" you to the app, and upon pushing a button, the user is led to the second RelativeLayout. In this layout there is a search bar that will allow the user to search for a certain criteria (specific to the app, not important) then display the results in a ListView.
Everything works correctly, but displaying the ListView seems to have some problems. The adapter is set properly, and I tested the method on another ListView in a test layout, and it worked fine. However something seems wrong with the ListView in the RelativeLayout. Here is the layout code
activity_welcome.xml
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/welcomeFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:background="#color/white"
tools:context="com.jacemcpherson.announcer.WelcomeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:padding="40dp">
...
<!-- This code irrelevant, all's well :) -->
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#color/white"
android:padding="40dp">
<TextView
android:id="#+id/textFirstThingsFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textYouNeedToSearch"
android:layout_centerHorizontal="true"
android:text="#string/first_things_first"
android:textColor="#color/app_color"
android:textSize="32sp" />
<TextView
android:id="#+id/textYouNeedToSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:gravity="center"
android:text="#string/you_need_to_search_for_your_school"
android:textColor="#color/black"
android:textSize="18sp" />
<EditText
android:id="#+id/schoolSearchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textYouNeedToSearch"
android:hint="#string/search_hint" />
<ProgressBar
android:id="#+id/searchListProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/schoolSearchEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:visibility="gone" />
<!-- This is the problem area -->
<ListView
android:id="#+id/searchResultsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/schoolSearchEditText" />
</RelativeLayout>
</ViewFlipper>
I'm setting the adapter works like normal, but just in case I missed something, here's the line of code for that...
mListView.setAdapter(new ArrayAdapter<String>(
mContext,
android.R.layout.simple_list_item_1,
result // this is the array of results to be displayed in the list.
));
Thank you for your help, if I've missed something that in some way makes this unanswerable without further information, please let me know.
Change the layout_height from wrap_content to either match_parent or fill_parent.
This is explained very well by this answer.

Layout XML View IDs and R problems

I am creating a layout in XML, and I have noticed this very strange occurrence. It has never happened before, but lately it's been getting on my nerves. I kind of want to abandon Eclipse and find a better IDE because of it.
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/action_search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="kkm" >
</EditText>
<ListView
android:id="#+id/actions_list"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_above="#id/cancel_action_selection"
android:layout_below="#id/action_search_bar" >
</ListView>
<TextView
android:id="#+id/action_preview_pane"
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_alignBottom="#id/actions_list"
android:layout_alignTop="#id/actions_list"
android:layout_toRightOf="#id/actions_list"
android:gravity="center_vertical|center_horizontal"
android:text="kk" />
<Button
android:id="#+id/cancel_action_selection"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:text="Cancel" />
<Button
android:id="#+id/define_new_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/cancel_action_selection"
android:text="NEW" />
<Button
android:id="#+id/select_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/define_new_action"
android:text="OK" />
</RelativeLayout>
Here is what I'm trying to achieve:
The problem is, if I try to set the ListView to be above the cancel button (via android:layout_above), it will say that it can't find the id for the cancel button.
Now, what I have noticed is that if I try and specify any ids for layouts BEFORE they are created with #+id, then I will get that strange error. What I mean by this is the following:
In the XML, apparently IDs have to be declared in order. Strange, no?
I would have to declare the cancel button's id first with #+id way before I define the ListView and set its layout_above attribute.
If I try to do what I have done here, it will say that it cannot find the id for the cancel button.
I have tried cleaning the project, ending adb.exe, refreshing, etc. and all it does is corrupt my project even more. R is not even generated afterwards. I don't want to have to go in a loop problem after problem because Eclipse can't handle things like this properly.
Does anyone have a solution for this? Or a better IDE?
http://developer.android.com/guide/topics/ui/layout/relative.html
RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.
So you are placing listview relative to the button cancel. So you need to define button with a id. Position the listview relative to the button.
http://developer.android.com/guide/topics/ui/layout/linear.html
There is an example in the links posted both for linearlayout and relative layout have a look at it.
Edit:
If you have errors in your resource files R.java will not be generated. So make sure you do not have errors in your resource files and then clean and build the project.
To clarify the confusion. Note android:layout_above="#+id/button1"
<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"
tools:context=".OtherScreenActivity1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Button" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
>
</ListView>
</RelativeLayout>
Same as above
<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"
tools:context=".OtherScreenActivity1" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
android:layout_alignParentTop="true" >
</ListView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Button" />
</RelativeLayout>
You have this
android:layout_above="#id/cancel_action_selection"
Use
android:layout_above="#+id/cancel_action_selection"
http://developer.android.com/guide/topics/ui/declaring-layout.html.
Look at the attribute id in the above link

This RelativeLayout layout or its LinearLayout parent is useless

I'm developing an Android 3.1. and above application.
On one activity I generate its layout dynamically. I'm using formdetail.xml as contentview:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.formdetail);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
currentFormId = extras.getString("FormId");
}
}
formdetail.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
downloadFormsButton and formErrorMsg must be always in form.
But using that xml I get this warning:
This RelativeLayout layout or its LinearLayout parent is useless
I need linearLayout to add TableLayouts programmatically.
How can I solve this warning?
Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" />
</RelativeLayout>
You either ignore it or right click your project. Click Build Path->Configure Build Path.... Under Android Lint Preferences look for UselessParent and set it's severity to ignore or click Ignore All.
EDIT:
I take that last part back. I think Ignore All will wipe all Lint warnings and errors.
I was also having the same issue with my app layout.
I don't know how and it is right way or not, but this get resolved after setting background attribute to both layouts.
Now there is no warning and working perfect. Hope it will also work for you.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/bg"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg2"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/img_BookImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:contentDescription="#string/India"
android:src="#drawable/india" />
</FrameLayout>
</FrameLayout>
That is just an warning from lint. Lint doesn't know you would later append views to this layout and it will warn you that you added an unnecessary extra view(increasing your layout depth).
You should just ignore the warning(If it really bothers you here is a link on how to remove warnings from lint).

Categories

Resources