传递数组另一个类和asynctask

时间:2014-04-19 17:12:49

标签: java android

大家好我有AsyncTask和字符串数组的问题... 我有一个闪屏

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 

我想在另一个类中打印我的数组。 在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;
}

这对我不起作用...... 崩溃我的应用程序... 谢谢

2 个答案:

答案 0 :(得分:0)

SplashScreen是一个Activity类。这样做SplashScreen x = new SplashScreen();是错误的。

引用Raghav Sood @

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

  

通过将Activity视为普通的Java类,最终得到null   上下文。因为Activity中的大多数方法都是在其Context上调用的,所以   将获得一个空指针异常,这就是你的应用程序崩溃的原因。

相反,您可以使用SplashScreen将值从MainActivity传递到Intent

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

在MainActivity中

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

编辑:

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();

答案 1 :(得分:0)

将数据对象从一个Activity传递到另一个Activity很简单,如果要传递Array对象,则应该序列化对象。例如;

ArrayList array = new ArrayList();

对于传递数组对象,你必须使用intent PutExtra,例如:

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

intent.putExtra(&#34;键&#34;,数组); startActivity(意向);

// intent.putExtra(&#34;键&#34;,数组);如果您的Model类没有连续化,将显示错误 例如: public class Model实现了Seriallizable {

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

}

//要将数据输入另一个类,只需使用

ArrayList data =(ArrayList)getIntent()。getSerializable(&#34; key&#34;);

现在你可以使用这个数据对象玩arround了。