Android:从TabHost启动新活动会禁用onClick

问题描述

|| 我有一个带有4个标签的TabActivity。当单击选项卡中一个选项卡上的按钮并启动新的Activity(不在TabHost内的新Activity)时,新Activity不注册OnClick()。新的Activity甚至无法显示Toast,这让我认为TabHost某种程度上阻止了ui? 将“活动”作为选项卡之一时,OnClick可以正常工作。 任何想法是什么原因? 我提供了3个课程: 1)TabActivity 2)开始的标签中的活动: 3)无法注册OnClick()的新活动 1)TabActivity:
public class OverView extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overview);

        /** TabHost will have Tabs */
        TabHost tabHost     = (TabHost)findViewById(android.R.id.tabhost);

        /** TabSpec used to create a new tab.
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */

        /** tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec Search      = tabHost.newTabSpec(\"tid1\");
        TabSpec AllArtists  = tabHost.newTabSpec(\"tid1\");
        TabSpec Favorites   = tabHost.newTabSpec(\"tid1\");
        TabSpec About       = tabHost.newTabSpec(\"tid1\");

        /** TabSpec setIndicator() is used to set name for the tab. */
        Search.     setIndicator(\"Search\");
        AllArtists. setIndicator(\"AllArtists\");
        Favorites.  setIndicator(\"Favorites\");
        About.      setIndicator(\"About\");

        /** TabSpec setContent() is used to set content for a particular tab. */
        Search.setContent       (new Intent(this,Search.class));
        AllArtists.setContent   (new Intent(this,AllArtists.class));
        Favorites.setContent    (new Intent(this,Favorites.class));
        About.setContent        (new Intent(this,About.class));

        /** Add tabSpec to the TabHost to display. */
        tabHost.addTab(Search);
        tabHost.addTab(AllArtists);
        tabHost.addTab(Favorites);
        tabHost.addTab(About);
    }
}
2)AllArtists(选项卡中的“活动”。单击列表项将启动新的活动):
public class AllArtists extends Activity {

    // Debug
    private final String    TAG = this.getClass().getSimpleName();  

    // XML
    EditText                searchBox;
    ListView                listView;

    // Adapter
    ListAdapter             listAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allartists);

        listAdapter = new ListAdapter(this,null);

        // XML
        listView    = (ListView)findViewById(R.id.allartists_listview);
        searchBox   = (EditText)findViewById(R.id.allartists_searchbox);

        listView.setAdapter(listAdapter);
        listView.setFastScrollEnabled(true);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id) {

                String memberID = (String)listAdapter.getID(position).toString();

                if (!memberID.equals(\"HEADER\")){
                    Log.d(TAG,\"Jumping to Artists.class\");
                    Intent intentArtist = new Intent (AllArtists.this,Artist.class);
                    intentArtist.putExtra(\"ID\",memberID);
                    startActivity(intentArtist);
                }
            }
        });
}
3)艺术家(新的活动已开始。此类未注册OnClick):
public class Artist extends Activity implements OnClickListener{

// Debug
private final String    TAG = this.getClass().getSimpleName();  

// XML
Button          favorite_btn;

LinearLayout    tel;
LinearLayout    mob;
LinearLayout    email;
LinearLayout    www1;
LinearLayout    www2;
LinearLayout    add;

TextView        name_tv;
TextView        tel_tv;
TextView        mob_tv;
TextView        email_tv;
TextView        www_tv1;
TextView        www_tv2;

// Strings
String memberID;
String sirName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_artist);

    Toast.makeText(this,\"OK,your in Activity_Artist..\",Toast.LENGTH_SHORT);


    // XML
    favorite_btn    = (Button)findViewById(R.id.artist_ib_favorite);

    tel             = (LinearLayout)findViewById(R.id.artist_tel_container);
    mob             = (LinearLayout)findViewById(R.id.artist_mob_container);
    email           = (LinearLayout)findViewById(R.id.artist_email_container);
    www1            = (LinearLayout)findViewById(R.id.artist_www_container1);
    www2            = (LinearLayout)findViewById(R.id.artist_www_container2);
    add             = (LinearLayout)findViewById(R.id.artist_add_container);

    name_tv         = (TextView)findViewById(R.id.artist_tv_name);
    tel_tv          = (TextView)findViewById(R.id.artist_tel_dynamic);
    mob_tv          = (TextView)findViewById(R.id.artist_mob_dynamic);
    email_tv        = (TextView)findViewById(R.id.artist_email_dynamic);
    www_tv1         = (TextView)findViewById(R.id.artist_www_dynamic1);
    www_tv2         = (TextView)findViewById(R.id.artist_www_dynamic2);

    // OnClickListeners
    favorite_btn.setOnClickListener(this);
    tel.setOnClickListener(this);
    mob.setOnClickListener(this);
    email.setOnClickListener(this);
    www1.setOnClickListener(this);
    www2.setOnClickListener(this);
    add.setOnClickListener(this);

        // Code here to get memberID for fillContent()
}

private void fillContent(String memberID) throws JSONException {
// Code here to fill the TextViews etc with content from the DataBase.
}

@Override
public void onClick(View v) {

    switch (v.getId()){

    case R.id.artist_ib_favorite:
        Toast.makeText(this,\"onClick\",Toast.LENGTH_SHORT);
        Log.d(TAG,\"Neo is attempting to insert member into favorites\");

        MyDB db = new MyDB(this);
        db.insertFavorite(memberID,sirName);

        break;

    case R.id.artist_tel_container:
        break;

    case R.id.artist_mob_container:
        Log.d(TAG,\"OMG CLICKED THE MOBILE!\");
        break;

    case R.id.artist_email_container:
        break;

    case R.id.artist_www_container1:
        break;

    case R.id.artist_add_container:
        break;
    }
}
谢谢 ;)     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)