findViewById returning null on LinearLayout - java

I am programming an app which has many views, and then based on which view you click, it will update the views. When I try to find the layout to update the views, findViewById returns null. FYI: I am calling findViewById from a function that onCreate() calls. And yes, I am calling setContentView before calling that function.
MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateViews();
}
public void updateViews(){
LinearLayout layout = (LinearLayout) findViewById(R.id.list_layout);
layout.removeAllViews(); //NPE: Attempt to invoke virtual method 'android.widget.LinearLayout.removeAllViews()' on a null object reference
}
What can I do to fix this issue? I suspect it has something to do with layout inflation, but I am not sure.

Personally, I see only two reasons of NPE (Null Pointer Exception):
First. You don't have already LinearLayout with R.id.list_layout which is a really silly, because your project is making and it returns NPE.
You said that you have that view with this android:id. Let's say you have a TextView with this id. I swear that in that case Android Studio won't say about NPE, but about wrong view attributes like:
Hey rpurohit! There's no TextView matched with this id. Check please correctness of your configuration.
Second. You said also that you have only one view, which is this LinearLayout. I can say only
Congratulations! You've found an issue!
and explain you that you're trying to remove all views from your master ViewGroup, but like you said:
The LinearLayout is the only thing that is inside the XML file.
so your IDE tries to remove any view from its parent, but as there's no ChildViews, it tells you about Null Pointer Exception.
I'm sure that now you understand your simple mistake :-)

When you use layout.removeAllViews(). that means remove all child
into the layout and dose not contain parent layout.
If you have this code:
<Linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#android:color/holo_blue_dark">
<ToggleButton
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</Linearlayout>
and call layout.removeAllViews() method. this remove textView and toggleButton but dose not remove parent layout(layout id).
If you wantremove child and parent view . You can use like this:
((ViewGroup)layout.getParent()).removeAllViews();

Related

Android Tutorials -- Why does my app crash when I try to access an existing Text View

I am going through the Android tutorial here. I have no problem implementing it the way they have. But, I am trying to understand why this other way fails. If I enable the two commented out lines and comment out the lines after them as noted, my app crashes. It seems to me that I should be able to reference an existing text view by ID, set its text, and then set the content view to be the layout that contains the text view I have referenced. I am sure I am thinking about this the wrong way, but I'd like some clarification on why it doesn't work.
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
//TextView textView = (TextView)findViewById(R.id.text_view);
TextView textView = new TextView(this); //comment this out
textView.setTextSize(40);
textView.setText(message);
//setContentView(R.layout.activity_display_message);
setContentView(textView); //comment this out
and my 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.<myname>.myfirstapp.DisplayMessageActivity">
<TextView
android:id="#+id/text_view"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
It seems to me that I should be able to reference an existing text view by ID, set its text, and then set the content view to be the layout that contains the text view I have referenced
The logic is wrong. Only after setContentView() is called, the findViewById() can be used to get view from layout file, and then the view's methods can be called.
Your intent.getStringExtra(MyActivity.EXTRA_MESSAGE) can be called anywhere in OnCreate(), so there is nothing wrong about its position.

Setting CustomView in Android ActionBar; CustomView does not appear

I am trying to set a custom view in an ActionBar. I am using the support.v7 ActionBar - min API is 9. For some odd reason, I cannot get the custom view to appear in the ActionBar. Am I missing something painfully obvious? Any help appreciated.
Here is the layout I am inflating:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flsfjkelsjek"
android:background="#color/black"
android:textColor="#color/white"
/>
...Very simple, just a TextView for testing.
Here is the code to set this view as my custom ActionBar view:
TextView customView = (TextView) myInflater.inflate(R.layout.my_test_textview, null);
myActionBar.setDisplayShowTitleEnabled(false);
myActionBar.setDisplayHomeAsUpEnabled(true);
myActionBar.setDisplayShowCustomEnabled(true);
myActionBar.setCustomView(customView, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Again, very simple and straightforward. I have tried many different varations on this, such as:
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_SHOW_TITLE);
and have even tried that line along with all of the above. I've also tried with a TextView just created on the fly, rather than inflating one.
The custom view simply doesn't appear. When I check the value of
myActionBar.getCustomView()
it returns my TextView. However, the width of that TextView is 0. Does anyone have any idea why the CustomView is not showing up?

Create a Custom ImageButton Class with FrameLayout and TextView

