简体   繁体   English

Android:无法连接两个数组

[英]Android: Couldn't be able to Concatenate two Arrays

I want to concatenate the data of two String arrays into third, but every time I use this code to concatenate the arrays my App stops working and shutdown: 我想将两个String数组的数据串联成第三,但每次使用此代码来串联数组时,我的应用程序都会停止工作并关闭:

ArrayList<String> SnoTitles3;
SnoTitles3 = new ArrayList<String>();
SnoTitles3.addAll(Arrays.asList(SnoTitles2));
SnoTitles3.addAll(Arrays.asList(SnoTitles1));
String [] concatedArgs = SnoTitles3.toArray(
                             new String[SnoTitles1.length +SnoTitles2.length]);

I have read in one of the Stackoverflow question that the Arrays SnoTitles1 and SnoTitles2 needed to be declared as final in order to concatenate them into the third one, but I cannot use final keyword with those two arrays because the size of these arrays is undefined initially but the size and the data inside these arrays is declared before the concatenation. 我已经读过一个Stackoverflow问题,需要将数组SnoTitles1SnoTitles2声明为final才能将它们连接成第三个数组,但是我不能在这两个数组中使用final关键字,因为最初这些数组的大小是不确定的但是这些数组中的大小和数据是在串联之前声明的。

The data and the size of these two arrays are fetched from the XML, but everything is done before the concatenation part. 这两个数组的数据和大小是从XML获取的,但是所有操作都在连接部分之前完成。

One more thing, the app works fine when I remove above lines of code/ 还有一件事,当我删除以上代码行/

Logcat output: Logcat输出:

