2014-06-05 51 views
0

我正在學習android應用程序開發我已經創建了一個項目,使用指南http://www.edumobile.org/android/android-beginner-tutorials/ftp-message-viewer-in-android/,但獲取錯誤調用「ftp_host無法解析或不是字段」和「ftp_message_result無法解析或不能領域」。ftp_host無法解析或不是字段

我的佈局文件包括以下

<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()); 
} 

}

回答

0

證明,如果你的佈局文件真的叫activity_list_item。如果不是,你必須改變

setContentView(R.layout.activity_list_item); 

,在你的onCreate() - 方法,來:

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