I am a newbie, so, any help is appreciated. I read a lot of posts here at StackOverflow and also I searched for my doubt in Google, but it's hard to find a good answer.
Here is what I am trying to do:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageButton
android:background="#layout/roundcorners"
android:id="#+id/hug"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/hug">
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="#000000"
android:textSize="15dip"
android:textStyle="bold"
android:paddingBottom="5dip"
android:clickable="true"
android:text="Hug">
</TextView>
</FrameLayout>
Above you guys can see the XML version of what I need.
The point is... I will have many of these FrameLayouts at run time. Every information to fill out the buttons will come from a database.
So, I need to create a Java Class where I can use a loop through all the registers from my database and instantiate a new FrameLayout Class (this class must have an ImageButton and a TextView as you can see from above XML) and just pass parameters, like this:
for (int i = 0; i < mArray.length; i++) {
button = new MyNewImageButton(name, src, text);
}
The above is just to simplify. What I mean is that I will pass parameters from my database when creating an Instance of this class that I am planning to create. Of course, every single button created will be added to the layout.
So... my question is: I know how to do this using XML, but I am REALLY having a hard time to create a class to do this.
Any thoughts? Any help is appreciated.
P.S.: Sorry if I made any mistake in my English, ok? I am a Brazilian. Someday my English will be flawless! Sorry if this question was already answered.
sorry to answer my own question to make another question. I tried to use the comments but there's a limitation in the number of characters, so, I am really sorry.
Hey guys and #blessenm. Well... I tried to use inflater and I came up with the following code:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// *******************************************
setContentView(R.layout.main);
//this is my main screen
//it's a linearlayout vertical orientation
ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal);
//these two new LinearLayouts will be one above the other,
//just like two lines
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
//inside of each linearlayout I set the orientation to horizontal
//so, everytime a picture is inflated from xml, it will fill in one
//linearlayout
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
//setting linearlayout parameters, so they fill the whole screen
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//the first two inflated xml imagebuttons I add to LinearView1
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
//the next two inflated xml imagebuttons I add to LinearView2
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
//after the above, we should have a grid 2X2
//after linearlayouts are filled, I add them to the main screen
parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1));
parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1));
However this is not working. In the errorlog I get the following message:
"Unhandled event loop exception".
Any ideas about what I am doing wrong?
Thanks!
If you are just trying to create a view from the xml and add it to the layout. Just use the LayoutInflater.
Inside the activity use something like
FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
R.id.YOUR_VIEW_XML,null);
layout.addView(frame);
If you are trying to create a class extend the frame layout or the the view. Create a constructor which takes your parameters and assign's the required values.
EDIT:
To Acess Elements Inside
If you have set id's to those element, you can access them by
TextView text = (TextView)frame.findViewById(R.id.yourtextview);
Or you can use the child index like
TextView text = (TextView)frame.getChildAt(0);
It sounds like you are looking for a way to create a view class that will be an ImageButton and a TextView wrapped with a FrameLayout.
In this case, you could look into creating your own View class. Probably a View class that extends FrameLayout. See this dev article for more information about how to create a custom view. http://developer.android.com/guide/topics/ui/custom-components.html
Specifically the "Compound Controls" section: http://developer.android.com/guide/topics/ui/custom-components.html#compound

Android overlay a view ontop of everything?

