Android将对象转换为字符串数组

时间:2013-10-07 07:37:39

标签: android eclipse

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

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

}

这是AsyncTask。

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]是一个String数组。我之前在一个不同的活动中完成了这个,但是并没有像整个片段和AsyncTask那样复杂,只是在活动中。我不确定导致错误的是什么。您可能会看到一些冗余代码,这些代码是我尝试使用随机内容使其工作然后查看导致它的原因。另外,我删除了所有的网络代码。让我知道我是否应该整理这篇文章,以便更容易阅读。

1 个答案:

答案 0 :(得分:1)

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

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

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

然后,你可以这样做:

UserInfo = (String[]) objarr[0];
UserInfo[0] = "name";
UserInfo[1] = "surname";
....