2012-05-14 67 views
0

我面臨的問題是,在給定背景時,android中的按鈕不可點擊。所以,我用ImageButton。 在Java文件中ImageButtons是如何聲明的? 我在我想放置在ImageButton的右上角的textview中有一個紅色的通報徽章。但是textView在按鈕上不可見,即它在按鈕後面隱藏了一半。這是什麼解決方案?ImageButton問題以及Android中的強制關閉項目

另一個問題是我的項目面臨着一個強制關閉的問題。 日誌貓顯示:

 
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 

什麼是強制關閉的原因是什麼?

<?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方法是: 包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"); 
    } 
} 
+0

請發佈佈局XML(可能main.xml)和你的onCreate方法。 – MByD

+1

你在onCreate方法(在phone.activities.leave.onCreate(leave.java:33)) – keyser

回答

0

在Java文件如何ImageButtons聲明?

ImageButton ib = findByViewId(R.id.yourImageButtonId); 

TextView的不超過該按鈕可見即它被半藏 後面的按鈕。這是什麼解決方案?

您可以將文本設置爲按鈕,無需在其上放置文本視圖。

你也可以給一個簡單的按鈕一個android:drawableOnTop屬性, 給定的資源出現在上面。

您的其他問題:在您的onCreate()方法

檢查線33。發佈它,如果你不能自己找到NullPointer。

+0

Thankyou尋求幫助時,在第33行有一個NullPointerException。我曾嘗試將通知作爲可繪製對象,但它只能安排在右上方?...但我想將它作爲適用於應用程序的通知徽章...在角落中,部分位於應用程序按鈕上方和外部...可以這樣做嗎? – Unknown

+0

你可以添加一個圖像,你想如何看起來像? – Ostkontentitan

+0

[link] http://www.google.co.in/imgres?hl=zh-CN&sa=X&biw=1311&bih=564&tbm=isch&prmd=imvnsa&tbnid=amOUs2jvM0ukdM:&imgrefurl=http://ipadacademy.com/2012/04/ipad-教程 - 什麼 - 是 - 推通知 - 如何-DO-I-管理推的通知和文檔ID = -sEoZ6Y8cOoFcM&imgUrl的HTTP =://ipadacademy.com/wp-content/uploads/2012/04/badge.png&w=420&h=288&ei = -PCwT6nyG4j3rQetrLQk&zoom = 1 – Unknown