0

好吧,我在使用 android 时遇到了一些问题。也许它很简单,但我在 android 世界中很新(实际上是 3、4 天)。好吧,我有一个活动,它运行一个异步任务,可以从传感器网络读取数据。异步任务仅执行函数 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);
              ...
           }

提前谢谢了

4

1 回答 1

0

奇怪的是您的应用程序没有崩溃,因为所有 UI 操作都应该在 UI 线程上完成。也许是因为 asynctask 的线程已经崩溃,但 ui 线程没有崩溃,这就是为什么没有发生任何事情的原因。无论如何,为了在 asyncTask 中创建/更新视图,您可以使用以下任何一种方法,它会尽快将某些内容发布到 UI 线程:

  1. 使用处理程序。
  2. 使用发布进度
  3. 使用runOnUiThread
于 2012-05-29T12:09:45.913 回答