我从谷歌下载项目示例(LocationUpdates.zip,http://developer.android.com/training/location/retrieve-current.html)。 当我运行它时,它显示如下异常,但并非总是如此:
08-10 16:10:34.119: W/System.err(26914): java.lang.NumberFormatException: Invalid int: ""
08-10 16:10:34.127: W/System.err(26914): at java.lang.Integer.invalidInt(Integer.java:138)
08-10 16:10:34.127: W/System.err(26914): at java.lang.Integer.parseInt(Integer.java:359)
08-10 16:10:34.127: W/System.err(26914): at java.lang.Integer.parseInt(Integer.java:332)
08-10 16:10:34.127: W/System.err(26914): at java.util.Calendar.getHwFirstDayOfWeek(Calendar.java:807)
08-10 16:10:34.127: W/System.err(26914): at java.util.Calendar.<init>(Calendar.java:745)
08-10 16:10:34.127: W/System.err(26914): at java.util.GregorianCalendar.<init>(GregorianCalendar.java:338)
08-10 16:10:34.127: W/System.err(26914): at java.util.GregorianCalendar.<init>(GregorianCalendar.java:325)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.ASN1Time.getDecodedObject(ASN1Time.java:44)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.ASN1UTCTime.decode(ASN1UTCTime.java:82)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.ASN1Choice.decode(ASN1Choice.java:308)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.BerInputStream.readSequence(BerInputStream.java:554)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.DerInputStream.readSequence(DerInputStream.java:105)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.ASN1Sequence.decode(ASN1Sequence.java:40)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.BerInputStream.readSequence(BerInputStream.java:554)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.DerInputStream.readSequence(DerInputStream.java:105)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.ASN1Sequence.decode(ASN1Sequence.java:40)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.BerInputStream.readSequence(BerInputStream.java:554)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.DerInputStream.readSequence(DerInputStream.java:105)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.ASN1Sequence.decode(ASN1Sequence.java:40)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.asn1.ASN1Type.decode(ASN1Type.java:91)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.provider.cert.X509CertImpl.<init>(X509CertImpl.java:102)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.provider.cert.X509CertFactoryImpl.getCertificate(X509CertFactoryImpl.java:656)
08-10 16:10:34.127: W/System.err(26914): at org.apache.harmony.security.provider.cert.X509CertFactoryImpl.engineGenerateCertificate(X509CertFactoryImpl.java:109)
08-10 16:10:34.127: W/System.err(26914): at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:195)
08-10 16:10:34.135: W/System.err(26914): at com.google.android.gms.common.GooglePlayServicesUtil.a(Unknown Source)
08-10 16:10:34.135: W/System.err(26914): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
08-10 16:10:34.135: W/System.err(26914): at com.google.android.gms.internal.p.connect(Unknown Source)
08-10 16:10:34.135: W/System.err(26914): at com.google.android.gms.location.LocationClient.connect(Unknown Source)
08-10 16:10:34.135: W/System.err(26914): at com.example.android.location.MainActivity.onStart(MainActivity.java:197)
08-10 16:10:34.135: W/System.err(26914): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
08-10 16:10:34.135: W/System.err(26914): at android.app.Activity.performStart(Activity.java:4475)
08-10 16:10:34.135: W/System.err(26914): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
08-10 16:10:34.135: W/System.err(26914): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-10 16:10:34.135: W/System.err(26914): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-10 16:10:34.135: W/System.err(26914): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-10 16:10:34.135: W/System.err(26914): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 16:10:34.135: W/System.err(26914): at android.os.Looper.loop(Looper.java:137)
08-10 16:10:34.135: W/System.err(26914): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-10 16:10:34.135: W/System.err(26914): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 16:10:34.135: W/System.err(26914): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 16:10:34.135: W/System.err(26914): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-10 16:10:34.135: W/System.err(26914): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-10 16:10:34.135: W/System.err(26914): at dalvik.system.NativeStart.main(Native Method)
代码在这里:
public class MainActivity extends FragmentActivity implements
LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
// A request to connect to Location Services
private LocationRequest mLocationRequest;
// Stores the current instantiation of the location client in this object
private LocationClient mLocationClient;
......
}
......
public void onStart() {
Log.i(TAG, "onStart of MainActivity");
super.onStart();
/*
* Connect the client. Don't re-start any requests here;
* instead, wait for onResume()
*/
Log.i(TAG, "connect() of LocationClient begin");
mLocationClient.connect();
Log.i(TAG, "connect() of LocationClient finish");
}
例外是因为句子:
mLocationClient.connect();
当我尝试新建一个MimeMessage时,还有另一个项目,它会抛出一个完全相同的异常:NumberFormatException:
MimeMessage message = new MimeMessage(session);
事实上,在我的项目中,有很多场景都会抛出这种异常,即使我没有操作数字/时间相关的变量。 这家伙跟我有同样的问题。 Android error in java InputStreamReader only with Huawei phone
我在这个例外中挣扎了很多天。我不知道为什么。希望一些Android专家可以帮助我。谢谢大家。 我的手机是华为Ascend D1 dual。
答案 0 :(得分:0)
您可能需要将LocationClient mLocationClient;
声明为类变量
答案 1 :(得分:0)
您似乎已经声明了mLocationClient变量,但是在调用另一个方法(mLocationClient.connect()方法)之前没有初始化它。
尝试将此添加到您的代码中。
mLocationClient = new LocationClient();
这将在调用该方法之前初始化变量。
如果已经处理过,那么请知道在调用mLocationClient.connect()时,如果mLocationClient为null,则会给出错误。因此,在连接之前,如果其为null,则需要设置一个设置mLocationClient的条件。例如
public void onStart() {
Log.i(TAG, "onStart of MainActivity");
super.onStart();
/*
* Connect the client. Don't re-start any requests here;
* instead, wait for onResume()
*/
Log.i(TAG, "connect() of LocationClient begin");
//Checks to see if LocationClient is not set and then sets it
setUpLocationClientIfNull();
mLocationClient.connect();
Log.i(TAG, "connect() of LocationClient finish");
}
//And now the method to set up mLocationClient if its null
private void setUpLocationClientIfNull() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(this, this, this);
}
}
这可以确保在调用mLocationClient.connect()方法之前设置了mLocationClient。
答案 2 :(得分:0)
服务器用于SSL的证书不完全遵循X.509标准,并且Apache Harmony证书解析器中断,因为它期望ASN.1 UTCTime及其创建的其他类型。
您无法替换服务器证书,您需要使用自己的SSL握手实现。