【问题标题】:Layout error : java.lang.UnsupportedOperationException: Can't convert to dimension布局错误:java.lang.UnsupportedOperationException:无法转换为维度
【发布时间】:2016-01-04 16:16:11
【问题描述】:

当单击 ListActivity 中的项目并打开 MyBrowser Activity 时,应用程序会不幸停止。我已经检查了 LogCat,它显示了一个致命错误。在这方面找不到解决方案。 其他活动在模拟器中运行良好,但点击浏览器后,它显示致命异常。

LogCat

    ` --------- beginning of crash
    01-04 21:26:13.013    2487-2487/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.dell.firstapp, PID: 2487
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.firstapp/com.example.dell.firstapp.MyBrowser}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5257)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
         Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
                at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:572)
                at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6660)
                at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1864)
                at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1776)
                at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:62)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:808)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
                at android.app.Activity.setContentView(Activity.java:2145)
                at com.example.dell.firstapp.MyBrowser.onCreate(MyBrowser.java:22)
                at android.app.Activity.performCreate(Activity.java:5990)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5257)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
        android:weightSum="100"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <EditText

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/etURL"
            android:layout_weight="20" />
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Go"
            android:id="@+id/bGo"
            android:layout_weight="80"
            android:layout_marginRight="@+id/editText"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:weightSum="8"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bBack"
            android:text="Go back page"
            android:layout_weight="2"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bFwd"
            android:text="Go forward"
            android:layout_weight="2"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bRefresh"
            android:text="Refresh page"
            android:layout_weight="2"/>
        <Button android:text="clear History"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bHistory"
            android:layout_weight="2"/>
    </LinearLayout>
    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/wvBrowser"></WebView>

</LinearLayout>

Android 清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dell.firstapp" >
    <uses-sdk android:minSdkVersion="8"/>
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Starting"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.dell.firstapp.STARTING" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.dell.firstapp.MENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Email"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.dell.firstapp.EMAIL" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Camera" android:label="@string/app_name">
        </activity>
        <activity
            android:name=".MyBrowser" android:label="@string/app_name">
        </activity>
    </application>
</manifest>

活动类

    public class MyBrowser extends Activity implements View.OnClickListener{

            EditText url;
            WebView ourBrow;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.simplebrowser);

                WebView ourBrow=(WebView) findViewById(R.id.wvBrowser);
                ourBrow.getSettings().setJavaScriptEnabled(true);
                ourBrow.getSettings().setLoadWithOverviewMode(true);
                ourBrow.getSettings().setUseWideViewPort(true);

                ourBrow.setWebViewClient(new ourViewClient());
                try {
                    ourBrow.loadUrl("http://www.google.com");
                }catch (Exception e){
                    e.printStackTrace();
                }

                Button go=(Button) findViewById(R.id.bGo);
                Button back=(Button) findViewById(R.id.bBack);
                Button forward=(Button) findViewById(R.id.bFwd);
                Button refresh=(Button) findViewById(R.id.bRefresh);
                Button clearHistory=(Button) findViewById(R.id.bHistory);
                url=(EditText) findViewById(R.id.etURL);
                go.setOnClickListener(this);
                back.setOnClickListener(this);
                forward.setOnClickListener(this);
                refresh.setOnClickListener(this);
                clearHistory.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.bGo:
                        String theWebsite=url.getText().toString();
                        ourBrow.loadUrl(theWebsite);
                        //Hiding the keyboard after using an EditText
                        InputMethodManager imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(url.getWindowToken(),0);
                        break;
                    case R.id.bBack:
                        if(ourBrow.canGoBack())
                            ourBrow.goBack();
                        break;
                    case R.id.bFwd:
                        if(ourBrow.canGoForward())
                            ourBrow.goForward();
                        break;
                    case R.id.bRefresh:
                        ourBrow.reload();
                        break;
                    case R.id.bHistory:
                        ourBrow.clearHistory();
                        break;
                }
            }
        }

编辑:新异常

01-04 23:37:11.766    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:11.864    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:12.000    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:16.058    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:16.122    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:16.202    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:16.373    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:16.471    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:16.585    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:19.396    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:19.496    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:19.574    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:19.736    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:19.802    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:19.916    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:20.366    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:20.468    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:20.549    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:20.735    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:20.798    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:20.879    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:21.065    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:21.183    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:21.261    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:21.456    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:21.572    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:21.685    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:21.872    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:21.985    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:22.065    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:22.232    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:22.337    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:22.422    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:23.628    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:23.737    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:23.817    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:24.002    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)
01-04 23:37:24.053    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44
01-04 23:37:24.136    2227-2243/? E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
01-04 23:37:24.337    2227-2243/? E/eglCodecCommon﹕ **** ERROR unknown type 0x73000f (glSizeof,72)

【问题讨论】:

    标签: android fatal-error crash


    【解决方案1】:

    你的错误是

    UnsupportedOperationException: Can't convert to dimension
    

    这是由以下行(对于您的 Go 按钮)引起的,因为 Android 无法将 @+id/editText 转换为维度:

    android:layout_marginRight="@+id/editText"
    

    android:layout_marginRight 需要一个维度,由数字或维度资源提供,请参见下面的示例

    // number
    android:layout_marginRight="16dp"
    
    // dimension resource
    android:layout_marginRight="@dimen/margin_size"
    

    【讨论】:

    • 酷,很高兴你成功了!如果它解决了您的问题,请upvote(completely optionnal)/accept the answer,因此问题可以标记为已解决。在此先感谢 ;)
    • 一旦我再次点击 go 按钮,应用程序就会停止......并且也无法使用其他按钮
    • 有什么异常?
    • 此错误与您的第一个问题不同,很可能是与模拟器相关的错误,请参阅this question。请不要在一个 stackoverflow 问题中混合多个问题,因为这会使其他人难以理解最初的问题和相关的解决方案。如果我的回答解决了您的第一个问题,请将其标记为已接受,然后为您的下一个问题创建一个不同的问题,并且不要忘记首先在 google 上搜索已有的解决方案;)
    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多