09-29 18:16:55.327: E/SocketStream(333): readFully was waiting for 403440 bytes, got 49152 
09-29 18:16:55.327: E/SocketStream(333): readFully was waiting for 354288 bytes, got 49152 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 305136 bytes, got 49152 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 255984 bytes, got 16384 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 239600 bytes, got 49152 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 190448 bytes, got 49152 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 141296 bytes, got 49152 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 92144 bytes, got 5524 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 86620 bytes, got 49152 
09-29 18:16:55.331: E/SocketStream(333): readFully was waiting for 37468 bytes, got 32768 
09-29 18:16:55.367: E/Trace(1028): error opening trace file: No such file or directory (2)
09-29 18:16:55.383: E/jdwp(1028): Failed writing handshake bytes: Broken pipe (-1 of 14)
09-29 18:16:55.447: E/AndroidRuntime(1028): FATAL EXCEPTION: main
09-29 18:16:55.447: E/AndroidRuntime(1028): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.news.securitynews/com.news.securitynews.MainActivity}: java.lang.NullPointerException
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.os.Looper.loop(Looper.java:137)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at java.lang.reflect.Method.invokeNative(Native Method)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at java.lang.reflect.Method.invoke(Method.java:511)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at dalvik.system.NativeStart.main(Native Method)
09-29 18:16:55.447: E/AndroidRuntime(1028): Caused by: java.lang.NullPointerException
09-29 18:16:55.447: E/AndroidRuntime(1028):     at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at java.util.Arrays.asList(Arrays.java:154)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at com.news.securitynews.MainActivity.onCreate(MainActivity.java:61)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.Activity.performCreate(Activity.java:5008)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-29 18:16:55.447: E/AndroidRuntime(1028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-29 18:16:55.447: E/AndroidRuntime(1028):     ... 11 more
09-29 18:16:55.467: E/SocketStream(333): readFully was waiting for 403440 bytes, got 49152 
09-29 18:16:55.467: E/SocketStream(333): readFully was waiting for 354288 bytes, got 49152 
09-29 18:16:55.467: E/SocketStream(333): readFully was waiting for 305136 bytes, got 49152 
09-29 18:16:55.467: E/SocketStream(333): readFully was waiting for 255984 bytes, got 49152 
09-29 18:16:55.467: E/SocketStream(333): readFully was waiting for 206832 bytes, got 196608 

Update: 更新:

I have realized that my Program has few issues, the arrays SnoTitles1 and SnoTitles2 were initialized at the top of the class but the data is added inside them in a public mehtod, here is what I mean, 我已经意识到我的程序没有什么问题,数组SnoTitles1和SnoTitles2在类的顶部被初始化,但是数据是在公共方法中添加到其中的,这就是我的意思,

This is the MainActivity Class, 这是MainActivity类,

public class MainActivity extends ListActivity {

    public String[] SnoTitles1;
    public String[] SnoTitles2;
    ArrayList<String> SnoTitles3;
    String myString = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


/* here getBlogPostsTask.execute(); and getBlogPostsTask.execute1(); add the data inside the arrays using the method update_data1() and update_data2(), which are defined at the bottom.*/


            GetJSONTitles getBlogPostsTask = new GetJSONTitles();
            getBlogPostsTask.execute();

            GetXMLTitles getBlogPostsTask1 = new GetXMLTitles();
            getBlogPostsTask1.execute(); 

/* the values of both the Arrays SnoTitles1 and SnoTitles2 are updated when the above code is executed, so I tried to concatenate them below, */ / *执行上述代码时,将更新数组SnoTitles1和SnoTitles2的值,因此我尝试在下面将它们连接起来,* /

SnoTitles3.addAll(Arrays.asList(SnoTitles2));
SnoTitles3.addAll(Arrays.asList(SnoTitles1));
String [] concatedArgs = SnoTitles3.toArray(new String[SnoTitles1.length+SnoTitles2.length]);

Here are the methods updated_data1 and update_data2 which modified the values of the Arrays, 这是方法update_data1和update_data2,它们修改了数组的值,

    public void update_data2(){
        SnoTitles2 = new String[fetchedXMLTitles.getTitle().size()];
        for (int i = 0; i < fetchedXMLTitles.getTitle().size(); i++) {      
            SnoTitles2[i] = fetchedXMLTitles.getTitle().get(i); 
        }
    }


        public void update_data1(){
        SnoTitles1 = new String[fetchedXMLTitles.getTitle().size()];
        for (int i = 0; i < fetchedXMLTitles.getTitle().size(); i++) {      
            SnoTitles2[i] = fetchedXMLTitles.getTitle().get(i); 
        }
    }


    }

both these methods are in the same class. 这两种方法都在同一类中。 I didn't shown the entire code because its too complex and its too difficult to explain it here. 我没有显示整个代码,因为它太复杂了,在这里很难解释。

The point is, If the Arrays SnoTitles1 and SnoTitles2 are modified inside the methods, then why cant I concatenate them ? 关键是,如果在方法内部修改了数组SnoTitles1和SnoTitles2,那么为什么不能将它们串联起来呢?

-- Use System.arraycopy give you the best performance. -使用System.arraycopy可以提供最佳性能。

public static String[] concatArrays(String[] src1, String[] src2) {
    if (src1 == null) {
        throw new IllegalArgumentException("src1 is required.")
    }        
    if (src2 == null) {
        throw new IllegalArgumentException("src2 is required.")
    }        

    String[] result = new String[src1.length + src2.length];

    System.arraycopy(src1, 0, result, 0, src1.length);
    System.arraycopy(src2, 0, result, src1.length, src2.length);

    return result;
}

-- Back to your issue: -回到您的问题:

You will receive NullPointerException if you perform Concatenate two array BEFORE ALL update_data1 and update_data2 done . 如果在所有update_data1和update_data2完成之前执行串联两个数组,则将收到NullPointerException It means you have to make sure these 2 methods called Before you perform array concatenation. 这意味着在执行数组串联之前,必须确保这两个方法称为。 Since these two methods called in different threads (I guess) so the best way is you have an COUNTER. 由于这两个方法在不同的线程中调用(我猜),所以最好的方法是您有一个COUNTER。 Please see my solution: 请查看我的解决方案:

// Total pending load is 2
AtomicInteger counter = new AtomicInteger(2);


public void update_data2(){
    SnoTitles2 = new String[fetchedXMLTitles.getTitle().size()];
    for (int i = 0; i < fetchedXMLTitles.getTitle().size(); i++) {      
        SnoTitles2[i] = fetchedXMLTitles.getTitle().get(i); 
    }

    if (counter.decrementAndGet() == 0) {

         // Perform array concatenation here

         concatedArgs = concateString(SnoTitles1, SnoTitles2);
    }
}


public void update_data1(){
    SnoTitles1 = new String[fetchedXMLTitles.getTitle().size()];
    for (int i = 0; i < fetchedXMLTitles.getTitle().size(); i++) {      
        SnoTitles1[i] = fetchedXMLTitles.getTitle().get(i); 
    }

    if (counter.decrementAndGet() == 0) {

         // Perform array concatenation here

         concatedArgs = concateString(SnoTitles1, SnoTitles2);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM