Why, in this very simple example, the last statement (setBackgroundColor)
produces a crash of the application ?
public class Macumba extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView vista = (ImageView)findViewById(R.id.vista);
vista.setBackgroundColor(Color.YELLOW);
}
}
main.xml is very simple, too:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="#+id/vista"
android:layout_width="300px"
android:layout_height="330px"
android:layout_x="9px"
android:layout_y="8px"
/>
</AbsoluteLayout>
What kind of exception do you get? NullPointerException? If so, it's most likely because vista is null, which is because R.id.vista is not in your layout.
Are you sure it's the right main.xml file? And not one from eg. another project?
Related
There are a number of questions like this but the solutions haven't worked for me.
Anyways, my main activity has a button that, in its onclick method, takes me to another activity, ViewPowerActivity. I have a layout xml file for it called power_view.xml. Inside that I have some layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
...
ViewPowerActivity has the basic onCreateMethod:
public class ViewPowerActivity extends Activity {
private final static Powers powers=new StubPowers();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.power_view);
Power power=powers.getPowers().get(0);
View powerView = findViewById(R.layout.power_view);
...
}
...
}
The findViewById call above is returning null.
If I remove all the code after setContentView(...) and simply return there, it displays the empty layout just fine. I've setContentView, I've cleaned the project, I've tried setting the power view as the main activity, and all sorts of things. What else could it be?
From you code it is clear thar power_view is a layout ie. xml. So R.id.power_view is incorrect.
It seems you want to access the parent view of that layout. Then do the following.
You have to set an id to the parent LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
...
Then,
View powerView = findViewById(R.id.parentLinear);
If you want to take some other view in powerView, you should set id to that view in xml layout and initialize your that view to power_view by findViewById(R.id.your_view)
power_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
public class ViewPowerActivity extends Activity {
private final static Powers powers=new StubPowers();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.power_view);
Power power=powers.getPowers().get(0);
View powerView = (ViewGroup)findViewById(R.id.layout);
...
}
...
}
I'm new to Android (and surely I'm not an expert Java developer), so I'm sorry for the dumb question
I would like to "capture" the id of a textView and to set its text value dynamically,
here is how I'm trying to do this:
public class AndStringTestActivity extends Activity {
/** Called when the activity is first created. */
String abc = "abcabc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(abc);
setContentView(R.layout.main);
}
}
and here is the Xml from which i'm reading the id of the textview i would like to write inside
// main.xml
<?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/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
the error i receive is the usual Application stopped unexpectedly
Thanks.
Call setContentView before trying to find the TextView. The TextView does not exist before you have called setContentView (which will inflate your XML layout)
Call setContentView(R.layout.main) before you call findViewById().
I have a pretty simple application right now. Here is the main Activity class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
connectButton = (ConnectButton) findViewById(R.id.connect);
connectButton.init(this, facebook, new String [] { "publish_stream",
"friends_about_me" });
}
And here is part of the R.java class
public static final class layout {
public static final int main=0x7f030000;
}
and the main.xml
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layout1"
android:orientation="vertical" >
<ImageButton android:background="#drawable/facebookconnect" android:id="#+id/connect" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>
</LinearLayout>
</LinearLayout>
When i click on the application from the AVD, it throws an error and makes me force close the application. It is throwing a null pointer at the SetContentView(R.layout.main).
Any help would be appreciated
Clean your project. In eclipse go to Project --> Clean...
Good day!
I'm implementing aChartEngine to draw charts on Android and have faced with a trouble: I receive NullPointerException when try to add created chart view to layout with addView method.
chart_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/charts_relative_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:name="#+id/price_chart"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
// other elements
</RelativeLayout>
Charts.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
//other code
}
protected void onResume() {
super.onResume();
if (chart_price == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.price_chart);
chart_price = ChartFactory.getLineChartView(this, buildDataset(getTitles(), dateArray, priceArray), getRenderrer(paramList.get(0).length, paramList.get(1).length, maxPrice));
layout.addView(chart_price); // THIS LINE CAUSES NULLPOINTEREXCEPTION
}
else {
chart_price.repaint();
}
}
Are any ideas, what can be the reason of this error?
The problem is in your xml file.
android:name="#+id/price_chart"
should be
android:id="#+id/price_chart"
Make any change to your XML file and then save and run it again. Worked for me when I had a problem earlier tonight that was similar.
i hava a layout file like
test.xml
<?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">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/test_string" />
</RelativeLayout>
and java file like this.
test1.java
public class test1 extends Activity {
String temp="This is String"; //which change dynamically
TextView tempview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tempview=(TextView)findViewById(R.id.test_string);
tempview.setText(temp);
setContentView(R.layout.test);
}
}
what i am trying to do is show the value of temp in activity as above code. but it throws an NullPointerException. so how to do. how to set the values of sting.xml???
thanks in advance..
setContentView(R.layout.test); should come before (TextView)findViewById(R.id.test_string);