GVKun编程网logo

Android: Avoid passing null as the view root

13

针对Android:Avoidpassingnullastheviewroot这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Androidandroid.view.ViewRoot$Call

针对Android: Avoid passing null as the view root这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Android android.view.ViewRoot$CalledFromWrongThreadException异常的解决方案、Android ClassNotFound和android.view.InflateException:、Android findViewById返回NULL、android ImageView.getDrawingCache is NULL等相关知识,希望可以帮助到你。

本文目录一览:

Android: Avoid passing null as the view root

Android: Avoid passing null as the view root

在做一个应用时把Android SDK从4.4换成6.0,使用LayoutInflater的inflate方法时出现以下情

LayoutInflater.inflate(int resource, ViewGroup root)

参数root为null时,出现warning提示

Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout''s root element)

建议添加

@SuppressLint("InflateParams")

在网上查了,如果改用View的inflate方法root为null就不会出现warning提示

View.inflate(Context context, int resource, ViewGroup root)


Android android.view.ViewRoot$CalledFromWrongThreadException异常的解决方案

Android android.view.ViewRoot$CalledFromWrongThreadException异常的解决方案

android.view.ViewRoot$CalledFromWrongThreadException异常的解决方案

在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处理,然后更新用户界面显示。但是,在主线线程之外的线程中直接更新页面显示的问题是

 

报异常:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

【只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)】

也就是说必须在一般必须在程序的主线程(也就是ui)线程中进行更新界面显示的工作。可以采用下面的方法之一来解决:

法1:

在Activity.onCreate(Bundle savedInstanceState)中创建一个Handler类的实例, 在这个Handler实例的handleMessage回调函数中调用更新界面显示的函数。

/** 
     * 启动线程用来刷新登录提示文字,N秒刷新一次 
     *  
     */  
    private class FreshWordsThread extends Thread  
    {  
        @Override  
        public void run()  
        {  
            try  
            {  
                mLoadingWords = "test";  
                mLoadhandler.sendEmptyMessage(REFRESH);  
            }  
            catch (InterruptedException e)  
            {  
                e.printStackTrace();  
                Thread.currentThread().interrupt();  
            }  
        }  
    }  
  
  
  
    //主线程中的handler  
    class LoadHandler extends Handler  
    {  
        /** 
         * 接受子线程传递的消息机制 
         */  
        @Override  
        public void handleMessage(Message msg)  
        {  
            super.handleMessage(msg);  
            int what = msg.what;  
  
            Log.i(TAG, "Main handler message code: " + what);  
            switch (what)  
            {                  
                case REFRESH:  
                {  
                    // 刷新页面的文字  
                    mLoadingText.setText(mLoadingWords);  
                    break;  
                }  
  
            }  
        }  
        
    }  

法2:利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThread(Runnable)。 这样Runnable对像就能在ui程序中被调用。

FusionField.currentActivity.runOnUiThread(new Runnable() {  
	public void run() {  
		Toast.makeText(FusionField.currentActivity, "Success", Toast.LENGTH_LONG).show();  
	}  
}); 

 

Android ClassNotFound和android.view.InflateException:

Android ClassNotFound和android.view.InflateException:

我正在尝试将facebook整合到我的应用程序中.我使用从facebook下载的facebook示例应用程序作为参考(也在developer.facebook生成了appId和所有这个过程).我很懒,所以我不想通过所有的facebook sdk的代码.简单来说,我只是在我的应用程序中添加com_android_facebook库项目,并复制我的应用程序中的facebook示例应用程序中的所有类,Example.java除外

现在我修改MyActivity类看起来像Example.java类,意味着MyActivity现在拥有Example类的所有代码以及我的主Activity.

我改变了我的应用程序的布局

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout

   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   xmlns:android="http://schemas.android.com/apk/res/android"
  >
  <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textSize="22px"
    android:textColor="#ff00ff"
    android:gravity="center"
 >
  </TextView>

<com.android.facebook.LoginButton
 android:id="@+id/login"
 android:src="@drawable/login"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
/>

         
     
      

<Button android:id="@+id/uploadButton"
    android:text="@string/upload"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

<Button android:id="@+id/requestButton"
    android:text="@string/request"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />


<Button android:id="@+id/postButton" 
    android:text="@string/post"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

<Button android:id="@+id/deletePostButton" 
    android:text="@string/delete"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

</RelativeLayout>

现在当我运行我的应用程序时,我遇到了错误

   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.myapp/com.android.myapp.MyActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: java.lang.classNotFoundException: com.android.facebook.LoginButton in loader dalvik.system.PathClassLoader@44c06850

任何帮助,建议是最受欢迎的…

解决方法:

该SDK的主要源代码中没有LoginButton.
它仅在示例代码中可用,因此它无法加载不可用的类(ClassNotFoundException)…

Android findViewById返回NULL

Android findViewById返回NULL

有时我的xml视图有一个奇怪的问题,并且包含在Android Eclipse SDK中的子元素.

例如,我有一个名为main.xml的xml视图,其中的LinearLayout和TextView是唯一的子级,其ID为textView1.一切正常(长时间).当进行一些主要的代码更改时,可能会发生findViewById(R.id.textView1);开始返回null.重新编译不会改变任何东西.真奇怪

一段时间后,我找到了一种解决方法…我将main.xml复制到main2.xml,并将setContentView(R.layout.main)更改为setContentView(R.layout.main2),并且一切正常,g继续进行,没有任何改变布局参考,甚至仅复制布局本身.没有内容被修改.在第一次运行之后,我可以删除main2.xml并将布局设置回R.layout.main.没有更多的NullPointerException.

有人知道发生了什么问题以及如何在不执行愚蠢的复制操作的情况下解决此问题吗?

最好,
哈克牛排

解决方法:

如果Eclipse有问题(例如强制关闭),这恰好发生在我身上.它对我有用的是清理项目,直到获得所有资源和引用为止.

android ImageView.getDrawingCache is NULL

android ImageView.getDrawingCache is NULL

情况描述如下:

项目上需要对一些用户图片作处理(较复杂的处理,android 自带的 API 实现不了,通过算法自己实现),当用户选中某张图片 a.jpg 的时候先将原图显示在 ImageView 中(不论图片大小,如果图片较大 ImageVIew 等比例缩小显示),因为是自己去处理图片,当然是图片越小处理越快越好,所以项目上决定采用图片显示的副本来处理而非原图(因为原图可能会很大)。

问题:

为了偷点懒,我是从 ImageView.getDrawingCache 方法获取的副本,因为这个副本才是真正的显示大小的副本(imageView.getDrawable,这个方法获取的是原图大小的图片),我的代码如下:
imageView.setDrawingCacheEnabled(Boolean.TRUE);
Bitmap tempBitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(Boolean.FALSE);  //clear drawing cache 
这个代码我在模拟器上运行没问题,tempBitmap 是有值的,不为 null;

当我在真机上测试时问题来了,tempBitmap 为 null;imageView.getDrawingCache () 这个方法根本没获取到 cache。

在网上查了点其他资料,都试了试没解决到问题,望有高人给点指点

开发环境 android 2.3.3,真机 android 2.3.5

会不会是因为这个版本的问题 ?

『分享点在国外找到点资料,可能有用的到的地方,我这按照这上面的没解决到,但其他很多人解决到鸟

http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null』

关于Android: Avoid passing null as the view root的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android android.view.ViewRoot$CalledFromWrongThreadException异常的解决方案、Android ClassNotFound和android.view.InflateException:、Android findViewById返回NULL、android ImageView.getDrawingCache is NULL的相关知识,请在本站寻找。

本文标签: