java Android 应用程序因 ChoreoGrapher 中的 NullPointerException 而崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12533006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:16:33  来源:igfitidea点击:

Android app crashes with NullPointerException in ChoreoGrapher

javaandroid

提问by Bart Friederichs

I have the following code, trying to create a simple ListAdapter in a ListView (I had this code working before and this is the only place I have changed something):

我有以下代码,尝试在 ListView 中创建一个简单的 ListAdapter (我之前使用过这段代码,这是我更改某些内容的唯一地方):

public BuddyListAdapter(Context context, HashMap<String, Buddy> hashMap) {
    buddyList = new ArrayList<Buddy>(hashMap.values());
    mInflater = LayoutInflater.from(context);
    ctx = context;
}

When stepping through, nothing happens, but when running, I get a NullPointerExceptionin ChoreoGrapher.doCallbacks, when I create an empty ArrayList, it doesn't crash:

单步执行时,什么也没有发生,但是在运行时,我得到了一个NullPointerExceptionin ChoreoGrapher.doCallbacks,当我创建一个 empty 时ArrayList,它不会崩溃:

public BuddyListAdapter(Context context, HashMap<String, Buddy> hashMap) {
    buddyList = new ArrayList<Buddy>();
    mInflater = LayoutInflater.from(context);
    ctx = context;
}

What is the ChoreoGrapherand why is it crashing my app?

它是什么ChoreoGrapher以及为什么它会导致我的应用程序崩溃?

I am completely stuck on what could be the problem, or how to figure out what is wrong. The Eclipse debugger or crash information is not really helping.

我完全被困在可能是什么问题上,或者如何找出问题所在。Eclipse 调试器或崩溃信息并没有真正的帮助。

The entire stack trace is as follows:

整个堆栈跟踪如下:

Thread [<1> main] (Suspended (exception NullPointerException))  
    Choreographer.doCallbacks(int, long) line: 558  
    Choreographer.doFrame(long, int) line: 525  
    Choreographer$FrameDisplayEventReceiver.run() line: 711 
    Handler.handleCallback(Message) line: 615   
    Choreographer$FrameHandler(Handler).dispatchMessage(Message) line: 92   
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 4745    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 511  
    ZygoteInit$MethodAndArgsCaller.run() line: 786  
    ZygoteInit.main(String[]) line: 553 
    NativeStart.main(String[]) line: not available [native method]  

I also have a Handlerin the main activity. When it is not doing anything, the listview doesn't crash. However, I remove all callbacks from it before openning the listview.

Handler在主要活动中也有一个。当它不做任何事情时,列表视图不会崩溃。但是,我在打开列表视图之前从中删除了所有回调。

采纳答案by Bart Friederichs

I am having the same thing again in another application. It seems to be caused by an unhandled exception in getViewin the list adapter.

我在另一个应用程序中再次遇到同样的事情。这似乎是由getView列表适配器中未处理的异常引起的。

回答by Peterdk

I have this bug in my app only on Android 4.4.

我的应用程序只有在 Android 4.4 上才有这个错误。

In the debugger it shows the same message as the question asker. However when not debugging the full stacktrace in DDMS is:

在调试器中,它显示与提问者相同的消息。但是,当不在 DDMS 中调试完整的堆栈跟踪时:

Full stacktrace:

完整的堆栈跟踪:

11-13 16:34:53.088: E/AndroidRuntime(1964): FATAL EXCEPTION: main
11-13 16:34:53.088: E/AndroidRuntime(1964): Process: appname, PID: 1964
11-13 16:34:53.088: E/AndroidRuntime(1964): java.lang.NullPointerException
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.TextView.makeNewLayout(TextView.java:6124)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.TextView.onMeasure(TextView.java:6419)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.Spinner.setUpChild(Spinner.java:632)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.Spinner.makeView(Spinner.java:585)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.Spinner.getBaseline(Spinner.java:431)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1262)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer.doFrame(Choreographer.java:532)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.os.Handler.handleCallback(Handler.java:733)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.os.Looper.loop(Looper.java:137)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at java.lang.reflect.Method.invoke(Method.java:515)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at dalvik.system.NativeStart.main(Native Method)

Looking through the 4.4 sourcecode of TextView at line 6124, it turns out that this is caused by missing layout params when ellipsing. This is strange, since the code is failing inside a ListAdapter, where you don't supply layout params in getView.

在第 6124 行查看 TextView 的 4.4 源代码,事实证明这是由于省略时缺少布局参数引起的。这很奇怪,因为代码在 a 中失败ListAdapter,您没有在 中提供布局参数getView

I used to inflate android.R.layout.simple_spinner_itemfor standard spinner items. It looks like this item uses ellipsing. So in my getViewI now call:

我曾经android.R.layout.simple_spinner_item为标准旋转器物品充气。看起来这个项目使用ellipsing. 所以在我getView现在调用:

view.setEllipsize(null);

This fixes my problems on Android 4.4.

这解决了我在 Android 4.4 上的问题。

Edit

编辑

It turns out the problem is not the ellipsing, but improper inflating of a view in getView.

事实证明,问题不在于省略,而在于getView.

inflater.inflate(android.R.layout.simple_spinner_item, null);//WRONG
inflater.inflate(android.R.layout.simple_spinner_item, parent, false);//GOOD

Note that you supply falseto not attach the view immediately to the parent, because this is in a adapter that attaches children when needed. Otherwise you will get errors.

请注意,false不要立即将视图附加到父视图,因为这是在需要时附加子视图的适配器中。否则你会得到错误。

When you inflate properly, you don't need to set ellipseSizeto null. This is because when you inflate the good way, with a parent, it handles the LayoutParamsproperly, causing the ellipse code not to raise NullPointerException.

当你正确充气时,你不需要设置ellipseSizenull。这是因为当你用好的方法膨胀时,它会LayoutParams正确处理,导致椭圆代码不会引发NullPointerException.

So, inflate properly, and then you don't have to worry anymore.

所以,适当充气,然后你就不必再担心了。

回答by FER

Check for places where you may have invalid type casts when manipulating your views in the code. I had the exact same error with this line

检查在代码中操作视图时可能存在无效类型转换的地方。我对这条线有完全相同的错误

picture.setLayoutParams(new ViewGroup.LayoutParams(50, 50));

The Choreographer was throwing this error "android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams"

Choreographer 抛出此错误“android.view.ViewGroup$LayoutParams 无法转换为 android.widget.LinearLayout$LayoutParams”

changing it to

将其更改为

picture.setLayoutParams(new android.widget.LinearLayout.LayoutParams(50, 50));

to fix the problem.

解决问题。