android,从异步任务中运行的方法类创建/更新视图

时间:2012-05-29 09:44:35

标签: android android-layout

好吧,我有一些android的问题。也许它很简单但我在Android世界中很新(实际上是3天,4天)。好吧,我有一个活动whitch运行asyncTask,它代表从传感器网络读取数据。异步任务只执行函数l.read(),它本身就是一个循环,并始终“传播”传感器网络,当新节点加入传感器网络时,它告诉UI线程还有一个节点已加入,所以它必须更新视图(巫婆只有一个标签说没有节点存在,现在必须有一个节点可用 - 除此之外我甚至必须加载这个选项卡的xml布局文件)问题是,当我预先形成了基本上创建视图并添加选项卡的功能(这是正常工作,因为当我在onCreate上调用它时它工作)应用程序不再响应,但是,Logcat没有给我任何错误。看起来它有效,但它没有显示任何东西。

拜托,我不希望你阅读所有这些代码,只是为了解释我的问题。已经2天了,我还在寻求解决方案..

主要活动:

public class PrincipalActivity extends Activity implements OnClickListener
{

private Button Send;
private Button Options;
private TextView ERROR;
private Bundle extras;
private String IP;
private String PORT;
private TabHost tabs;
private ligação l;
View m_vForm;
TabHost tabHost;
CustomView Cview;
ReadsData read;



//constructor
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    CustomView vv = new CustomView(PrincipalActivity.this,0);
    setContentView(vv);

    //instanciating the objects
    IP = new String();
    PORT = new String();      
    extras = getIntent().getExtras();
    IP = extras.getString("IP");  //get the IP and port from the previous activity
    PORT = extras.getString("PORT");
    ERROR = (TextView) findViewById(R.id.ERROR);   
    tabs = (TabHost) findViewById(R.id.tabhost);
    tabs.setup();
    Send = (Button) findViewById(R.id.SendCommand);
    Send.setOnClickListener(this);
    Options = (Button) findViewById(R.id.Options);
    Options.setOnClickListener(this);
  //=========================

    CreateAView("No Nodes",false);
    l = new ligação(IP, Integer.parseInt(PORT),this);  //CLASS LIGAÇÃO
   // RunnableThread rT1 = new RunnableThread("t1",l,this);

    read = new ReadsData();
    read.execute();

    //Textviews (Values)


}

public void AddUpdateNodes(Nodes n,int func)
{
    //func is 0 if it it to add a node or 1 if it is to update an existing one
    if(func == 0)
    {
        String s = new String();
        s+= n.GETSourceNodeID();
        CreateAView(s, true);
        Cview.addNewNode(n);        
    }
    if(func == 1)
        Cview.UpdateNodeInformation(n);
}


public void onClick(View v) 
{

    if(v == Send)
    {
        // if i call CreateAView here it works ok
        ERROR.setText(IP);

    }

    if(v == Options)
    {

        ERROR.setText(PORT);
    }
}

    // CREATING A VIEW WITH THE TABS
public void CreateAView(String s,boolean nodesAvailable)
{

     m_vForm = createTABForm(s,nodesAvailable);
     setContentView(m_vForm);
}

 private ViewGroup createTABForm(String s,boolean nodesAvailable1)
 {
    final boolean nodesAvailable = nodesAvailable1;

        // construct the TAB Host
        tabHost = new TabHost(this);
        tabHost.setLayoutParams(  new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)  );

        // the tabhost needs a tabwidget, that is a container for the visible tabs
        TabWidget tabWidget = new TabWidget(this);
        tabWidget.setId(android.R.id.tabs);
        tabWidget.setBackgroundColor(Color.BLUE);

        tabHost.addView(tabWidget, new LinearLayout.LayoutParams(      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)   ); 

        // the tabhost needs a frame layout for the views associated with each visible tab
        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setId(android.R.id.tabcontent);
        frameLayout.setPadding(0, 65, 0, 0);
        tabHost.addView(frameLayout, new LinearLayout.LayoutParams(   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)   ); 

        // setup must be called if you are not initialising the tabhost from XML
        tabHost.setup(); 

        // create the tabs
        TabSpec ts = tabHost.newTabSpec("TAB_TAG_2");
        ts.setIndicator(s); // creating a tabb with the specified name
        ts.setContent(new TabHost.TabContentFactory(){
             public View createTabContent(String tag)
             {
                 // -- this tab contains a single control - the listview -- //
                 Cview = new CustomView(PrincipalActivity.this,nodesAvailable);
                 return Cview;   // IS AN OBJECT FROM THE CLASS CustomView
             }
        });

        tabHost.addTab(ts); 
        return tabHost;
    } 




 ////////////////////////////////////////////////////////////////
 //Async Tasks
 private class ReadsData extends AsyncTask 
 {

    @Override
    protected Object doInBackground(Object... params) 
    {
        l.read();
        return null;
    }

    protected void onPostExecute()
     {
        l.SocketClosed_();
     }
 }

}

    //CLASS CUSTOM VIEW

 public CustomView(Context context,boolean NodesAvailable)
 {
   super(context);

   if(NodesAvailable) //loads a layout
   {
       LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       View view=layoutInflater.inflate(R.layout.finallayout,this);

       //only after inflate
        ADC0 = (TextView) findViewById(R.id.ADC00);

   }
   else  // loads another layout
   {
       LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View view=layoutInflater.inflate(R.layout.no_nodes,this);
   }

 }



 public void addNewNode(Nodes n)
 {
     String s = new String();


     s = new String();
     s += n.GETSensores(0).GETvalor();
     ADC0.setText(s);


 }

 public void UpdateNodeInformation(Nodes n)
 {
     String s = new String();
     s += n.GETSourceNodeID();

     //if(s.equals())

 }

}

    //SOME CODE OF LIGAÇÃO CLASS
  public class ligação 
  {
      protected PrincipalActivity cont;
    //Constructors
    public ligação(String ss,int Port,PrincipalActivity c){ s=ss; p=Port;cont = c; }

       public void read() 
   {
           while(flag)
           {
              ...
              cont.AddUpdateNodes(node, 0);
              ...
           }

非常感谢提前

1 个答案:

答案 0 :(得分:0)

你的应用程序没有崩溃是很奇怪的,因为所有的UI操作都应该在UI线程上完成。也许是因为asynctask的线程崩溃但不是ui线程,这就是为什么没有发生的原因。 无论如何,为了在asyncTask中创建/更新视图,你可以使用以下任何一个,它会尽快发布到UI线程的东西:

  1. 使用处理程序。
  2. 使用publishProgress
  3. 使用runOnUiThread