0

我创建了一个具有 FTP 连接的 Android 应用程序,但是当我启动该应用程序时,它说“不幸的是,您的应用程序必须停止”,但我找不到问题所在。
我试图进入本地被动模式,但它对我没有帮助,我也尝试在我的手机上运行该应用程序,但它给了我同样的信息。

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener {
    Button scan;
    String contents;
    String format;
    TextView contentstext;
    TextView formattext;
    FTPClient  ftpclient;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //----------------------FTP-------------
        new FtpTask().execute();
        //ftpclient.enterLocalPassiveMode();
        try {
        ftpclient.changeToParentDirectory();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




        scan=(Button)findViewById(R.id.scanbutton);
        contentstext=(TextView)findViewById(R.id.contentstext);
        formattext=(TextView)findViewById(R.id.formattext);
        scan.setOnClickListener(this);

    }

    private class FtpTask extends AsyncTask<Void, Void, FTPClient> {
        protected FTPClient doInBackground(Void... args) {
            FTPClient  ftp = new FTPClient ();
            try {
                ftp.connect("ftp.drivehq.com");
                ftp.login("zule","****");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           return ftp;
        }

        protected void onPostExecute(FTPClient result) {
            Log.v("FTPTask","FTP connection complete");
            ftpclient = result;
        }

    }

这是日志猫:

09-17 12:11:30.319: E/AndroidRuntime(1113): FATAL EXCEPTION: main
09-17 12:11:30.319: E/AndroidRuntime(1113): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.market/com.example.market.MainActivity}: java.lang.NullPointerException
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.os.Looper.loop(Looper.java:137)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at java.lang.reflect.Method.invokeNative(Native Method)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at java.lang.reflect.Method.invoke(Method.java:511)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at dalvik.system.NativeStart.main(Native Method)
09-17 12:11:30.319: E/AndroidRuntime(1113): Caused by: java.lang.NullPointerException
09-17 12:11:30.319: E/AndroidRuntime(1113):     at com.example.market.MainActivity.onCreate(MainActivity.java:32)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.Activity.performCreate(Activity.java:4465)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-17 12:11:30.319: E/AndroidRuntime(1113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-17 12:11:30.319: E/AndroidRuntime(1113):     ... 11 more
4

1 回答 1

0

查看堆栈跟踪。它说你在第 32 行有一个 NullPointerException。
如果我数对了,那就是这一行:

ftpclient.changeToParentDirectory();

请注意,您的班级成员ftpClient没有被实例化;所以它仍然是null当你尝试调用changeToParentDirectory()它时。

在此调用之前添加ftpClient = new FtpClient( ... )某处应该可以解决您的问题。为点添加正确的参数。

于 2012-09-17T12:16:26.507 回答