如何让ApacheCommonsNet在Android中工作java.lang.NullPointerException错误
我正在尝试让下面的AutomatedDelnetClient在使用Eclipse的Android应用程序中工作。如果我创建一个新的Java项目(不是Android)并添加下面的代码,它就可以正常工作 问题是,当我向Android项目添加相同的代码时,会收到以下错误消息:如何让ApacheCommonsNet在Android中工作java.lang.NullPointerException错误,java,android,eclipse,apache-commons-net,Java,Android,Eclipse,Apache Commons Net,我正在尝试让下面的AutomatedDelnetClient在使用Eclipse的Android应用程序中工作。如果我创建一个新的Java项目(不是Android)并添加下面的代码,它就可以正常工作 问题是,当我向Android项目添加相同的代码时,会收到以下错误消息: 05-20 14:07:25.991: E/AndroidRuntime(337): FATAL EXCEPTION: main 05-20 14:07:25.991: E/AndroidRuntime(337): java.l
05-20 14:07:25.991: E/AndroidRuntime(337): FATAL EXCEPTION: main
05-20 14:07:25.991: E/AndroidRuntime(337): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jbapp/com.example.jbapp.AutomatedTelnetClient}: java.lang.ClassCastException: com.example.jbapp.AutomatedTelnetClient
05-20 14:07:25.991: E/AndroidRuntime(337): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
05-20 14:07:25.991: E/AndroidRuntime(337): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-20 14:07:25.991: E/AndroidRuntime(337): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-20 14:07:25.991: E/AndroidRuntime(337): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-20 14:07:25.991: E/AndroidRuntime(337): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 14:07:25.991: E/AndroidRuntime(337): at android.os.Looper.loop(Looper.java:123)
05-20 14:07:25.991: E/AndroidRuntime(337): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-20 14:07:25.991: E/AndroidRuntime(337): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 14:07:25.991: E/AndroidRuntime(337): at java.lang.reflect.Method.invoke(Method.java:507)
05-20 14:07:25.991: E/AndroidRuntime(337): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-20 14:07:25.991: E/AndroidRuntime(337): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-20 14:07:25.991: E/AndroidRuntime(337): at dalvik.system.NativeStart.main(Native Method)
05-20 14:07:25.991: E/AndroidRuntime(337): Caused by: java.lang.ClassCastException: com.example.jbapp.AutomatedTelnetClient
05-20 14:07:25.991: E/AndroidRuntime(337): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-20 14:07:25.991: E/AndroidRuntime(337): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
05-20 14:07:25.991: E/AndroidRuntime(337): ... 11 more
这是AutomatedTelnetClient.class
package com.example.jbapp;
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
public class AutomatedTelnetClient {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "$";
private String server = "my.ip.add.ress";
private String user = "userName";
private String password = "password";
public AutomatedTelnetClient() {
try {
// Connect to the specified server
telnet.connect(server, 23);
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// Log the user on
readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);
// Advance to a prompt
readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
}
public void su(String password) {
try {
write("su");
readUntil("Password: ");
write(password);
prompt = "$";
readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
}
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
// boolean found = false;
char ch = (char) in.read();
while (true) {
System.out.print(ch);
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void write(String value) {
try {
out.println(value);
out.flush();
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void disconnect() {
try {
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
AutomatedTelnetClient telnet = new AutomatedTelnetClient();
System.out.println("Got Connection...");
telnet.sendCommand("ps -ef ");
System.out.println("run command");
telnet.sendCommand("ls ");
System.out.println("run command 2");
telnet.disconnect();
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.jbapp;
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class AutomatedTelnetClient extends Activity {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String server = "my.ip.add.ress";
private String prompt = "#";
private String user = "user";
private String password = "password";
String tmpOutput;
TextView outputView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputView = (TextView)findViewById(R.id.output);
}
public void sendTelnet(View view) {
// Do something in response to button}
try {
// Connect to the specified server
telnet.connect(server, 23);
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// Log the user on
readUntil("Login: ");
write(user);
readUntil("Password: ");
write(password);
readUntil("ATP>");
write("shell");
// Advance to a prompt
readUntil(prompt + " ");
write("ls");
readUntil(prompt + " ");
telnet.disconnect();
outputView.setText(tmpOutput);
finish();
}
catch (Exception e) {
e.printStackTrace();
}
}
public String readUntil(String pattern) {
// outputView.setText("DOESN'T work #2");
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
// boolean found = false;
char ch = (char) in.read();
while (true) {
System.out.print(ch);
tmpOutput = tmpOutput + ch;
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void write(String value) {
try {
outputView.setText(value);
out.println(value);
out.flush();
System.out.println(value);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jbapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-library android:name="org.apache.commons.net.telnet.TelnetClient" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".JbAndroidAppActivity"
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="com.example.jbapp.DisplayMessageActivity" />
<activity android:name="com.example.jbapp.AutomatedTelnetClient" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button_telnet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendTelnet"
android:text="@string/button_telnet" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView android:id="@+id/output"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="@string/output_message" />
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jbapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-library android:name="org.apache.commons.net.telnet.TelnetClient" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AutomatedTelnetClient"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我对Java/Andrtoid/Eclipse非常陌生,但对其他语言有丰富的经验。感谢您的帮助!:)
谢谢
我现在有了读取Telnet并将结果显示到TextView中的代码!:) AutomatedTelnetClient.class
package com.example.jbapp;
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
public class AutomatedTelnetClient {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "$";
private String server = "my.ip.add.ress";
private String user = "userName";
private String password = "password";
public AutomatedTelnetClient() {
try {
// Connect to the specified server
telnet.connect(server, 23);
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// Log the user on
readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);
// Advance to a prompt
readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
}
public void su(String password) {
try {
write("su");
readUntil("Password: ");
write(password);
prompt = "$";
readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
}
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
// boolean found = false;
char ch = (char) in.read();
while (true) {
System.out.print(ch);
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void write(String value) {
try {
out.println(value);
out.flush();
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void disconnect() {
try {
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
AutomatedTelnetClient telnet = new AutomatedTelnetClient();
System.out.println("Got Connection...");
telnet.sendCommand("ps -ef ");
System.out.println("run command");
telnet.sendCommand("ls ");
System.out.println("run command 2");
telnet.disconnect();
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.jbapp;
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class AutomatedTelnetClient extends Activity {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String server = "my.ip.add.ress";
private String prompt = "#";
private String user = "user";
private String password = "password";
String tmpOutput;
TextView outputView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputView = (TextView)findViewById(R.id.output);
}
public void sendTelnet(View view) {
// Do something in response to button}
try {
// Connect to the specified server
telnet.connect(server, 23);
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// Log the user on
readUntil("Login: ");
write(user);
readUntil("Password: ");
write(password);
readUntil("ATP>");
write("shell");
// Advance to a prompt
readUntil(prompt + " ");
write("ls");
readUntil(prompt + " ");
telnet.disconnect();
outputView.setText(tmpOutput);
finish();
}
catch (Exception e) {
e.printStackTrace();
}
}
public String readUntil(String pattern) {
// outputView.setText("DOESN'T work #2");
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
// boolean found = false;
char ch = (char) in.read();
while (true) {
System.out.print(ch);
tmpOutput = tmpOutput + ch;
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void write(String value) {
try {
outputView.setText(value);
out.println(value);
out.flush();
System.out.println(value);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jbapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-library android:name="org.apache.commons.net.telnet.TelnetClient" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".JbAndroidAppActivity"
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="com.example.jbapp.DisplayMessageActivity" />
<activity android:name="com.example.jbapp.AutomatedTelnetClient" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button_telnet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendTelnet"
android:text="@string/button_telnet" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView android:id="@+id/output"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="@string/output_message" />
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jbapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-library android:name="org.apache.commons.net.telnet.TelnetClient" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AutomatedTelnetClient"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这里是AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jbapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-library android:name="org.apache.commons.net.telnet.TelnetClient" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".JbAndroidAppActivity"
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="com.example.jbapp.DisplayMessageActivity" />
<activity android:name="com.example.jbapp.AutomatedTelnetClient" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button_telnet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendTelnet"
android:text="@string/button_telnet" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView android:id="@+id/output"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="@string/output_message" />
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jbapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-library android:name="org.apache.commons.net.telnet.TelnetClient" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AutomatedTelnetClient"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
非常感谢您的帮助!!:)假设您正在使用Eclipse,在
res/layout
文件夹下,您应该会看到一个main.xml
。这包含一个基本布局。您要做的是将android:id=“@+id/output”
添加到TextView
。然后,您可以通过TextView output=(TextView)findViewById(R.id.output)在代码中访问它
并使用output.setText(myText)代码>
当然,您需要活动。在活动中创建方法并使用setContentView(R.layout.main)代码>我从来没有尝试过使用任意的、非活动的类来创建意图(我想)——你确定你能做到吗?我认为它必须是一个组件类。没有说我是对的,只是有点惊讶。我不知道!:)-老实说,我在不断学习我现在“有点”让它工作了,请看下面我的答案。下一个问题是我对输出做了什么,现在它将成为LogCat,我如何让它在屏幕上显示文本视图?这有点道理。。。我正在努力解决的问题是如何通过output.settext(myText)获得Telnet输出赋值?查看您的代码,您可以将findView…
和output.setText
放在write
方法中,并使用value
而不是myText
。谢谢。我觉得我终于有进展了!:)-最新的错误现在是Java.lang.NullPointerException,每当我尝试向TextView写入任何内容时。。。不确定我在活动中是否有正确的代码。一旦创建,请确保findView…
位于setContentView
之后。设置断点并逐步遍历代码以查找未设置的变量也很方便。嗯,我在上面的“我的答案”中用您建议的更改编辑了代码。您能看到导致NullPointerException
的任何明显原因吗?(当执行中的TextView输出…
行时发生。)