我面临这个问题,即在给定背景时,android 中的按钮不可点击。所以,我使用了 ImageButton。 在 Java 文件中如何声明 ImageButtons? 我在文本视图中有一个红色的 otification 徽章,我想将它放在 ImageButton 的右上角。但是textView 在按钮上不可见,即它在按钮后面隐藏了一半。解决方案是什么?
另一个问题是我的项目面临强制关闭问题。日志猫显示:
05-11 04:13:29.508: D/(3913): 在定义意图之后 05-11 04:13:29.678: D/AndroidRuntime(3913): 关闭虚拟机 05-11 04:13:29.678: W/dalvikvm(3913): threadid=1: 线程退出未捕获异常 (group=0x4001d800) 05-11 04:13:29.688:E/AndroidRuntime(3913):致命异常:主要 05-11 04:13:29.688: E/AndroidRuntime(3913): java.lang.RuntimeException: 无法启动活动组件信息 {phone.activities/phone.activities.leave}:java.lang.NullPointerException 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.os.Handler.dispatchMessage(Handler.java:99) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.os.Looper.loop(Looper.java:123) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 java.lang.reflect.Method.invoke(Method.java:521) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 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): 由: java.lang.NullPointerException 引起 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 phone.activities.leave.onCreate(leave.java:33) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-11 04:13:29.688: E/AndroidRuntime(3913): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-11 04:13:29.688: E/AndroidRuntime(3913): ... 11 更多
强制关闭的原因是什么?
<?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>
onCreate 方法是: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");
}
}