我正在尝试打开一个活动(CryptoVars),当用户从主活动中选择菜单项但我得到标题的错误。
这是我的代码:
java class:
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清单文件:
<?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)
Dimens 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>
任何建议都会有所帮助并深表感激。
答案 0 :(得分:1)
问题是,无法找到引用的维度资源。
原因是,您在以下目录中指定了维度:values-w820dp
这意味着,您只为特定屏幕大小提供维度。
要解决此问题,只需将文件dimens.xml从文件夹values-w820dp
复制到values
。
以下是关于此主题的进一步阅读:Providing Alternative Resources