自定义android布局给出java.lang.NullPointerException

时间:2015-10-18 16:02:01

标签: java android android-layout nullpointerexception android-statusbar

我在android编程方面比较新,我在代码中检测到以下错误的原因时遇到了一些困难:

  

10-18 18:15:45.083 31605-31605 /? W / System.err的?在com.app.name.widgets.SizeNotifierRelativeLayout.onLayout(SizeNotifierRelativeLayout.java:43)       10-18 18:15:45.092 31605-31605 /? E / AndroidRuntime?致命异议:主要       显示java.lang.NullPointerException

我正在使用由我的xml文件调用的自定义布局com.app.name.widgets.SizeNotifierRelativeLayout。错误发生在第43行,其中包含以下函数中的代码super.onLayout(changed, l, t, r, b);

@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (delegate != null) {
            View rootView = this.getRootView();
            int usableViewHeight = rootView.getHeight() - AndroidUtilities.statusBarHeight - AndroidUtilities.getViewInset(rootView);
            this.getWindowVisibleDisplayFrame(rect);
            int keyboardHeight = usableViewHeight - (rect.bottom - rect.top);
            delegate.onSizeChanged(keyboardHeight);
        }
    }
}

根据我的研究,我已经识别出由于在没有创建对象的情况下声明变量而发生 NullPointerException 错误;但由于我是java新手,因此我很难确定错误发生的位置。请指出我正确的方向,我将非常感激。下面是我的活动,布局和SizeNotifierRelativeLayout文件以及logcat。提前谢谢。

Activity.java

    package com.app.name;

import com.app.name.widgets.SizeNotifierRelativeLayout;

public class Activity extends ActionBarActivity implements ISideNavigationCallback, SizeNotifierRelativeLayout.SizeNotifierRelativeLayoutDelegate, NotificationCenter.NotificationCenterDelegate {

    private SizeNotifierRelativeLayout sizeNotifierRelativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        AndroidUtilities.statusBarHeight = getStatusBarHeight();

        sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout) findViewById(R.id.chat_layout);
        sizeNotifierRelativeLayout.delegate = this;

        NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
    }

    /**
     * Get the system status bar height
     * @return
     */
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

activity_view.xml

    <com.app.name.widgets.SizeNotifierRelativeLayout
    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"
    android:id="@+id/chat_layout"
    android:background="@drawable/background"
    tools:context="com.app.name.Activity">

    <ListView
        android:id="@+id/chat_list_view"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:divider="@drawable/chat_divider"
        android:layout_width="match_parent"
        android:scrollbarStyle="outsideOverlay"
        android:layout_above="@+id/bottomlayout"
        android:layout_height="match_parent"></ListView>

    <LinearLayout
        android:id="@+id/bottomlayout"
        android:background="@android:color/white"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView android:src="@drawable/ic_msg_panel_smiles" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="8dp" android:layout_marginRight="8dp"
                android:layout_width="wrap_content" android:id="@+id/emojiButton" android:layout_alignBottom="@+id/chat_edit_text1" android:layout_marginBottom="8dp"
                android:layout_height="wrap_content" />

            <EditText
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:id="@+id/chat_edit_text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollHorizontally="false"
                android:layout_toLeftOf="@+id/enter_chat1"
                android:layout_toRightOf="@id/emojiButton"
                android:layout_toEndOf="@id/emojiButton"
                android:layout_toStartOf="@+id/enter_chat1"
                android:hint="@string/type_your_message"
                android:maxLines="4"
                android:singleLine="false"
                android:inputType="textCapSentences"

                android:textSize="18sp"
                android:paddingLeft="4dp" />

            <ImageView android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:id="@+id/enter_chat1"
                android:layout_width="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/chat_edit_text1"
                android:paddingLeft="13dp"
                android:paddingStart="13dp"
                android:paddingRight="17dp"
                android:paddingEnd="17dp"
                android:src="@drawable/ic_chat_send" />
        </RelativeLayout>
    </LinearLayout>
</com.app.name.widgets.SizeNotifierRelativeLayout>

