我有一个Android应用,将数据发送到在Raspberry Pi上运行的CPP服务器。直到我更新Android Studio之前,它都可以正常工作,现在我遇到了此错误:Method threw 'java.lang.NullPointerException' exception. Cannot evaluate android.system.StructAddrinfo.toString()
我在Socket sock = null;
(请参阅下(设置了一个断点,然后单击一步,直到到达StructAddrinfo.java
(请参见下文(,这是我在上面列出的错误的地方。在此文件中,libcore.util.Objects
无法解决。
我一直在尝试修复数小时,但没有运气...
client.java:
package com.senoirdesign.seniorassistant;
import android.os.AsyncTask;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* Created by toddf on 11/20/2017.
*/
/* class for socket stuff */
public class Client extends AsyncTask<Void, Void, Void> {
String ip_addr;
int port_num;
String msg;
/* Constructor for Client class */
Client(String ip, int port, String message) {
ip_addr = ip;
port_num = port;
msg = message;
}
@Override
protected Void doInBackground(Void... args) {
Socket sock = null;
try {
sock = new Socket(ip_addr, port_num);
DataOutputStream DOS = new DataOutputStream(sock.getOutputStream());
DOS.writeUTF(msg);
DOS.flush();
sock.close();
} catch(UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
structaddrinfo.java:
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.system;
import java.net.InetAddress;
import libcore.util.Objects;
/**
* Information returned/taken by getaddrinfo(3). Corresponds to C's {@code struct addrinfo} from
* <a href="http://pubs.opengroup.org/onlinepubs/009695399/basedefs/netdb.h.html"><netdb.h></a>
*
* TODO: we currently only _take_ a StructAddrinfo; getaddrinfo returns an InetAddress[].
*
* @hide
*/
public final class StructAddrinfo {
/** Flags describing the kind of lookup to be done. (Such as AI_ADDRCONFIG.) */
public int ai_flags;
/** Desired address family for results. (Such as AF_INET6 for IPv6. AF_UNSPEC means "any".) */
public int ai_family;
/** Socket type. (Such as SOCK_DGRAM. 0 means "any".) */
public int ai_socktype;
/** Protocol. (Such as IPPROTO_IPV6 IPv6. 0 means "any".) */
public int ai_protocol;
/** Address length. (Not useful in Java.) */
// public int ai_addrlen;
/** Address. */
public InetAddress ai_addr;
/** Canonical name of service location (if AI_CANONNAME provided in ai_flags). */
// public String ai_canonname;
/** Next element in linked list. */
public StructAddrinfo ai_next;
@Override public String toString() {
return Objects.toString(this);
}
}
gradle(app(:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.senoirdesign.seniorassistant"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:appcompat-v7:25.4.0'
testCompile 'junit:junit:4.12'
}
gradle(项目(:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我解决了我的问题。经过很多事情,我遵循了这些步骤https://teamtreehouse.com/community/getting-cannot-collove-symbol-error-eror-eror-eror-after-converting-android-project-form-eceplorm-eclipse-to android-studio-with-with-with-gradle-with-gradle要更新编译SDK版本并构建项目的工具版本,然后我确实文件 ->无效CACHE&amp;重新开始。修复了它。