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...
Related
I have a listview in my code and I want to set adapter on it.
But the issue is, even after initializing the listview, it is still null resulting into nullPointerException. (I checked it by logging and debugging)
I'm not able to access any view from that xml.
What am I missing? Any help appreciated.
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFF"
android:dividerHeight="2dp" />
<ListView
android:id="#+id/lvTomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lvToday"
android:divider="#FFF"
android:dividerHeight="2dp" />
</RelativeLayout>
</ScrollView>
Activity file
public class DashboardActivity extends Activity {
ListView lvToday, lvTomorrow;
TextView lblCall;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
init();
}
private void init() {
lvToday = (ListView) findViewById(R.id.lvToday);
lvTomorrow = (ListView) findViewById(R.id.lvTomorrow);
TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this,
DashboardActivity.this);
// This line is giving NULL
lvToday.setAdapter(adapter);
TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
DashboardActivity.this, DashboardActivity.this);
// This line is giving NULL
lvTomorrow.setAdapter(adapter1);
}
}
1) test eclipse menu: Project -> Clean...
2) if you have more than one version for your xml layout (example layout-large, layout-xlarge,...), check if all of them have your view.
You are missing a TextView in your layout
Put a String []
along with name of your Adapter in your
OnDraw()
Method
I have to show an advertisment on the bottom of the screen using leadboltcontroller jar..I have implemented the code and I have given all the sets of permissions as well. But I am not getting any advertisment. Here is my code:
public class Leadbolt_DemoActivity extends Activity {
/** Called when the activity is first created. */
private AdController mucontroller;
private String MY_LB_SECTION_ID="328886602";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout rlbottom=(RelativeLayout)findViewById(R.id.relbottom);
final Activity act = this;
rlbottom.post(new Runnable() {
public void run() {
mucontroller = new AdController(act, MY_LB_SECTION_ID);
mucontroller.setAsynchTask(true);
mucontroller.loadNotification();
mucontroller.setAdditionalDockingMargin(50);
mucontroller.loadAd();
}
//});
});
I have given one addid of another app in another machine but still i am not getting any addid..Is the addid unique for each app & for each machine???Please help me.
use Relativelayout for footer positioning of ads
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<bla.bla.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF0000"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/ad"/>
here is the post which you are looking for..
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?
i'm getting this error when i try to run my app in the emulator (any android version): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.test/com.android.test.HomeScreen}: java.lang.NullPointerException
my question is: is this because in 2.2 there is no "golf button" in the layout, and in 3.0 there is?
this is my activity:
public class HomeScreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
Button basketball = (Button)findViewById(R.id.basketball);
basketball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent basketball = new Intent(HomeScreen.this, basketball.class);
startActivity(basketball);
}
});
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent golf = new Intent(HomeScreen.this, golf.class);
startActivity(golf);
}
});
}
}
i got 2 layouts
layout 1: layout-normal-mdpi <= running in 2.2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/basketbal"
android:layout_marginTop="46dp"
android:layout_marginRight="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
</Button>
</RelativeLayout>
layout 2: xlarge-port <= running in 3.0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:background="#drawable/basketbal"
android:layout_height="225dp"
android:layout_width="225dp"
android:layout_marginRight="70dp"
android:layout_marginTop="90dp"
android:layout_alignParentRight="true">
</Button>
<Button
android:id="#+id/golf"
android:background="#drawable/golf"
android:layout_height="550dp"
android:layout_width="211dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="390dp"
android:layout_alignParentLeft="true">
</Button>
</RelativeLayout>
if you are getting error in the below line,
Button golf = (Button)findViewById(R.id.golf);
which is not actually present in the first layout you have provided here, it is no wonder. This definitely doesn't exist and which obviously returns null to you and there you go, getting null pointer exception.
To avoid this either you have to catch the exception else you will have change the layout accordingly such that it contains both the buttons in both the layouts.
You're lacking golf button in your first layout, as you stated.
So, when you run
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
golf is set to null (first line) and then you're trying to assign it something...
Try this:
Button golf = (Button)findViewById(R.id.golf);
if (golf != null) {
golf.setOnClickListener(new View.OnClickListener() {
EDITED:
Put these lines in your 2.2 layout:
<Button
android:id="#+id/golf"
android:visibility="invisible"
android:layout_height="1dp"
android:layout_width="1dp"
android:layout_alignParentLeft="true">
</Button>
With attribute visibility="invisible" your app should work and your layout shouldn't be wasted
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);