0

设置场景:我有一个执行 AsyncTask 的片段。这是片段。

package com.IB.ecko;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

@SuppressLint("NewApi")
public class ProfileFragment extends Fragment {

public Object objarr[] = new Object[3];
Bitmap ProfileImages[];
String UserInfo[] = new String[6];
String UserInfo2[];
String Summary;
private ImageButton ProfileImageButton;
private TextView UsernameTextView;
private TextView AgeTextView;
private TextView PersonalityTextView;
private TextView SummaryTextView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_profile_fragment, container, false);

    ProfileImageButton = (ImageButton) v.findViewById(R.id.ProfileImageButton);
    UsernameTextView = (TextView)  v.findViewById(R.id.ProfileViewUserNameTextView);
    AgeTextView = (TextView) v.findViewById(R.id.ProfileViewAgeTextView);
    PersonalityTextView = (TextView) v.findViewById(R.id.ProfileViewPersonalityTextView);
    SummaryTextView = (TextView) v.findViewById(R.id.ProfileViewSummaryTextView);

    //Download Profile
    NetworkAsyncDownload task = new NetworkAsyncDownload(objarr);
    task.execute();
    // Inflate the layout for this fragment
    return v;
}
public void SetViews(){
    UserInfo = (String[]) objarr[0];
    ProfileImages = (Bitmap[]) objarr[1];
    Summary = (String) objarr[2];
    UserInfo2 = (String[]) objarr[3];
    ProfileImageButton.setImageBitmap(ProfileImages[0]);
    UsernameTextView.setText(UserInfo[0]);
    PersonalityTextView.setText(UserInfo2[4]);
    AgeTextView.setText(UserInfo[3]);
    SummaryTextView.setText(Summary);
}

}

这是异步任务。

package com.IB.ecko;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ref.WeakReference;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import android.content.Context; 
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.ImageButton;
import android.widget.TextView;

public class NetworkAsyncDownload extends AsyncTask<Void, Void, Object[]> { 


FileOutputStream desFileStream;
FileInputStream fileinputstream;
ObjectInputStream objectinputstream;
Object input1;
Object input2;
Object input3;
Object input4;
Object objarr[];
Bitmap ProfileImages[];
String UserInfo[];
String UserInfo2[];
String Summary;

private final WeakReference<Object[]> ObjArrRef;

public NetworkAsyncDownload(Object obj[]){

    ObjArrRef = new WeakReference<Object[]>(obj);

}

@Override
protected Object[] doInBackground(Void... params) {

        desFileStream = new FileOutputStream(path + "JUIFromServer");
        mFTPClient.retrieveFile("JUI", desFileStream);
        fileinputstream = new FileInputStream(path + "JUIFromServer");
        objectinputstream = new ObjectInputStream(fileinputstream);
        input1 = objectinputstream.readObject();
        mFTPClient.retrieveFile("ProfilePictures", desFileStream);
        input2 = objectinputstream.readObject();
        mFTPClient.retrieveFile("Summary", desFileStream);
        input3 = objectinputstream.readObject();
        mFTPClient.retrieveFile("JUI2", desFileStream);
        input4 = objectinputstream.readObject();
        objarr = new Object[] {input1, input2, input3, input4};

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return objarr;
}
 protected void onPostExecute(Object object[]) {
     objarr = ObjArrRef.get();
     objarr = object;
     ProfileFragment SetViews = new ProfileFragment();
     SetViews.objarr = object;
     SetViews.SetViews();
 }

}

当我加载片段时,这是它给我的错误日志。

10-07 03:28:21.358: E/AndroidRuntime(7759): FATAL EXCEPTION: main
10-07 03:28:21.358: E/AndroidRuntime(7759): java.lang.NullPointerException
10-07 03:28:21.358: E/AndroidRuntime(7759):     at com.IB.ecko.ProfileFragment.SetViews(ProfileFragment.java:44)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at com.IB.ecko.NetworkAsyncDownload.onPostExecute(NetworkAsyncDownload.java:85)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at com.IB.ecko.NetworkAsyncDownload.onPostExecute(NetworkAsyncDownload.java:1)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at android.os.Looper.loop(Looper.java:137)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at android.app.ActivityThread.main(ActivityThread.java:5227)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at java.lang.reflect.Method.invoke(Method.java:511)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:110)
10-07 03:28:21.358: E/AndroidRuntime(7759):     at dalvik.system.NativeStart.main(Native Method)

该错误似乎是由以下原因引起的:

java.lang.NullPointerException 10-07 03:28:21.358:E/AndroidRuntime(7759): at com.IB.ecko.ProfileFragment.SetViews(ProfileFragment.java:44)

这条线就在这里:UserInfo = (String[]) objarr[0];

抱歉命名,我无意让代码对读者友好。objarr 是我保存的对象数组。objarr[0] 是一个字符串数组。我之前在一个不同的活动中做过这个,但在整个片段和 AsyncTask 的事情发生时并不那么复杂,就在活动中。我不确定是什么导致了错误。您可能会看到一些冗余代码,这些代码是我尝试随机操作以使其工作的结果,然后查看是什么原因造成的。另外,我删除了所有网络代码的东西。让我知道我是否应该整理这篇文章,以便更容易阅读。

4

1 回答 1

1

似乎你有ProfileFragment.objarr = new Object[3],但每个对象都是空的。

当您初始化一个数组(new Object[])时,您会为包含对象保留空间,但每个对象在实例化之前都是空的。例如,像这样:

ProfileFragment.objarr[0] = new String[6];

然后,你可以这样做:

UserInfo = (String[]) objarr[0];
UserInfo[0] = "name";
UserInfo[1] = "surname";
....
于 2013-10-07T07:58:45.293 回答