1

我在 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/Android 运行时?致命异常:主要 java.lang.NullPointerException

我正在使用com.app.name.widgets.SizeNotifierRelativeLayout由我的 xml 文件调用的自定义布局。错误发生在第 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。提前致谢。

活动.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;
    }
}

活动视图.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);
        }
    }


}

日志猫

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/Android 运行时?致命异常:在 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. 800) 在 android.os.Handler.dispatchMessage(Handler.java:100) 在 android.os.Looper.loop(Looper.java:194) 在 android.app.ActivityThread.main(ActivityThread.java:5370) 在 java. lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) 10-18 18:51:12.169 549-1386/? 带活动管理器?强制完成活动 com.app.name/.Activity 194) 在 android.app.ActivityThread.main(ActivityThread.java:5370) 在 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) 在 dalvik.system.NativeStart.main(Native Method) 10-18 18:51:12.169 549-1386/? 带活动管理器?强制完成活动 com.app.name/.Activity 194) 在 android.app.ActivityThread.main(ActivityThread.java:5370) 在 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) 10-18 18:51:12.169 549-1386/? 带活动管理器?强制完成活动 com.app.name/.Activity ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) 10-18 18:51: 12.169 549-1386/? 带活动管理器?强制完成活动 com.app.name/.Activity ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) 10-18 18:51: 12.169 549-1386/? 带活动管理器?强制完成活动 com.app.name/.Activity

4

0 回答 0