Android onCreate ClassCastException - android

I have this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.main, null);
setContentView(layout);
s = (Spinner) findViewById(R.id.spinner1);
It throws a ClassCastException why is it?
If I do setContentView(R.layout.main) everything goes well. But i need to have the layout in a variable because I need it to use an advertsiment library. Is there a a way to inflate the XML and have the layout in an variable?
Thanks

Which line is throwing the ClassCastException?
Also you can use setContentView(R.layout.main); then still use a normal find view by id call to get a reference to your root layout.
LinearLayout layout = (LinearLayout)findViewById(R.id.yourParentId);
as long as this comes after you've called setContentView() you should get returned a reference to your layout object that you can use however you wish.

On what line does it throw the exception? Maybe the root view of the layout is not really a LinearLayout. Or maybe R.id.spinner1 is not a Spinner.

Related

findViewById() returns null for an inflated layout

I have an Activity and its layout. Now I need to add a LinearLayout from another layout, menu_layout.xml.
LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.menu_layout, null);
After this, findViewById() returns null. Is there is any solution for that?
Note: I can't put both XML in one place, and using <include> also is not working.
Explanation
When you inflate a layout, the layout is not in the UI yet, meaning the user will not be able to see it until it's been added. To do this, you must get a hold of a ViewGroup (LinearLayout,RelativeLayout,etc) and add the inflated View to it. Once they're added, you can work on them as you would with any other views including the findViewById method, adding listeners, changing properties, etc
Code
//Inside onCreate for example
setContentView(R.layout.main); //Sets the content of your activity
View otherLayout = LayoutInflater.from(this).inflate(R.layout.other,null);
//You can access them here, before adding `otherLayout` to your activity
TextView example = (TextView) otherLayout.findViewById(R.id.exampleTextView);
//This container needs to be inside main.xml
LinearLayout container = (LinearLayout)findViewById(R.id.container);
//Add the inflated view to the container
container.addView(otherLayout);
//Or access them once they're added
TextView example2 = (TextView) findViewById(R.id.exampleTextView);
//For example, adding a listener to the new layout
otherLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
}
});
Assuming
main.xml contains a LinearLayout with the id container
other.xml is a layout file in your project
other.xml contains a TextView with the id exampleTextView
Try using
layoutObject.findViewById();

Android: Button not showing on view

I'm completely new to Android and am trying to add a simple button to my view, but my view is completely blank. Here is my setup:
EDIT: I have now changed my code to write this programmatically since I'm used to that with iOS.
I'm now getting the following error:
Error: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOrientation(int)' on a null object reference
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linearLayout;
public Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// Create main view
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Create button
myButton = new Button(this);
myButton.setText("Fire Call");
// Add button to view
linearLayout.addView(myButton);
}
}
You need to setContentView which takes a View or a layout resource id.
Also, you can only call findViewById if you provide a layout resource id, and call setContentView with it. Alternatively, you could inflate it and call view.findViewById on the resulting inflated view.
Here's your code with some changes that should hopefully fix your new issue:
public class MainActivity extends Activity {
LinearLayout linearLayout;
public Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// Create main view
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Create button
myButton = new Button(this);
myButton.setText("Fire Call");
// Add button to view
linearLayout.addView(myButton);
setContentView(linearLayout);
}
}
I cannot pinpoint the error but maybe you can.
Try checking after every line you type and see if the button appears. Also check if the problem is with button only or does it extend to other layouts as well.
Just type and see what happens.
Follow it up with dimensions.
Don not forget to save after each step. Usually all such problems can be solved by detailed debugging.
If none of the layout appears, try and restart the software.
You didn't callsetContentView(R.layout.activity_main) , in your code there, you commented it out, so your layout wasnt inflated so you got a blank white screen. Uncomment that line and you will be fine.

linking my java layout object with xml layout in android

I have created an LinearLayout object in my .java file and tried to connect it with my .xml layout by using the following code,but it is not responding. Can you please explain to me why and what errors are in this code? I have named the id of the LinearLayout of .xml file as "root".
public class MainActivity extends Activity {
LinearLayout l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView HelloWorldImageView = new ImageView(this);
TextView t=new TextView(this);
t.setText("hello");
HelloWorldImageView.setImageResource(R.drawable.ic_launcher);
l=new LinearLayout(this);
l=(LinearLayout)findViewById(R.id.root);
l.addView(t);
l.addView(HelloWorldImageView);
setContentView(l);
}
}
Do it like this-
setContentView(R.layout.yourxml);
ImageView HelloWorldImageView = new ImageView(this);
TextView t=new TextView(this);
t.setText("hello");
HelloWorldImageView.setImageResource(R.drawable.ic_launcher);
l=(LinearLayout)findViewById(R.id.root);
l.addView(t);
l.addView(HelloWorldImageView);
You have to set that xml file in the setContentView() which has the root linear layout in it.
First off- you don't need the new LinearLayout line. The findViewById() will return an instantiated version. Same for the the other newed Views.
Secondly- you want to do a setContentView() before making any calls to findViewById(). This way you set the layout (by passing it a layout id), Android will instantiate all the Views in your layout, and you can get references to them via findViewById().

LayoutInflater for using xml and View class

after I asked if I should use XML or a View class for my project you told me, that I should do everything possible in XML and use a class for the rest. You told me, that animating Sprites isn't possible with XML so I wanted to make a View Class. I got the tip to google "LayoutInflater" for this and I did.
There aren't many Informations about inflaters so I visited android's developers database and tried to find out how this works.
As far as I know now, you have to put something into the onCreate method of your main game activity (the setContentView has to be the mainXML).
So now I created a LinearLayout in my mainXML and called it "container" and made this being a ViewGroup called "parent".
Now I have created a global variable "private View view" and wrote this line:
view = LayoutInflater.from(getBaseContext()).inflate(new ViewClass(this),
null);
Thw Problem now is that u can't inflate a class like this and I think I'm doing this whole inflating thing wrong.
Do you have any tips and tricks for me for making it work to have a LinearLayout in my mainXML and being able to make the content from my View Class appear in it?
EDIT:
Got it to work without errors, but nothing happens if I start my game now.
Here is the code pls answer if u have any solutions:
super.onCreate(savedInstanceState);
// inflate mainXML->
View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
// find container->
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
// initialize your custom view->
view = new GameLayout(this);
// add your custom view to container->
container.addView(view);
setContentView(R.layout.activity_game);
And my GameLayout:
public GameLayout(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.BLACK);
}
There are two ways of going about this. I'll show you one of them. Do the following in your onCreate(Bundle) before calling setContentView(...):
// inflate mainXML
View mainView = getLayoutInflater().inflate(R.layout.mainXML, null);
// find container
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
// initialize your custom view
view = new ViewClass(this);
// add your custom view to container
container.addView(view);
Finally:
setContentView(mainView);
Alternatively, you can place your custom view inside mainXML:
<your.package.name.ViewClass
android:id="#+id/myCustomView"
android:layout_width="match_parent"
android:layout_height="match_parent"
.... />

edit xml layout params dynamically before inflate

I would like to change layout params like layout_weight dynamically before inflating the layout and setting the content view for an activity. How would u do this?:
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//edit layout weight for some views in my layout here, before setting content view
setContentView(R.layout.mylayout);
}
I mean yes you can do it. By R.layout.mylayout you are just referencing to your layout resource. Just get a Layout object from that resource which might be like Layout mylayout = (Layout)findresourcebyid(R.layout.mylayout). After getting the reference you can change it dynamically and then inflate by setcontentview().

Categories

Resources