java - 无法启动 Activity 组件错误:Binary XML file line #2: Error inflating class <unknown>

标签 java android xml

当用户从主 Activity 中选择菜单项时,我试图打开一个 Activity (CryptoVars),但我收到标题错误。

这是我的代码:

java类:

    package com.android.python27;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class CryptoVars extends Activity {

    private TextView pkReceivedLabel;
    private TextView paramsReceivedLabel;

    private String server_info = "";
    private String my_mac = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crypto_vars);

        pkReceivedLabel = (TextView) this.findViewById(R.id.PKreceivedLabel);
        paramsReceivedLabel = (TextView) this.findViewById(R.id.ParamsreceivedLabel);

        WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = manager.getConnectionInfo();
        setMyMAC(info.getMacAddress());

        createServerInfoInputDialog();
        downloadVarsFromHTTPServer();
    }

    public void setServerInfo(String info) {
        this.server_info = info;
    }

    public String getServerInfo() {
        return this.server_info;
    }

    public void setMyMAC(String mac) {
        this.my_mac = mac;
    }

    public String getMyMAC() {
        return this.my_mac;
    }

    public void createServerInfoInputDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("HTTP Server info");

        // Set up the input
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                setServerInfo(input.getText().toString());
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();
    }

    public void downloadVarsFromHTTPServer() {

        String getPKrequest = "http://" + getServerInfo() + "/.pk?" + getMyMAC();
        String getParamsrequest = "http://" + getServerInfo() + "/.params?" + getMyMAC();

        //HTTP request for private key
        try {

            Log.d("HTTPrequest", "Requesting device's private key (PK)...: " + getPKrequest);

            URL url = new URL(getPKrequest);
            URLConnection conn = url.openConnection();
            int contentLength = conn.getContentLength();
            Log.d("contentLength", String.valueOf(contentLength));
            InputStream ins = url.openStream();
            byte data[] = new byte[1024];
            DataOutputStream fos = new DataOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory() + "/myPK.key"));
            int count = 0;
            while ((count = ins.read(data)) != -1) {
                fos.write(data, 0, count);
            }
            fos.flush();
            fos.close();

            Toast.makeText(this, "PK received", Toast.LENGTH_SHORT).show();
            pkReceivedLabel.setText("1.Private key received...myPK.key");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //HTTP request for crypto params
        try {

            Log.d("HTTPrequest", "Requesting crypto params...: " + getParamsrequest);

            URL url = new URL(getParamsrequest);
            URLConnection conn = url.openConnection();
            int contentLength = conn.getContentLength();
            Log.d("contentLength", String.valueOf(contentLength));
            InputStream ins = url.openStream();
            byte data[] = new byte[1024];
            DataOutputStream fos = new DataOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory() + "/params.param"));
            int count = 0;
            while ((count = ins.read(data)) != -1) {
                fos.write(data, 0, count);
            }
            fos.flush();
            fos.close();

            Toast.makeText(this, "Params received", Toast.LENGTH_SHORT).show();
            paramsReceivedLabel.setText("2.Params received...params.param");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Android list 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.python27"
    android:installLocation="internalOnly"
    android:versionCode="1"
    android:versionName="1.0">

    <supports-screens android:xlargeScreens="true" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.ACCESS_WIFI_STATE"
        android:required="true" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <application
        android:name=".ScriptApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity
            android:name=".ScriptActivity"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.googlecode.android_scripting.action.ACTION_LAUNCH_FOR_RESULT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <service android:name=".ScriptService">
            <intent-filter>
                <action android:name="com.android.python27.ScriptService" />
            </intent-filter>
        </service>
        <service android:name=".BackgroundScriptService">
            <intent-filter>
                <action android:name="com.android.python27.BackgroundScriptService" />
            </intent-filter>
        </service>
        <service android:name=".StartPREService">
            <intent-filter>
                <action android:name="com.android.python27.StartPREService" />
            </intent-filter>
        </service>
        <service android:name=".RpcServerService" />

        <activity
            android:name="com.googlecode.android_scripting.activity.FutureActivity"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@android:style/Theme.Translucent" />
        <activity
            android:name=".DialogActivity"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <activity
            android:name=".RpcServerLauncher"
            android:taskAffinity=""
            android:theme="@android:style/Theme.Translucent.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="DD Transfer" />
        <activity
            android:name=".Logs"
            android:label="Logs" />
        <activity
            android:name=".Request"
            android:label="Request" />
        <activity
            android:name=".Download"
            android:label="Download" />
        <activity
            android:name=".Cache"
            android:label="Cache" />
        <activity android:name=".CryptoVars"></activity>
    </application>

