简体   繁体   中英

passing arrays another class and asynctask

Hi guys i have a problem with AsyncTask and strings arrays... I have a splashscreen

SplashScreen.java:

public class SplashScreen extends Activity {

private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 6000; //6 seconds
private Handler myhandler;

int thread;
public String[] lista = new String[200];
public String[] Compagnia = new String[200];
public String[] CodiceVolo = new String[200];
public String[] Citta = new String[200];
public String[] OraPrevista = new String[200];
public String[] OraStimata = new String[200];
public String[] StatoVolo = new String[200];

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.splashscreen);
    new MyTask().execute("");

    myhandler = new Handler();

    // run a thread to start the home screen
    myhandler.postDelayed(new Runnable()
    {
        @Override
        public void run() 
        {
           finish();

           if (!mIsBackButtonPressed)
           {
                // start the home activity 
            //    Intent intent = new Intent(SplashScreen.this, MainActivity.class);
              //  SplashScreen.this.startActivity(intent);
           }

        }

    }, SPLASH_DURATION); 
}


//handle back button press
@Override
public void onBackPressed() 
{
    mIsBackButtonPressed = true;
    super.onBackPressed();
}



public class MyTask extends AsyncTask<String, Void, String> {
    ProgressDialog prog;
    String info;

    @Override
    protected void onPreExecute() {
        prog = new ProgressDialog(SplashScreen.this);
        prog.setMessage("Connessione in corso...");
        prog.show();
        prog.setCanceledOnTouchOutside(false);
        prog.setCancelable(false);
        thread = 0;
    }

    @Override
    protected String doInBackground(String... params) {
        try {           
            org.jsoup.nodes.Document doc = Jsoup.connect("http://s.eu").timeout(7*1000).get();

            org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").first();
            Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();

            while(iterator.hasNext()){
                thread++;

                Compagnia[thread] = iterator.next().text();
                CodiceVolo[thread] = iterator.next().text();
                Citta[thread] = iterator.next().text();
                OraPrevista[thread] = iterator.next().text();
                OraStimata[thread] = iterator.next().text();
                StatoVolo[thread] = iterator.next().text();
                System.out.println("SPLASH: "+CodiceVolo[thread]);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }return info;
    }

    @Override
    protected void onPostExecute(String result) { 
        super.onPostExecute(result);
        prog.dismiss();

        Bundle bundle = new Bundle();
        bundle.putString("edttext", "From Activity");
        arrivi fragobj = new arrivi();
        fragobj.setArguments(bundle);

        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
       // intent.putExtra("key",CodiceVolo[2]);

        SplashScreen.this.startActivity(intent);


       // CheckRow();
    }
}// FINE THREAD 

I want to print my array in another class. in arrivi.java:

        public class arrivi extends Fragment {

ListView list;
List<RowItem> rowItems;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View arrivi = inflater.inflate(R.layout.arrivi, container, false); 
   String strtext = getArguments().getString("edttext");
   System.out.println("CODCICE VOLO: "+strtext);


   return arrivi;
}

This doesn't work for me... Crash my app... Thank you

SplashScreen is a Activity class. And doing this SplashScreen x = new SplashScreen(); is wrong.

Quoting Raghav Sood @

Can i Create the object of a activity in other class?

By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

Instead you can pass the value from SplashScreen to MainActivity using Intent .

Intent intent = new Intent(SplashScreen.this, MainActivity.class);
intent.putExtra("key",CodiceVolo[2])
SplashScreen.this.startActivity(intent);

In MainActivity

String value = getIntent().getStringExtra("key");

Edit:

arrivi fragobj = new arrivi();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
fragobj.setArguments(bundle)
transaction.replace(R.id.fragment_container, fragobj);
transaction.addToBackStack(null);
transaction.commit();

Passing data object from one Activity to another is simple, If you want to pass Array object than the object should be serialized. Eg;

ArrayList array = new ArrayList();

for Passing array object you have to use intent PutExtra, Eg:

Intent intent = new Intent(SplashScreen.this, MainActivity.class);

intent.putExtra("key",array); startActivity(intent);

//intent.putExtra("key",array); will show error if your Model class is not seriallized eg: public class Model implements Seriallizable{

String name;
String address;
//generate your getter setter and set data in to this.

}

//For getting data in to another class just use

ArrayList data = (ArrayList)getIntent().getSerializable("key");

Now you can play arround with this data object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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