2013-04-25 2 views
0

Я пытаюсь реализовать пользовательский вид в своем приложении. Подхожу эту страницу: http://developer.android.com/training/custom-views/index.htmlОшибка при реализации пользовательского вида в android:

и я создал следующее:

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/leftMenu" 
android:layout_width="100dp" 
android:layout_height="fill_parent" 
android:background="@drawable/left_menu_background" 
android:orientation="vertical" 
android:visibility="gone" > 

<ImageButton 
    android:layout_width="fill_parent" 
    android:layout_height="52dp" 
    android:background="@drawable/left_menu_close_button" 
    android:onClick="showLeftMenu" /> 

<ImageButton 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/left_menu_separator" /> 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/left_menu_buttons_container" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/left_menu_data_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/left_menu_button_transparent" 
      android:gravity="center_horizontal" 
      android:orientation="vertical" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp" 
      android:onClick="showSelectTab"> 

      <ImageButton 
       android:id="@+id/left_menu_data_image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/transparent_rectangle" 
       android:scaleType="fitXY" 
       android:src="@drawable/folder" 
       android:onClick="showSelectTab"/> 

      <TextView 
       android:id="@+id/left_menu_data" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:text="Data" 
       android:textColor="@color/my_white" 
       android:onClick="showSelectTab"/> 
     </LinearLayout> 

     <ImageButton 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/left_menu_separator" /> 

     <LinearLayout 
      android:id="@+id/left_menu_settings" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/transparent" 
      android:gravity="center_horizontal" 
      android:orientation="vertical" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp" 
      android:onClick="showSettingsPopup"> 


      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/transparent_rectangle" 
       android:scaleType="fitXY" 
       android:src="@drawable/settings" 
       android:onClick="showSettingsPopup"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:text="Settings" 
       android:textColor="@color/my_white" 
       android:onClick="showSettingsPopup"> 
      </TextView> 
     </LinearLayout> 

     <ImageButton 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/left_menu_separator" /> 
    </LinearLayout> 
</ScrollView> 

и те классы:

AppViewLinearLayout:

package com.emildesign.sgreportmanager.ui; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.LinearLayout; 

public class AppViewLinearLayout extends LinearLayout { 

public AppViewLinearLayout(Context context, AttributeSet attrs, int layoutResource) 
{ 
    super(context, attrs); 
    if (isInEditMode()) 
     return; 
    View view = LayoutInflater.from(context).inflate(layoutResource, null); 
    addView(view); 
} 

}

и: LeftSideMenu:

package com.emildesign.sgreportmanager.ui; 

import com.emildesign.sgreportmanager.R; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 

public class LeftSideMenu extends AppViewLinearLayout 
{ 
private LeftSideMenuClickListener mListener; 

public LeftSideMenu(Context context, AttributeSet attrs, int layoutResource) 
{ 
    super(context, attrs, layoutResource); 
    findViewById(R.id.left_menu_data).setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if (mListener != null) 
       mListener.dataOnClick(v); 
     } 
    }); 

    findViewById(R.id.left_menu_settings).setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if (mListener != null) 
       mListener.settingsOnClick(v); 
     } 
    }); 
} 

public interface LeftSideMenuClickListener { 
    void dataOnClick(View v); 

    void settingsOnClick(View v); 
} 

}

Теперь я пытаюсь добавить его в свой основной макет так:

<FrameLayout 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=".LoginScrActivity" > 

<com.emildesign.sgreportmanager.ui.LeftSideMenu 
    android:id="@+id/leftSideMenu" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
.... 

и по какой-то причине, я получаю это в LogCat:

E/AndroidRuntime(1034): java.lang.RuntimeException: Unable to start activity Co 
mponentInfo{com.emildesign.sgreportmanager/com.emildesign.sgreportmanager.activi 
ties.ReportsTableActivity}: android.view.InflateException: Binary XML file line 
#10: Error inflating class com.emildesign.sgreportmanager.ui.LeftSideMenu 

UPDATE:

Я сделал следующие изменения в LeftSideMenu: ушел

public LeftSideMenu(Context context, AttributeSet attrs) 
{ 
    super(context, attrs, R.layout.left_menu); 
      ..... 

ошибка, но я все еще можно увидеть мой собственный макет.

Hirarchy Viewer:

enter image description here Что я делаю неправильно? Любая помощь будет оценена по достоинству. Спасибо.

ответ

1

Добавить этот конструктор:

public LeftSideMenu (Context context, AttributeSet attrs) {...} 
+0

Спасибо, я уже сделал это. посмотрите пожалуйста вопрос обновления. но по какой-то причине я все еще не вижу своего пользовательского представления. –

+0

попробуйте запустить hierarchyviewer, что вы видите? – pskink

+0

В иерархическом представлении я вижу свое мнение. см. обновленный вопрос. –

Смежные вопросы