</manifest>

xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context="com.android.python27.CryptoVars">

    <TextView
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text= "1."
        android:id="@+id/PKreceivedLabel"
        android:textColor="#FF0000"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="71dp" />

    <TextView
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text= "2."
        android:id="@+id/ParamsreceivedLabel"
        android:textColor="#FF0000"
        android:layout_below="@+id/PKreceivedLabel"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="116dp" />

</RelativeLayout>

这是我得到的错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.python27/com.android.python27.CryptoVars}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
                                                                    at android.app.ActivityThread.access$700(ActivityThread.java:134)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                    at android.os.Looper.loop(Looper.java:137)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:4867)
                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:511)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
                                                                    at dalvik.system.NativeStart.main(Native Method)
                                                                 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
                                                                    at android.app.Activity.setContentView(Activity.java:1901)
                                                                    at com.android.python27.CryptoVars.onCreate(CryptoVars.java:37)
                                                                    at android.app.Activity.performCreate(Activity.java:5047)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 
                                                                    at android.app.ActivityThread.access$700(ActivityThread.java:134) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                    at android.os.Looper.loop(Looper.java:137) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:4867) 
                                                                    at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
                                                                    at dalvik.system.NativeStart.main(Native Method) 
                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                    at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316) 
                                                                    at android.app.Activity.setContentView(Activity.java:1901) 
                                                                    at com.android.python27.CryptoVars.onCreate(CryptoVars.java:37) 
                                                                    at android.app.Activity.performCreate(Activity.java:5047) 
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056) 
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 
                                                                    at android.app.ActivityThread.access$700(ActivityThread.java:134) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                    at android.os.Looper.loop(Looper.java:137) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:4867) 
                                                                    at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
                                                                    at dalvik.system.NativeStart.main(Native Method) 
                                                                 Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
                                                                    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
                                                                    at android.view.View.<init>(View.java:3411)
                                                                    at android.view.View.<init>(View.java:3342)
                                                                    at android.view.ViewGroup.<init>(ViewGroup.java:426)
                                                                    at android.widget.RelativeLayout.<init>(RelativeLayout.java:184)
                                                                    at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316) 
                                                                    at android.app.Activity.setContentView(Activity.java:1901) 
                                                                    at com.android.python27.CryptoVars.onCreate(CryptoVars.java:37) 
                                                                    at android.app.Activity.performCreate(Activity.java:5047) 
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056) 
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 
                                                                    at android.app.ActivityThread.access$700(ActivityThread.java:134) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                    at android.os.Looper.loop(Looper.java:137) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:4867) 
                                                                    at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:511)

尺寸 xml:

<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
     (such as screen margins) for screens with more than 820dp of available width. This
     would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>

任何建议都会有帮助并深表感谢。

最佳答案

问题是找不到引用的维度资源。

原因是,您在以下目录中指定了尺寸:values-w820dp 这意味着您仅为特定屏幕尺寸提供尺寸。

<小时/>

要解决此问题,只需将文件dimens.xml从文件夹values-w820dp复制到values即可。

以下是有关此主题的一些进一步阅读:Providing Alternative Resources

关于java - 无法启动 Activity 组件错误:Binary XML file line #2: Error inflating class <unknown>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34792486/

相关文章:

Android Drawable 与它的布局不匹配

c# - LINQ to Array in Silverlight 2

java - 我应该为货币/金钱使用哪种 XML 数据类型?

java - 尝试将字符串与哈希表键/值进行比较

java - 在Java中绘制方阵

android - 将资源文件迁移到Androidx

java - 更改操作栏和其他元素的颜色

java - 当 management.port 与服务器端口不同时,如何调用 OncePerRequestFilter?

android - 为什么模拟器虚拟键盘上的某些字母在输入类型为 "number"的 Android EditText 中显示数字?

android - 是否需要释放来自 OpenSL ES 音频播放器的已完成缓冲区?