Android 通过Runnable启动新活动

Android 通过Runnable启动新活动,android,multithreading,android-activity,handler,runnable,Android,Multithreading,Android Activity,Handler,Runnable,我检查线程中的字符串是否为NULL,如果为NULL,则处理程序启动 启动新活动的Runnable 一切正常,但在显示新活动后,它会切换回调用活动,并崩溃 下面是一段代码片段 if(username==null) { dialogs.dismiss(); handlers.post(new MyRunable()); } 而Runnable是 public class MyRunable implements Runnable { public void run(){

我检查线程中的字符串是否为NULL,如果为NULL,则处理程序启动 启动新活动的Runnable

一切正常,但在显示新活动后,它会切换回调用活动,并崩溃

下面是一段代码片段

if(username==null)
{
    dialogs.dismiss();
    handlers.post(new MyRunable());
}
而Runnable是

public class MyRunable implements Runnable
{
    public void run(){

    Toast.makeText(ListActivity.this, "Your Connection is too slow",Toast.LENGTH_LONG).show();

    startActivity(new Intent(ListActivity.this,Anim.class));

    }
}
这是我的全部资料来源

    package com.cram;

    import java.io.File;
    import java.io.IOException;

    import javax.xml.parsers.ParserConfigurationException;

    import org.xml.sax.SAXException;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Typeface;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class BuddyListActivity extends Activity
    {

        String ss;
        private static ProgressDialog dialogs,dialog;
        private Thread downloadThreads;
        boolean results=false;
        Context ctx;
        String[]ddd;
        ListView lv1;
        String [] icons;
        BuddyDbAdapter adapter;
        NetworkInfo netinfo;
        Cursor cursor;
        String values;
        GetBuddy gb;
        BuddyDB db;
        String[] username;
        String[] firstname;
        String[] lastname;
        String[] avatar;
        String[] id;
        File[] iconss;
        Handler handlers;
        boolean ready=false;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.buddy);
            db=new BuddyDB(BuddyListActivity.this);
            Typeface font = Typeface.createFromAsset(getAssets(),"fonts/Fortheloveofhate.ttf");
    TextView btxt = (TextView) findViewById(R.id.textbuddy);
    btxt.setTypeface(font);
    ctx=getApplicationContext();    
    lv1=(ListView)findViewById(R.id.list22);
    netinfo=null;
    adapter=new BuddyDbAdapter(BuddyListActivity.this);
    netinfo=checkDataConnection(getApplicationContext());
    handlers = new Handler();
    adapter.open();

    if(netinfo!=null)
    {   
        downloadThreads = (Thread) getLastNonConfigurationInstance();
        if (downloadThreads != null && downloadThreads.isAlive()) {
        dialog = ProgressDialog.show(this, "Download", "downloading");
        }

        startdownload();

    }




    if(netinfo==null)
    { 

        cursor=adapter.showBuddy();         
        if (cursor==null||cursor.moveToFirst()==false) 
        {
             AlertDialog.Builder buddybox = new AlertDialog.Builder(this);
                buddybox.setMessage("You have No Buddies Yet douche!!");
    buddybox.setNeutralButton("Okay", new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        startActivity(new Intent(getBaseContext(),Anim.class));
                    }
                });
                buddybox.setCancelable(false);
             buddybox.show();
    }
        else
            {
            cursor.moveToFirst();   
            int j=0;
            ddd=new String[cursor.getCount()];
            icons=new String [cursor.getCount()];
                do
                {
            String firstName = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_FISTNAME));
            String lastname  = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_LASTNAME));
            String Avatar    = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.AVATAR));
            ddd[j]=firstName+" "+lastname;
    Log.d("Test", ddd[j]);
    icons[j]=Avatar;
    Log.d("Test", icons[j]);
    j++;
        }while(cursor.moveToNext());


        iconss= new File[icons.length];
        Log.d("Test",icons.length+"");
    for (int q = 0; q < icons.length; q++) {
        iconss[q] = new File(Download.ROOT +"/"+ icons[q]+".jpg");
    Log.d("Test", iconss[q].getAbsolutePath().toString());
    }
    //adapter.close();
                    lv1.setAdapter(new BuddyAdapter(BuddyListActivity.this,R.layout.list1,ddd,iconss));
                    onDestroy();
                }



            }
        }

    private void box() {
        // TODO Auto-generated method stub

    cursor=adapter.showBuddy(); 
    if (cursor==null||cursor.moveToFirst()==false) 
    {
        dialogs.dismiss();
         AlertDialog.Builder buddybox = new AlertDialog.Builder(this);
            buddybox.setMessage("You have No Buddies Yet ass!!");
    buddybox.setNeutralButton("Okay", new OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            startActivity(new Intent(getBaseContext(),Anim.class));
                        }
                    });
                    buddybox.setCancelable(false);
                 buddybox.show();

        }

        }

        private NetworkInfo checkDataConnection(Context applicationContext) {
        final ConnectivityManager connMgr = (ConnectivityManager)BuddyListActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo=null;
        final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if(wifi.isConnected()||mobile.isConnected())
        {networkinfo=connMgr.getActiveNetworkInfo();
         return networkinfo;
        }

        else
        {
            return null;
        }

    }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (adapter != null) {
                adapter.close();
            }

        }

        @Override
        protected void onStop()
        {
            super.onStop();
            finish();

        }
        private void startdownload() {
            dialogs = ProgressDialog.show(BuddyListActivity.this, "Contacting Our Servers", "Geting Your Buddies Avatar");
        downloadThreads = new MyThread();
        downloadThreads.start();

    }

    public class MyThread extends Thread {

        @Override
        public void run() {
            try {

                new Thread();
                GetBuddy tt=new GetBuddy();
                String xml=tt.get();
                if(xml==null)
                {   dialogs.dismiss();
                    handlers.post(new MyRunable());
                    ready=false;
                    //downloadThreads.suspend();

    }
    final Download cd = new Download();
    results = cd.downloadBitmap();
    if(results)
    {
    BuddyParse bp=new BuddyParse();

    try {

        username=bp.show(xml);
    //                  if(username==null)
    //                  {   dialogs.dismiss();
    //                      handlers.post(new MyRunable());
    //                      //downloadThreads.suspend();
    //                  }
        firstname=bp.firstname();
        lastname=bp.lastname();
        avatar=bp.avatar();
        id=bp.id();
        adapter.deleteAll();
        ready=true;
    } 
    catch (ParserConfigurationException e) 
    {
        e.printStackTrace();
    }
    catch (SAXException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    //  Log.d("len", username.length+"");

    for(int k=0; k<username.length;k++)
    { 
    adapter.insertBuddy(id[k], username[k], firstname[k], lastname[k], avatar[k]);
    Log.d("Test", id[k]+username[k]+firstname[k]+lastname[k]+avatar[k]+"");
    }
    box();
    cursor=adapter.showBuddy();
    cursor.moveToFirst();
    int i=0;
    ddd=new String[cursor.getCount()];
    icons=new String [cursor.getCount()];
        do
        {
    String firstName = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_FISTNAME));
    String lastname  = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_LASTNAME));
    String Avatar    = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.AVATAR));
    ddd[i]=firstName+" "+lastname;
    Log.d("Test", ddd[i]);
    icons[i]=Avatar;
    Log.d("Test",icons[i]);
    i++;
        }while(cursor.moveToNext());


     iconss= new File[avatar.length];
    for (int k = 0; k < avatar.length; k++) {
        iconss[k] = new File(Download.ROOT+"/"+avatar[k]+".jpg");

    Log.d("Test", iconss[k].getAbsolutePath()+"thread");
    //Log.d("Test", ddd[k]);
            }

        if (results&&ready)
            {
            dialogs.dismiss();
            handlers.post(new MyRuns());

            }
    } 
    //              else
    //              {   dialogs.dismiss();
    //                  handlers.post(new MyRunable());
    //                  
    //              }
            }finally {

            }

        }


    }

    public class MyRuns implements Runnable {
        public void run() {
            ready=true;
            lv1.setAdapter(new BuddyAdapter(ctx,R.layout.list1,ddd,iconss));
            onDestroy();
        }
    }
    public class MyRunable implements Runnable {
        public void run() {
            //Toast.makeText(BuddyListActivity.this, "Your Connection is too slow", Toast.LENGTH_LONG).show();
                startActivity(new Intent(BuddyListActivity.this,Anim.class));


            }
        }
    }