Can you overlay a view on top of everything in android?
In iPhone I would get the new view set its frame.origin to (0,0) and its width and height to the width and height of self.view. Adding it to self.view would then cause it to act as an overlay, covering the content behind (or if it had a transparent background then showing the view behind).
Is there a similar technique in android? I realise that the views are slightly different (there are three types (or more...) relativelayout, linearlayout and framelayout) but is there any way to just overlay a view on top of everything indiscriminately?
Simply use RelativeLayout or FrameLayout. The last child view will overlay everything else.
Android supports a pattern which Cocoa Touch SDK doesn't: Layout management.
Layout for iPhone means to position everything absolute (besides some strech factors). Layout in android means that children will be placed in relation to eachother.
Example (second EditText will completely cover the first one):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/root_view">
<EditText
android:layout_width="fill_parent"
android:id="#+id/editText1"
android:layout_height="fill_parent">
</EditText>
<EditText
android:layout_width="fill_parent"
android:id="#+id/editText2"
android:layout_height="fill_parent">
<requestFocus></requestFocus>
</EditText>
</FrameLayout>
FrameLayout is some kind of view stack. Made for special cases.
RelativeLayout is pretty powerful. You can define rules like View A has to align parent layout bottom, View B has to align A bottom to top, etc
Update based on comment
Usually you set the content with setContentView(R.layout.your_layout) in onCreate (it will inflate the layout for you). You can do that manually and call setContentView(inflatedView), there's no difference.
The view itself might be a single view (like TextView) or a complex layout hierarchy (nested layouts, since all layouts are views themselves).
After calling setContentView your activity knows what its content looks like and you can use (FrameLayout) findViewById(R.id.root_view) to retrieve any view int this hierarchy (General pattern (ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view)).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id = "#+id/Everything"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- other actual layout stuff here EVERYTHING HERE -->
</LinearLayout>
<LinearLayout
android:id="#+id/overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</LinearLayout>
Now any view you add under LinearLayout with android:id = "#+id/overlay" will appear as overlay with gravity = right on Linear Layout with android:id="#+id/Everything"
You can use bringToFront:
View view=findViewById(R.id.btnStartGame);
view.bringToFront();
The best way is ViewOverlay , You can add any drawable as overlay to any view as its overlay since Android JellyBeanMR2(Api 18).
Add mMyDrawable to mMyView as its overlay:
mMyDrawable.setBounds(0, 0, mMyView.getMeasuredWidth(), mMyView.getMeasuredHeight())
mMyView.getOverlay().add(mMyDrawable)
I have just made a solution for it. I made a library for this to do that in a reusable way that's why you don't need to recode in your XML. Here is documentation on how to use it in Java and Kotlin. First, initialize it from an activity from where you want to show the overlay-
AppWaterMarkBuilder.doConfigure()
.setAppCompatActivity(MainActivity.this)
.setWatermarkProperty(R.layout.layout_water_mark)
.showWatermarkAfterConfig();
Then you can hide and show it from anywhere in your app -
/* For hiding the watermark*/
AppWaterMarkBuilder.hideWatermark()
/* For showing the watermark*/
AppWaterMarkBuilder.showWatermark()
Gif preview -
I have tried the awnsers before but this did not work.
Now I jsut used a LinearLayout instead of a TextureView, now it is working without any problem. Hope it helps some others who have the same problem. :)
view = (LinearLayout) findViewById(R.id.view); //this is initialized in the constructor
openWindowOnButtonClick();
public void openWindowOnButtonClick()
{
view.setAlpha((float)0.5);
FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
fb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// check if the Overlay should be visible. If this value is false, it is not shown -> show it.
if(view.getVisibility() == View.INVISIBLE)
{
view.setVisibility(View.VISIBLE);
keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
Log.d("Overlay", "Klick");
}
else if(view.getVisibility() == View.VISIBLE)
{
view.setVisibility(View.INVISIBLE);
keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
bringToFront() is super easy for programmatic adjustments, as stated above. I had some trouble getting that to work with button z order because of stateListAnimator. If you end up needing to programmatically adjust view overlays, and those views happen to be buttons, make sure to set stateListAnimator to null in your xml layout file. stateListAnimator is android's under-the-hood process to adjust translationZ of buttons when they are clicked, so the button that is clicked ends up visible on top. This is not always what you want... for full Z order control, do this:

How do I set a background image for each item of ListView in Android?

<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textStyle="bold"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/bottomtext"
android:singleLine="false"
/>
</LinearLayout>
</LinearLayout>
This is my current row. If I created a .JPEG, and I want that to be for each item...how would I change this .xml file? Where would I put the image? In Assets?
If you want to have individual backgrounds for each list item, you must declare your own custom Adapter.
Derive it from BaseAdapter and the most important part to implement is the getView(int, View, ViewGroup) method.
You must understand how android re-uses already existing list item view elements when you scroll through a list. That means: at any moment in time, there will be only as many views generated as there can be seen on screen simultaneously.
This optimal strategy of not generating too many views altogether leads to the problem that you will have to set the background for each list item according to their position currenty needed when getView is called. If you would try to set the background statically only when generating the view, it will re-appear again possibly attached to the wrong element.
The getView method either brings a "convertView" as its second parameter or not (null). If your method is called with a convertView set to something, that means: "re-use this view for the item required right now".
The technique used here is nicely described within the API demos (section Lists) and there's also a video blog for that.
Here's how it might be done:
public class MyAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
// position is the element's id to use
// convertView is either null -> create a new view for this element!
// or not null -> re-use this given view for element!
// parent is the listview all the elements are in
if (convertView == null) {
convertView = mInflater.inflate(R.layout.your_layout, null);
// here you must do whatever is needed to populate the elements of your
// list element layout
...
} else {
// re-use the given convert view
// here you must set all the elements to the required values
}
// your drawable here for this element
convertView.setBackground( ... );
// maybe here's more to do with the view
return convertView;
}
}
That's basically it. If there's only a few background drawings I would possibly cache them as well so you don't have to read the resources over and over again!
Have fun!
Did you want a list view where each row had its own background, like the one below?
In your code, you most likely would set a ListAdapter on the view. A number of the adapters that you can construct take in an layout or view argument. You should just have to set the android:background property for the view that you point this to.
For example, I might have an ArrayAdapter created like this:
new ArrayAdapter<String>(context, R.layout.item, R.id.itemName);
Of course, I use ListView's setAdapter with this adapter. In my item.xml file, I can define the text view itemName like this:
<TextView android:id="#+id/itemName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Test view" android:background="#drawable/add"/>
However, I don't think all types of ListAdapter has a constructor with that feature. Check the documentation for the type of ListAdapter that you are using. If you are using one without this constructor, I think you may have to subclass the adapter and override the adapter's getView to inflate your view from your XML file.
I have the image add.png in each of res/drawable folders.

Categories

Resources