Android 如何调用TabHost中的活动?

Android 如何调用TabHost中的活动?,android,android-fragments,android-tabhost,fragment-tab-host,Android,Android Fragments,Android Tabhost,Fragment Tab Host,我有一个片段,在片段中有一个按钮,可以称为另一个活动 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragm

我有一个片段,在片段中有一个按钮,可以称为另一个活动

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

  // Inflate the layout for this fragment
  View view = inflater.inflate(R.layout.fragment_test_fragment, container, false);

  Button button = (Button) view.findViewById(R.id.button2);
  button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent i = new Intent(getActivity(), AnotherPage.class);
          startActivity(i);
        }
    });
现在这个很好用

但是,现在我想做同样的事情,但是在我在这个片段中创建的TabHost

那么如何在选项卡中调用
getActivity()

我试着做:

public class tab_two_graph extends AppCompatActivity {

  View v;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_two_graph);

     Intent intent = getParent().getIntent();
     v = intent.getParcelableExtra("view");

    Button b = (Button) findViewById(R.id.button2);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent i = new Intent(v.getActivity(), Alert_main_page.class);
            startActivity(i);
        }
    });

  }

但它只会抛出错误。

您需要使用当前活动调用它,而不是您的视图。因此,您可以使用以下命令调用它:

Button b = (Button) findViewById(R.id.button2);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent i = new Intent(tab_two_graph.this, Alert_main_page.class);
            startActivity(i);
        }
    });

请阅读。

您需要调用当前活动,而不是您的视图。因此,您可以使用以下命令调用它:

Button b = (Button) findViewById(R.id.button2);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent i = new Intent(tab_two_graph.this, Alert_main_page.class);
            startActivity(i);
        }
    });

请阅读。

公共类选项卡两个图形扩展“AppCompatActivity”公共类选项卡两个图形扩展“AppCompatActivity”试试这个

Intent i = new Intent(((<YourActivityName>) getActivity()), AnotherPage.class);
            startActivity(i);
Intent i=newintent((()getActivity()),另一个page.class);
星触觉(i);
试试这个

Intent i = new Intent(((<YourActivityName>) getActivity()), AnotherPage.class);
            startActivity(i);
Intent i=newintent((()getActivity()),另一个page.class);
星触觉(i);

只需在此处为答案添加更多信息

您不必通过一个活动来启动另一个活动。您需要传递一个上下文,可以使用
tab\u two\u图访问该上下文。这就是为什么以下方法有效:

startActivity(new Intent(tab_two_graph.this, Alert_main_page.class));

只是想在这里为答案添加更多信息

您不必通过一个活动来启动另一个活动。您需要传递一个上下文,可以使用
tab\u two\u图访问该上下文。这就是为什么以下方法有效:

startActivity(new Intent(tab_two_graph.this, Alert_main_page.class));

视图v;--试着做“公开决赛”。并给出错误内容(运行时或Сompiler不接受语法?)错误是什么?视图v;——试着做“公开决赛”。并给出错误内容(运行时或Сompiler不接受语法?)错误是什么?