I am facing this problem that buttons in android are not clickable when given a background. So, I used ImageButton.
In the Java file how are ImageButtons declared?
I have a red otification badge in a textview which I wanted to place on the top right corner of the ImageButton. But the textView is not visible over the button i.e it gets half hidden behind the button. What is the solution for this?
Another problem is that my project faces a force close problem. The log cat shows:
05-11 04:13:29.508: D/(3913): after defining intent
05-11 04:13:29.678: D/AndroidRuntime(3913): Shutting down VM
05-11 04:13:29.678: W/dalvikvm(3913): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-11 04:13:29.688: E/AndroidRuntime(3913): FATAL EXCEPTION: main
05-11 04:13:29.688: E/AndroidRuntime(3913): java.lang.RuntimeException: Unable to start activity ComponentInfo
{phone.activities/phone.activities.leave}: java.lang.NullPointerException
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.os.Looper.loop(Looper.java:123)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-11 04:13:29.688: E/AndroidRuntime(3913): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 04:13:29.688: E/AndroidRuntime(3913): at java.lang.reflect.Method.invoke(Method.java:521)
05-11 04:13:29.688: E/AndroidRuntime(3913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-11 04:13:29.688: E/AndroidRuntime(3913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-11 04:13:29.688: E/AndroidRuntime(3913): at dalvik.system.NativeStart.main(Native Method)
05-11 04:13:29.688: E/AndroidRuntime(3913): Caused by: java.lang.NullPointerException
05-11 04:13:29.688: E/AndroidRuntime(3913): at phone.activities.leave.onCreate(leave.java:33)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 04:13:29.688: E/AndroidRuntime(3913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-11 04:13:29.688: E/AndroidRuntime(3913): ... 11 more
What is the reason for force close?
<?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="fill_parent"
android:layout_gravity="right"
android:orientation="vertical" android:clickable="true">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="90dp"
android:paddingRight="58dp"
android:text="eApproval"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="22dp"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
<TableLayout
android:id="@+id/tableLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:paddingLeft="20dp"
android:paddingRight="30dp"
android:paddingTop="25dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:layout_marginLeft="10dp">
<Button
android:id="@+id/buttonLeave"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:clickable="true"
/>
<TextView
android:id="@+id/textOne"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="70dp"
android:background="@drawable/picture1"
android:gravity="center"
android:text="5"
android:textColor="#FFF"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
<RelativeLayout android:paddingLeft="20dp" android:layout_marginLeft="10dp">
<Button
android:id="@+id/buttonTime"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="25dp"
android:clickable="true"
android:height="60dp"
android:width="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textTwo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="80dp"
android:background="@drawable/picture1"
android:gravity="center"
android:text="4"
android:textColor="#FFF"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
</TableRow>
<TableRow >
<TextView
android:id="@+id/textLeave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave Requests"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:textStyle="normal|bold"
android:typeface="sans"
android:width="90dp" android:layout_marginLeft="20dp"
android:padding="5dp"
android:gravity="center"/>
<TextView
android:id="@+id/textTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time Booking"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:width="90dp" android:layout_marginLeft="50dp" android:padding="5dp"
android:gravity="center"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout android:layout_marginLeft="10dp">
<Button
android:id="@+id/buttonCart"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:height="60dp"
android:width="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="10dp"
android:clickable="true"/>
<TextView
android:id="@+id/textThree"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="70dp"
android:background="@drawable/picture1"
android:gravity="center"
android:text="2"
android:textColor="#FFF"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
<RelativeLayout android:paddingLeft="20dp" android:layout_marginLeft="10dp">
<Button
android:id="@+id/buttonInvoice"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="25dp"
android:height="60dp"
android:width="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="10dp"
android:clickable="true"/>
<TextView
android:id="@+id/textFour"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="80dp"
android:background="@drawable/picture1"
android:gravity="center"
android:text="3"
android:textColor="#FFF"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
</TableRow>
<TableRow >
<TextView
android:id="@+id/textCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:text="Shopping Cart"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:width="90dp" android:layout_marginLeft="20dp" android:padding="5dp"
android:gravity="center"/>
<TextView
android:id="@+id/textInvoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:text="Invoice"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:width="90dp" android:layout_marginLeft="50dp" android:padding="5dp"
android:gravity="center"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout android:layout_marginLeft="10dp">
<Button
android:id="@+id/buttonPurchase"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:height="60dp"
android:width="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="10dp"
android:clickable="true"/>
<TextView
android:id="@+id/textFive"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="70dp"
android:background="@drawable/picture1"
android:gravity="center"
android:text="4"
android:textColor="#FFF"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
<RelativeLayout android:paddingLeft="20dp" android:layout_marginLeft="10dp">
<Button
android:id="@+id/buttonTravel"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:clickable="true"
android:height="60dp"
android:width="30dp" />
<TextView
android:id="@+id/textSix"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="80dp"
android:background="@drawable/picture1"
android:gravity="center"
android:text="7"
android:textColor="#FFF"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
</TableRow>
<TableRow >
<TextView
android:id="@+id/textPurchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:text="Purchase Order"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:width="90dp" android:layout_marginLeft="20dp" android:padding="5dp"
android:gravity="center"/>
<TextView
android:id="@+id/textTravel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:text="Travel Approval"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:width="90dp" android:layout_marginLeft="50dp" android:padding="5dp"
android:gravity="center"/>
</TableRow>
</TableLayout>
</LinearLayout>
The onCreate method is :
package phone.activities;
import phone.activities.R;
import phone.activities.R.id;
import phone.activities.R.layout;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class EApprovalActivity extends Activity implements OnClickListener {
private static final String TAG = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button leaveButton;
leaveButton = (Button)findViewById(R.id.buttonLeave);
leaveButton.setOnClickListener(this);
Button timeButton;
timeButton = (Button)findViewById(R.id.buttonTime);
timeButton.setOnClickListener(this);
Button cartButton;
cartButton = (Button)findViewById(R.id.buttonCart);
cartButton.setOnClickListener(this);
Button invoiceButton;
invoiceButton = (Button)findViewById(R.id.buttonInvoice);
invoiceButton.setOnClickListener(this);
Button purchaseButton;
purchaseButton = (Button)findViewById(R.id.buttonPurchase);
purchaseButton.setOnClickListener(this);
Button travelButton;
travelButton = (Button)findViewById(R.id.buttonTravel);
travelButton.setOnClickListener(this);
}
public void onClick(View v) {
ComponentName cn;
switch(v.getId())
{
case R.id.buttonLeave:
Intent leaveIntent = new Intent(EApprovalActivity.this,
leave.class);
startActivity(leaveIntent);
break;
case R.id.buttonTime:
Intent timeIntent = new Intent(EApprovalActivity.this,
time.class);
startActivity(timeIntent);
break;
case R.id.buttonCart:
Intent cartIntent = new Intent(EApprovalActivity.this,
cart.class);
startActivity(cartIntent);
break;
case R.id.buttonInvoice:
Intent invoiceIntent = new Intent(EApprovalActivity.this,
invoice.class);
startActivity(invoiceIntent);
break;
case R.id.buttonPurchase:
Intent purchaseIntent = new Intent(EApprovalActivity.this,
purchase.class);
startActivity(purchaseIntent);
break;
case R.id.buttonTravel:
Intent travelIntent = new Intent(EApprovalActivity.this,
travel.class);
startActivity(travelIntent);
break;
} Log.d(TAG,"after defining intent");
}
}
You can set Text to a button no need to put a textview over it.
Also you can give a simple button a
android:drawableOnTopattribute,the given recource appears on top.
To your other problem:
Check line 33 in your onCreate() method. Post it if you cant find the NullPointer on your own.