2013-10-07 31 views
0

設置場景:我有一個執行AsyncTask的Fragment。這是片段。Android將對象轉換爲字符串數組

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那樣複雜,只是在活動中。我不確定是什麼原因導致了錯誤。您可能會看到一些冗餘代碼,這是我嘗試隨機使其工作並隨後查看造成它的原因的結果。另外,我刪除了所有的網絡代碼。讓我知道如果我應該整理這篇文章,以便閱讀更容易。

+0

我建議只輸出你的objarr,看看它是否爲空。如果沒有,輸出你的objarr [0],看看裏面有什麼。例外說,有一個空指針。所以不知何故objarr [0] == null或objarr == null。所以不知何故objarr = ObjArrRef.get();不返回,你期望..檢查這一點。 –

回答

1

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

初始化一個數組(新對象[])時,爲包含對象保留空間,但每個對象都爲空,直到您將其實例化爲止。例如,像這樣:

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

然後,你可以這樣做:

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