ftp_host无法解析或无法解析

时间:2014-06-05 15:49:09

标签: android

我正在学习在线android应用程序开发我使用http://www.edumobile.org/android/android-beginner-tutorials/ftp-message-viewer-in-android/的指南创建了一个项目,但是得到错误调用" ftp_host无法解决或者没有字段"并且" ftp_message_result无法解析或不能解析字段"。

我的布局文件包括以下

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
   <LinearLayout android:layout_width="match_parent" 
              android:layout_height="wrap_content">
    <TextView android:text="@string/ftp_server_prompt"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"/>
    <EditText android:id="@+id/ftp_host" 
              android:layout_width="match_parent"
              android:layout_height="wrap_content" 
              android:inputType="textUri">
        <requestFocus></requestFocus>
    </EditText>
</LinearLayout>
<Button android:text="@string/ftp_button_text"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:onClick="showMessage"/>
<ScrollView android:layout_width="match_parent" 
            android:layout_height="match_parent">
    <TextView android:id="@+id/ftp_message_result"
              android:textSize="@dimen/ftp_message_size" 
              android:layout_width="match_parent" 
              android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>

和我的MainActivity文件如下所示

import java.io.BufferedReader;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private EditText mFtpHost;
private TextView mFtpMessageResult;
private static final int FTP_PORT = 8080;

/** Initializes the app when it is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_item);
    mFtpHost = (EditText)findViewById(R.id.ftp_host);
    mFtpMessageResult = (TextView)findViewById(R.id.ftp_message_result);
}

public void showMessage(View clickedButton) {
    String host = mFtpHost.getText().toString();
    try {
        Socket socket = new Socket();//(host, FTP_PORT);
        BufferedReader in = SocketUtils.getReader(socket);
        List<String> results = new ArrayList<String>();
        String line = in.readLine();
        results.add(line);
        if (line.startsWith("220-")) {
            while((line = in.readLine()) != null) {
                results.add(line);
                if ((line.equals("220") || line.startsWith("220 "))) {
                    break;
                }
            }
        }
        String output = makeOutputString(results);
        mFtpMessageResult.setText(output);
        socket.close();
    } catch (Exception e) {
        mFtpMessageResult.setText("Unknown host: " + host);
        e.printStackTrace(); // View this in DDMS window
    } 
}

private String makeOutputString(List<String> results) {
    StringBuilder output = new StringBuilder();
    for (String s: results) {
        output.append(s + "\n");
    }
    return(output.toString());
}

}

1 个答案:

答案 0 :(得分:0)

证明您的布局文件是否真的称为activity_list_item。如果不是,你必须改变

setContentView(R.layout.activity_list_item);

,在你的onCreate() - Method中:

setContentView(R.layout.'THE NAME OF THE LAYOUT');