SizeNotifierRelativeLayout.java

    package com.app.name.widgets;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.widget.RelativeLayout;

import com.app.name.AndroidUtilities;

public class SizeNotifierRelativeLayout extends RelativeLayout {

    private Rect rect = new Rect();
    public SizeNotifierRelativeLayoutDelegate delegate;

    public abstract interface SizeNotifierRelativeLayoutDelegate {
        public abstract void onSizeChanged(int keyboardHeight);
    }

    public SizeNotifierRelativeLayout(Context context) {
        super(context);
    }

    public SizeNotifierRelativeLayout(Context context, android.util.AttributeSet attrs) {
        super(context, attrs);
    }

    public SizeNotifierRelativeLayout(Context context, android.util.AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Calculate the soft keyboard height and report back to listener
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (delegate != null) {
            View rootView = this.getRootView();
            int usableViewHeight = rootView.getHeight() - AndroidUtilities.statusBarHeight - AndroidUtilities.getViewInset(rootView);
            this.getWindowVisibleDisplayFrame(rect);
            int keyboardHeight = usableViewHeight - (rect.bottom - rect.top);
            delegate.onSizeChanged(keyboardHeight);
        }
    }


}

logcat的

  

10-18 18:51:12.154 712-712 /? W / System.err的?在com.app.name.widgets.SizeNotifierRelativeLayout.onLayout(SizeNotifierRelativeLayout.java:43)       10-18 18:51:12.164 712-712 /? E / AndroidRuntime?致命异议:主要           显示java.lang.NullPointerException                   在android.widget.AbsListView.obtainView(AbsListView.java:2247)                   在android.widget.ListView.makeAndAddView(ListView.java:1849)                   在android.widget.ListView.fillDown(ListView.java:678)                   在android.widget.ListView.fillFromTop(ListView.java:739)                   在android.widget.ListView.layoutChildren(ListView.java:1664)                   在android.widget.AbsListView.onLayout(AbsListView.java:2050)                   在android.view.View.layout(View.java:14243)                   在android.view.ViewGroup.layout(ViewGroup.java:4490)                   在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)                   在com.app.name.widgets.SizeNotifierRelativeLayout.onLayout(SizeNotifierRelativeLayout.java:43)                   在android.view.View.layout(View.java:14243)                   在android.view.ViewGroup.layout(ViewGroup.java:4490)                   在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                   在android.view.View.layout(View.java:14243)                   在android.view.ViewGroup.layout(ViewGroup.java:4490)                   在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)                   在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)                   在android.widget.LinearLayout.onLayout(LinearLayout.java:1441)                   在android.view.View.layout(View.java:14243)                   在android.view.ViewGroup.layout(ViewGroup.java:4490)                   在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                   在android.view.View.layout(View.java:14243)                   在android.view.ViewGroup.layout(ViewGroup.java:4490)                   在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)                   在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)                   在android.widget.LinearLayout.onLayout(LinearLayout.java:1441)                   在android.view.View.layout(View.java:14243)                   在android.view.ViewGroup.layout(ViewGroup.java:4490)                   在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                   在android.view.View.layout(View.java:14243)                   在android.view.ViewGroup.layout(ViewGroup.java:4490)                   在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2230)                   在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1994)                   在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)                   在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4942)                   在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:776)                   在android.view.Choreographer.doCallbacks(Choreographer.java:579)                   在android.view.Choreographer.doFrame(Choreographer.java:548)                   在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:762)                   在android.os.Handler.handleCallback(Handler.java:800)                   在android.os.Handler.dispatchMessage(Handler.java:100)                   在android.os.Looper.loop(Looper.java:194)                   在android.app.ActivityThread.main(ActivityThread.java:5370)                   at java.lang.reflect.Method.invokeNative(Native Method)                   在java.lang.reflect.Method.invoke(Method.java:525)                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)                   at dalvik.system.NativeStart.main(Native Method)       10-18 18:51:12.169 549-1386 /? W / ActivityManager?强制完成活动com.app.name/.Activity

0 个答案:

没有答案