其他信息(下面的原始问题)
在评论部分中关注@ρяσѕρєяK建议后,我在另一个文件(SizeNotifierRelativeLayout.java
)上收到相同的错误,错误发生在第44行,即super.onLayout(changed, l, t, r, b);
。该文件和logcat的代码如下:
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);
}
@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-15 09:09:57.918 23530-23530/? W/System.err﹕ at com.app.name.widgets.SizeNotifierRelativeLayout.onLayout(SizeNotifierRelativeLayout.java:44)
10-15 09:09:57.927 23530-23530/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.AbsListView.obtainView(AbsListView.java:2247)
at android.widget.ListView.makeAndAddView(ListView.java:1849)
at android.widget.ListView.fillDown(ListView.java:678)
at android.widget.ListView.fillFromTop(ListView.java:739)
at android.widget.ListView.layoutChildren(ListView.java:1664)
at android.widget.AbsListView.onLayout(AbsListView.java:2050)
at android.view.View.layout(View.java:14243)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at com.app.name.widgets.SizeNotifierRelativeLayout.onLayout(SizeNotifierRelativeLayout.java:44)
at android.view.View.layout(View.java:14243)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14243)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
at android.view.View.layout(View.java:14243)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14243)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
at android.view.View.layout(View.java:14243)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14243)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2230)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1994)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4942)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at 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)
原始问题 - > 我有一个状态栏获取高度函数,它给了我一个
FATAL EXCEPTION:main java.lang.ExceptionInInitializerError
MyActivity
调用
AndroidUtilities.statusBarHeight = getStatusBarHeight();
功能对应错误:
at com.app.name.AndroidUtilities。(AndroidUtilities.java:31)
是
density = App.getInstance().getResources().getDisplayMetrics().density;
由于我对Android和Java相对较新,我希望能指出我所缺少的内容。谢谢!
MyActivity.java
package com.app.name;
import com.app.name.widgets.SizeNotifierRelativeLayout;
public class MyActivity extends ActionBarActivity implements ISideNavigationCallback, SizeNotifierRelativeLayout.SizeNotifierRelativeLayoutDelegate, NotificationCenter.NotificationCenterDelegate {
private SizeNotifierRelativeLayout sizeNotifierRelativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
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;
}
}
App.java
package com.app.name;
import android.app.Application;
import android.os.Handler;
public class App extends Application {
private static App Instance;
public static volatile Handler applicationHandler = null;
@Override
public void onCreate() {
super.onCreate();
Instance=this;
applicationHandler = new Handler(getInstance().getMainLooper());
NativeLoader.initNativeLibs(App.getInstance());
}
public static App getInstance()
{
return Instance;
}
}
AndroidUtilities.java
package com.app.name;
public class AndroidUtilities {
public static float density = 1;
public static int statusBarHeight = 0;
public static Point displaySize = new Point();
static {
density = App.getInstance().getResources().getDisplayMetrics().density;
checkDisplaySize();
}
}
logcat的
10-15 07:57:41.119 15076-15076/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:197)
10-15 07:57:41.130 15076-15076/? W/System.err? at com.app.name.AndroidUtilities.<clinit>(AndroidUtilities.java:31)
10-15 07:57:41.145 15076-15076/? E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.app.name.MyActivity.onCreate(MyActivity.java:197)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at 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)
Caused by: java.lang.NullPointerException
at com.app.name.AndroidUtilities.<clinit>(AndroidUtilities.java:31)
at com.app.name.MyActivity.onCreate(MyActivity.java:197)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at 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)