-1

我是 Android 编码的新手,所以我希望有人可以帮助我.. 我发现了。有一个类似的问题,我如何从选项卡主机内的活动中找到选项卡主机内的视图的viewbyid? 但答案对我不起作用......我的应用程序包括一个 main.xml 和 tab1.xml 和 tab2.xml 每个都有一个 Activity 类这里的代码:

public class MainActivity extends TabActivity {  


/** Called when the activity is first created. */ 
Intent tab1, tab2;

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

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);  


    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");  
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2");  

    tab1 = new Intent(this, tab1Activity.class);
    tab2 = new Intent(this, tab2Activity.class);

    firstTabSpec.setIndicator("TAB1").setContent(tab1);  
    secondTabSpec.setIndicator("Tab2").setContent(tab2);  

    tabHost.addTab(firstTabSpec);  
    tabHost.addTab(secondTabSpec);  

}

这里是 tabHost 的 Activity

  public class tab1Activity extends Activity
  {

/** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.xml);

    EditText t = (EditText) findViewById(R.id.note);
    t.setText("Test"); // here it crashes!

}

 }

所以我的问题是如何使用 findViewById global?我应该有意图地工作吗?如果有人帮助我会很棒!:-)

4

1 回答 1

0

我认为您应该检查您在 contentView 中设置的布局。可能是你问题的根源。

 public class tab1Activity extends Activity
  {

/** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.xml); // <-------- your problem could be here

    EditText t = (EditText) findViewById(R.id.note);
    t.setText("Test"); // here it crashes!

}

 }
于 2012-08-21T01:23:32.023 回答