即使没有错误警告,为什么应用程序会在启动后立即关闭?

时间:2013-08-12 19:30:47

标签: android android-layout

我有三个xml文件用于布局,例如

  1. activity_behind_left_simple.xml
  2. activity_behind_right_simple.xml
  3. activity_main.xml中
  4. 我将textViews实现为activity_behind_left_simple.xml 它显示菜单列表。它工作正常。

    所以我也在activity_behind_right_simple.xml做了同样的事。

    然而,现在应用程序一启动就会关闭:(

    为什么?右边有什么问题? 的 activity_behind_left_simple.xml

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/leftBehindMenuScroll" >
    
        <LinearLayout style="@style/behindMenuScrollContent"
            android:paddingTop="25dp" >
    
            <TextView
                style="@style/behindMenuItemTitle"
                android:text="People" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <jp.fureco.IconView 
                    android:id="@+id/iconViewItem4"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:textSize="20dp"/>
    
                <TextView
                    android:layout_marginLeft="10dp"
                    style="@style/behindMenuItemLabel"
                    android:text="Visitor" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </ScrollView>
    

    activity_behind_right_simple.xml

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/rightBehindMenuScroll" >
    
        <LinearLayout style="@style/behindMenuScrollContent"
            android:paddingTop="25dp" >
    
            <TextView
                style="@style/behindMenuItemTitle"
                android:text="Messages" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <jp.fureco.IconView 
                    android:id="@+id/iconViewItem5"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:textSize="20dp"/>
    
                <TextView
                    android:layout_marginLeft="10dp"
                    style="@style/behindMenuItemLabel"
                    android:text="Received" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </ScrollView>
    

    SimpleSideDrawer.java

        ....
       public View setLeftBehindContentView(int leftBehindLayout) {
            final View content = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(leftBehindLayout, mLeftBehindBase);
    
            String[] icons = getResources().getStringArray(R.array.icons);
            IconView iv4 = (IconView) findViewById(R.id.iconViewItem4);
            iv4.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "icons.ttf"));
            iv4.setText(icons[8]);
    
            IconView iv5 = (IconView) findViewById(R.id.iconViewItem5);
            iv5.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "icons.ttf"));
            iv5.setText(icons[315]);
    
    
            mLeftPaddingRect = new Rect(content.getPaddingLeft(), content.getPaddingTop(), content.getPaddingRight(), content.getPaddingBottom());
            mLeftBehindView = content;
            return content;
        }        
    
        ....
    

    然后,如果我删除此部分,应用程序将不再关闭。
    我该如何解决?

    IconView iv5 = (IconView) findViewById(R.id.iconViewItem5);
    iv5.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "icons.ttf"));
    iv5.setText(icons[315]);
    

    MainActivity.java

    ...
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
    
        getSize();
    
        mActionBar = getActionBar();
        mActionBar.hide();
    
    
        mNav = new SimpleSideDrawer(this);
        mNav.setLeftBehindContentView(R.layout.activity_behind_left_simple);
        mNav.setRightBehindContentView(R.layout.activity_behind_right_simple);
    ....
    

    SimpleSideDrawer.java 我发现了这个。这有关系吗?

    public View setRightBehindContentView(int rightBehindLayout) {
        final View content = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(rightBehindLayout, mRightBehindBase);
        mRightPaddingRect = new Rect(content.getPaddingLeft(), content.getPaddingTop(), content.getPaddingRight(), content.getPaddingBottom());
        mRightBehindView = content;
        return content;
    }
    

    第2部分

    public View setLeftBehindContentView(int leftBehindLayout) {
        final View content = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(leftBehindLayout, mLeftBehindBase);
    
        String[] icons = getResources().getStringArray(R.array.icons);
    
        IconView iv4 = (IconView) findViewById(R.id.iconViewItem4);
        iv4.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "icons.ttf"));
        iv4.setText(icons[25]);
    
        IconView iv5 = (IconView) findViewById(R.id.iconViewItem5);
        iv5.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "icons.ttf"));
        iv5.setText(icons[26]);
    
        IconView iv6 = (IconView) findViewById(R.id.iconViewItem6);
        iv6.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "icons.ttf"));
        iv6.setText(icons[117]);        
    
        mLeftPaddingRect = new Rect(content.getPaddingLeft(), content.getPaddingTop(), content.getPaddingRight(), content.getPaddingBottom());
        mLeftBehindView = content;
        return content;
    }
    

2 个答案:

答案 0 :(得分:1)

看起来你并没有在任何地方充气activity_behind_right_simple.xml。据我所知,当你试图修改iv5时,你应该得到一个NPE。

编辑:如果您将以下代码添加到主要版面并将ScrollViews括在FrameLayout中,则会在setContentView()期间让所有内容膨胀。

<include
        layout="@layout/leftScrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<include
        layout="@layout/rightScrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

答案 1 :(得分:1)

您需要提供对膨胀布局的引用。试试你的所有观点 在setLeftBehindContentView方法中进行访问:

     e.g:  

     IconView iv5 = (IconView) content.findViewById(R.id.iconViewItem5);

注意:我假设对应于R.id.iconViewItems的视图位于膨胀的布局文件中。