运行代码时出现NULLPOINTEREXCEPTION错误

时间:2014-07-10 08:46:01

标签: android

我正在尝试运行一个简单的代码,我几乎到处都看到从FTP设备上的FTP设备上传文本文件。代码是:

package com.example.testtrials;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.jibble.simpleftp.SimpleFTP;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class TestActivity extends ActionBarActivity {

    TextView text;

    public class FTPFileUpload extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            try {
                SimpleFTP ftp = new SimpleFTP();
                // Connect to an FTP server on port 21.
                ftp.connect(params[0], 21, params[1], params[2]);
                // Set binary mode.
                ftp.bin();
                // Change to a new working directory on the FTP server.
                ftp.cwd(params[3]);
                // You can also upload from an InputStream, e.g.
                ftp.stor(new FileInputStream(new File(params[4])), params[5]);
                // Quit from the FTP server.
                ftp.disconnect();
                text.setText("Upload Successful");
            }
            catch (IOException e) {
                // Jibble.
            }
            return null;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(text);

FTPFileUpload task = new FTPFileUpload();
task.execute(//someArgumets);
    }
}

当我

时,我的LogCat显示以下错误
07-10 13:38:18.310: E/AndroidRuntime(4825): FATAL EXCEPTION: main
07-10 13:38:18.310: E/AndroidRuntime(4825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testtrials/com.example.testtrials.TestActivity}: java.lang.NullPointerException
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.os.Looper.loop(Looper.java:137)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.ActivityThread.main(ActivityThread.java:4944)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at dalvik.system.NativeStart.main(Native Method)
07-10 13:38:18.310: E/AndroidRuntime(4825): Caused by: java.lang.NullPointerException
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3669)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.view.ViewGroup.addView(ViewGroup.java:3541)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.view.ViewGroup.addView(ViewGroup.java:3517)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:337)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.Activity.setContentView(Activity.java:1944)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:221)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:105)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:82)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at com.example.testtrials.TestActivity.onCreate(TestActivity.java:45)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.Activity.performCreate(Activity.java:5188)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-10 13:38:18.310: E/AndroidRuntime(4825):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
07-10 13:38:18.310: E/AndroidRuntime(4825):     ... 11 more

之前我ClassNotFoundException似乎现在已经修好了。在此之前它没有检测到我导入的simpleFTP库。我不得不安装新的IDE,包和所有。现在几乎被困住了。谁能帮我解决这个问题?或者帮我找一个可能真正有效的完整FTP上传代码?

2 个答案:

答案 0 :(得分:2)

您需要实施onCreate(...) Activity并设置布局,然后从AsynTask

调用onCreate(......)来电
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout); 

    new FTPFileUpload().execute();
}

您还需要在TextView方法中引用onCreate(....),例如

text=findViewById(R.id.textView);

答案 1 :(得分:0)

text.setText导致错误

  1. 您无法在doInBackground方法中使用UI组件。您必须在onPostExecute()方法中使用它。

  2. 此外,文字未初始化。

  3. 您需要在活动中使用OnCreate方法。它在哪里?这是你必须给你的xml充气的地方。

  4. 您需要使用以下代码

         public class TestActivity extends ActionBarActivity 
         {
    
               @Override
               protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               //setContentView(text);  ----> WRONG
               setContentView(R.layout.activity_login);   // ---> Your layout xml name. For example if xml name is activity_login.xml
    
               FTPFileUpload task = new FTPFileUpload();
               task.execute(//someArgumets);
        }
    
         TextView text;
         public class FTPFileUpload extends AsyncTask<String, Void, String> {
    
    
        @Override
        protected String doInBackground(String... params) {
         -----Your existing code here-----------
    
        }
    
        protected void onPostExecute(Void unused) 
        {
              text=findView(R.id.textView);
              text.setText("Upload Successful");
        }
        }