简体   繁体   English

如何从 Android Activity 调用另一个 Activity AsyncTask 类获取 NPE

[英]How to Call a Another Activity AsyncTask Class from a Android Activity Getting NPE

  1. ABC.class First activity Class. ABC.class第一个活动类。
  2. XYZ.class is AsyncTask Activity Class. XYZ.class是 AsyncTask 活动类。

having More than 10 Async task in my application ,So i kept Async task in Separate Class.我的应用程序中有超过 10 个异步任务,所以我将异步任务保留在单独的类中。

I am Calling the Another Activity Asynctask Class(XYZ.class) from a Activity(ABC.class).我正在从活动(ABC.class)调用另一个活动异步任务类(XYZ.class)。

Problem:问题:

1.I Can call that Async Task from ABC.class but, I Kept Some Instance values inside OnCreate of XYZ.class Class(Getting Azure Local Database Data) . 1.我可以从ABC.class调用该异步任务,但是,我在XYZ.class Class(Getting Azure Local Database Data) 的OnCreate中保留了一些实例值。 So Asynctask Getting NPE.所以 Asynctask 获得 NPE。

2.Oncreate Method is Not Running, When that Async task is calling From Other Activity. 2.Oncreate 方法未运行,当该异步任务从其他活动调用时。

3.Inside doinbackgroud iam getting NPE Error. 3.Inside doinbackgroud 我收到 NPE 错误。

help me how to Solve this ,else Suggest me Any other Solution.帮助我如何解决这个问题,否则建议我任何其他解决方案。

EDIT:1编辑:1

In my Async task Iam fetching Data from Azure Server to Local DB ,so i need Instances what i kept inside Oncreate .在我的异步任务中,我将数据从Azure 服务器提取到本地数据库,所以我需要我保存在Oncreate 中的实例。

ABC.class ABC.class

Calling the Async Task调用异步任务

 AsyncTaskload_UserGroupMappingTableClass myClass = new AsyncTaskload_UserGroupMappingTableClass(getApplicationContext());
                                                myClass.execute();

XYZ.class XYZ.class

th_tbusergroupmapping is my Database Name th_tbusergroupmapping 是我的数据库名称

 /*CLient for Table5 */
    private static MobileServiceClient mClient_UserGroupMapping;

    //ONLINE CLIENT  AZURE
    public  static MobileServiceTable<th_tbusergroupmapping> mToDoTable_UserGroupMapping_ServerAzure;

    //Offline CLient FOr  LOCAL Datbase.

    public static MobileServiceSyncTable<th_tbusergroupmapping> mToDoTable_UserGroupMapping_Local_Database;


     @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            try {
                // Create the Mobile Service Client instance, using the provided
    
                // Mobile Service URL and key
                mClient_UserGroupMapping = new MobileServiceClient(
                        "***********",
                        "**************",
                        this).withFilter(new ProgressFilter());
    
    
    /*UserGroupMapping*/
                // Get the Mobile Service Table instance to use
                mToDoTable_UserGroupMapping_ServerAzure = mClient_UserGroupMapping.getTable(th_tbusergroupmapping.class);
    
                // LOCAL DATABASE TABLE Instance to use
                mToDoTable_UserGroupMapping_Local_Database= mClient_UserGroupMapping.getSyncTable("th_tbusergroupmapping", th_tbusergroupmapping.class);
    
    
                //Init local storage
                initLocalStore().get();
    
    
            }
            catch (MalformedURLException e)
            {
    
                Log.i("Oncreate", "There was an error creating the Mobile Service. Verify the URL......!");
    
            } catch (Exception e) {
    
                Log.i("Oncreate", "Exception Occur......!");
            }
    
        }
    
    
     public static class AsyncTaskload_UserGroupMappingTableClass extends AsyncTask<Void, String, Void>
        {
    
            private Context context;
            public AsyncTaskload_UserGroupMappingTableClass(Context context) {
                this.context = context;
            }
    
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
    
            }
            @Override
            protected Void doInBackground(Void... params)
            {
            }
     @Override
            protected void onPostExecute(Void result)
            {
    
                try
                {
    
                    Log.i("DONE ", "Data Sync Done Successfully UserGroupMapping 1");
    
    
                } catch (Exception e)
                {
                    e.printStackTrace();
    
                    Log.i("Exception ", "Post Excecute");
                }
            }
   
        }

Do not keep AsyncTask in an activity then;然后不要将 AsyncTask 保留在活动中; just keep it in an ordinary Java class and do not try to keep the data inside the instances of that class;只需将它保存在一个普通的 Java 类中,不要试图将数据保存在该类的实例中; store your information elsewhere: in a singleton class, or in sharedpreferences, or sqlite database, etc.将您的信息存储在其他地方:在单例类中,或在共享首选项中,或在 sqlite 数据库等中。

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

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