Integer在外部类中看起来为null

时间:2015-04-15 21:46:37

标签: java android android-asynctask

我在内部asynctask类中有一个静态Integer currentheartbeat它显示了值,但是在asynctask类之外它看起来是null,我似乎无法达到它。它的原因是什么?

Github of my Android Project

我的班级:

package com.berkkarabacak.mspproje;

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

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.blob.CloudBlob;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;
import com.microsoft.azure.storage.blob.ListBlobItem;

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


public class Checkactivity extends ActionBarActivity {
    String tcnumara;
    static Integer currentheartbeat;
    static String currentheartbeatstring;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.checkheartbeat);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            tcnumara = extras.getString("tcnumber");
        }
        TextView t = (TextView) findViewById(R.id.tcnumaraset);
        t.setText(tcnumara);
        new Azure().execute();
        System.out.print(currentheartbeat);
        System.out.print(currentheartbeat);
        System.out.print(currentheartbeat);
        System.out.print(currentheartbeat);
        currentheartbeatstring=Integer.toString(currentheartbeat);
        TextView t2=(TextView)findViewById(R.id.currentheartbeat);
        t2.setText(currentheartbeatstring);


    }
    public class Azure extends AsyncTask<Void, Void, Void> {

        public static final String ConnectionString =
                "DefaultEndpointsProtocol=http;"
                        + "AccountName=mspproje;"
                        + "AccountKey=bpBHfSA4uVJVJjxaQcOCKTH7Hib/NAJslvOvnl6q65x652mUahzu6JXKPxAhyP8/w/WKp0htVS5/EVhjaGTkaA==";


        @Override
        protected Void doInBackground(Void... voids) {

            try
            {

                // Retrieve storage account from connection-string.
                CloudStorageAccount storageAccount = CloudStorageAccount.parse(ConnectionString);

                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

                // Retrieve reference to a previously created container.
                CloudBlobContainer container = blobClient.getContainerReference("mspproje");
                // Define the path to a local file.
                final String filePath = "C:\\Users\\Berk\\Desktop\\audi.jpg";

                // Create or overwrite the "myimage.jpg" blob with contents from a local file.
                CloudBlockBlob blob = container.getBlockBlobReference(MainActivity.tcnumara+".csv");
                String lastelementdeleted=blob.downloadText().substring(0, blob.downloadText().length() - 1);

                currentheartbeat=Integer.parseInt(lastelementdeleted.substring(lastelementdeleted.lastIndexOf(";")+2));
                System.out.println(currentheartbeat+"in inner async try");
            }


        catch (Exception e)
        {
            // Output the stack trace.
             e.printStackTrace();
        }



        return null;
        }
    }



}

在logcat中,您可以看到它在内部异步类中显示52但在主外部类中它看起来为空

04-16 00:35:03.244  30866-30866/com.berkkarabacak.mspproje E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.berkkarabacak.mspproje, PID: 30866
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.berkkarabacak.mspproje/com.berkkarabacak.mspproje.Checkactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
            at com.berkkarabacak.mspproje.Checkactivity.onCreate(Checkactivity.java:41)
            at android.app.Activity.performCreate(Activity.java:6283)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
04-16 00:35:03.324  30866-31340/com.berkkarabacak.mspproje I/System.out﹕ nullnullnullnull(HTTPLog)-Static: isSBSettingEnabled false
04-16 00:35:03.324  30866-31340/com.berkkarabacak.mspproje I/System.out﹕ (HTTPLog)-Static: isShipBuild true
04-16 00:35:03.324  30866-31340/com.berkkarabacak.mspproje I/System.out﹕ (HTTPLog)-Thread-17315-974616881: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
04-16 00:35:03.324  30866-31340/com.berkkarabacak.mspproje I/System.out﹕ (HTTPLog)-Static: isSBSettingEnabled false
04-16 00:35:03.504  30866-31340/com.berkkarabacak.mspproje I/System.out﹕ KnoxVpnUidStorageknoxVpnSupported API value returned is false
04-16 00:35:03.699  30866-31340/com.berkkarabacak.mspproje I/System.out﹕ (HTTPLog)-Static: isSBSettingEnabled false
04-16 00:35:03.774  30866-31340/com.berkkarabacak.mspproje I/System.out﹕ 52in inner async try

1 个答案:

答案 0 :(得分:1)

currentheartbeat以null值开头。

您正在尝试在AsyncTask有时间从Azure获取值之前打印它。

基本上你的Azure AsyncTask在后台运行 - 执行行只是在后台开始下载并立即执行下一行,此时变量为空。