package com.cram;
导入java.io.File;
导入java.io.IOException;
导入javax.xml.parsers.parserConfiguration异常;
导入org.xml.sax.SAXException;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.DialogInterface.OnClickListener;
导入android.content.Intent;
导入android.database.Cursor;
导入android.graphics.Typeface;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.Bundle;
导入android.os.Handler;
导入android.util.Log;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类BuddyStactivity扩展了活动
{
字符串ss;
私有静态进程对话框,对话框;
私有线程下载线程;
布尔结果=假;
上下文ctx;
字符串[]ddd;
ListView lv1;
字符串[]图标;
BuddyDbAdapter适配器;
网络信息网络信息;
光标;
字符串值;
GetBuddy gb;
BuddyDB-db;
字符串[]用户名;
字符串[]firstname;
字符串[]lastname;
字符串[]化身;
字符串[]id;
文件[]iconss;
处理者;
布尔就绪=假;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.buddy);
db=新BuddyDB(BuddyStactivity.this);
Typeface font=Typeface.createFromAsset(getAssets(),“font/Fortheloveofhate.ttf”);
TextView btxt=(TextView)findViewById(R.id.textbuddy);
btxt.setTypeface(字体);
ctx=getApplicationContext();
lv1=(ListView)findViewById(R.id.list22);
netinfo=null;
适配器=新BuddyBadapter(buddyStactivity.this);
netinfo=checkDataConnection(getApplicationContext());
handlers=newhandler();
adapter.open();
如果(netinfo!=null)
{   
downloadThreads=(线程)getLastNonConfigurationInstance();
if(downloadThreads!=null&&downloadThreads.isAlive(){
dialog=ProgressDialog.show(这是“下载”、“下载”);
}
startdownload();
}
如果(netinfo==null)
{ 
cursor=adapter.showBuddy();
if(cursor==null | | cursor.moveToFirst()==false)
{
AlertDialog.Builder buddybox=新建AlertDialog.Builder(此);
setMessage(“你还没有朋友灌洗!!”;
setNeutralButton(“好的”,新的OnClickListener(){
public void onClick(DialogInterface dialog,int which){
startActivity(新意图(getBaseContext(),Anim.class));
}
});
buddybox.setCancelable(false);
buddybox.show();
}
其他的
{
cursor.moveToFirst();
int j=0;
ddd=新字符串[cursor.getCount()];
icons=新字符串[cursor.getCount()];
做
{
String firstName=cursor.getString(cursor.getColumnIndex(buddyBadapter.BUDDY_FISTNAME));
String lastname=cursor.getString(cursor.getColumnIndex(buddyBadapter.BUDDY_lastname));
String Avatar=cursor.getString(cursor.getColumnIndex(buddyBadapter.Avatar));
ddd[j]=firstName+“”+lastname;
Log.d(“试验”,ddd[j]);
图标[j]=化身;
Log.d(“测试”,图标[j]);
j++;
}while(cursor.moveToNext());
iconss=新文件[icons.length];
Log.d(“Test”,icons.length+”);
对于(int q=0;qprivate final class MyRunnable extends
            AsyncTask<Void, Void, Document> {
        protected Document doInBackground(Void... params) {

        }

        protected void onPostExecute(Document result) {

        }
    }