0

我做CustomUI.java文件使得Custome UI(通過擴展視圖)

public class CustomUI extends View { 

    Bitmap icon; 
    float left=0; 

    public CustomUI(Context context) { 
     super(context); 
     icon=BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     super.onDraw(canvas); 
     canvas.drawBitmap(icon, left, 0, null); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      setMeasuredDimension(250, 100); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     if(event.getAction()==event.ACTION_SCROLL){ 
      left=event.getX(); 
     } 
     if(left>=200){ 
      //do some activity; 
     } 
     return true; 
    } 
} 

我做了佈局custom_ui.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".CustomUI" > 

    <com.example.missedcall.CustomUI 
     android:id="@+id/single_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 
    </com.example.missedcall.CustomUI> 

</RelativeLayout> 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.missedcall" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.missedcall.CustomUI" 
      android:label="@string/title_activity_custom_ui" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

當我顯示圖形佈局顯示我很好,但是當我運行它時, 其運行n顯示空白布局並在第二次顯示後顯示錯誤失去應用程序 請幫我解決這個問題,並在android中製作自定義UI。

日誌文件: http://i.stack.imgur.com/o4L6o.jpghttps://dl.dropboxusercontent.com/u/6608612/log.JPG

+0

它可能是在構造函數?儘量不帶參數... –

回答

0

你需要在CustomUi類中創建一個帶有參數AttributeSet以及上下文的構造函數。

public CustomUi(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    icon=BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
} 
在主類

然後聲明CustomUi作爲

CustomUi customUi = (CustomUi) findViewById(R.id.single_spinner); 

希望它可以幫助..

+0

亞現在其工作正常,謝謝你的幫助:) – user2290872

0

問題就在這裏:

<activity 
     android:name="com.example.missedcall.CustomUI" 

這裏需要提供胡亞蓉類。我猜你提供的是你的CustomView的類

+0

我改變它遵循 <活動 機器人:名字=「com.example.missedcall.Main」 和主要活動有活動和 的setContentView(R.layout.custom_ui)延伸; 和custom_ui.xml含有 但仍然在一秒鐘後關閉。 請幫我解決dis問題。 – user2290872

+0

雅現在在改變活動類的定製和構造函數的CustomUi.java文件 – user2290872