如何在 Fragment 中实现一个按钮并让它打开一个新的 Activity?

How to implement a button inside a Fragment and have it open a new Activity?

我是编程和 android 开发人员的新手,我正在尝试在 Fragment 中实现一个按钮,它将打开一个新的 Activity。

我收到错误:

Cannot resolve method 'findViewById(int)' I am also getting the error: Cannot resolve consutructor 'Intent(com.hashmi.omar.store.Ip6_Cab, java.lang.Class)'

下面是Ip6_Cab.java:

package com.hashmi.omar.vodafonestore;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by User on 29/06/2015.
 */
public class Ip6_Cab extends Fragment implements View.OnClickListener {

Button button_ip6_24m_b2;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.ip6_cab,container,false);
    return v;

    //Sets up iphone 6 button
    button_ip6_24m_b2 = (Button) findViewById(R.id.button_ip6_24m_b2);
    button_ip6_24m_b2.setOnClickListener(this);

}



private void title_ip6_24m_b2_payment() {
    startActivity(new Intent(Ip6_Cab.this, Ip6_24m_b2_payment.class));
}


public void onClick(View v) {
    switch (v.getId()){

        case R.id.button_ip6_24m_b2:
            title_ip6_24m_b2_payment();
            break;
    }
  }
}

下面是 xml 布局,我希望从中打开 Activity 的按钮是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="1dp"
android:paddingRight="1dp"
tools:context="com.hashmi.omar.store.Picker"
android:background="#ffffffff"
android:weightSum="1">



<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

/Image, Bundle and Button 1

        <ImageView
            android:layout_width="360dp "
            android:layout_height="267dp"
            android:id="@+id/imageView"
            android:background="@drawable/ip6_24m_b1"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"/>

        <Button
            android:layout_width="54dp"
            android:layout_height="28dp"
            android:id="@+id/button_ip6_24m_b1"
            android:background="@drawable/button_select_for_bundles"
            android:layout_marginTop="5dp"
            android:layout_gravity="bottom|center_horizontal" />

/Image, Bundle and Button 2

        <ImageView
            android:layout_width="360dp "
            android:layout_height="425dp"
            android:id="@+id/imageView2"
            android:background="@drawable/ip6_24m_b2"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"/>

        <Button
            android:layout_width="54dp"
            android:layout_height="28dp"
            android:id="@+id/button_ip6_24m_b2"
            android:background="@drawable/button_select_for_bundles"
            android:layout_marginTop="5dp"
            android:layout_gravity="bottom|center_horizontal" />


    </LinearLayout>
</ScrollView>

有人能帮忙吗?

您必须在布局中找到视图。在 Fragments 中,您需要在 findViewById.

之前添加您的视图

此外,如果您调用return,方法会在此处结束。您必须在方法结束时调用它...

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.ip6_cab,container,false);

    //Sets up iphone 6 button
    button_ip6_24m_b2 = (Button) v.findViewById(R.id.button_ip6_24m_b2);

    button_ip6_24m_b2.setOnClickListener(this);
    return v;    
}

关于开始新的 Activity,您必须:

getActivity().startActivity(new Intent(getActivity(), Ip6_24m_b2_payment.class));

改变 startActivity(new Intent(Ip6_Cab.this, Ip6_24m_b2_payment.class));getActivity().startActivity(new Intent(getActivity(), Ip6_24m_b2_payment.class));

这是因为startActivity方法只存在于一个Context中,而一个Fragment并没有扩展Context。所以你需要使用父activity来启动新的activity。同样的原因 Intent class 的第一个参数必须是 getActivity(),应该是一个上下文。

您可能想将 button_ip6_24m_b2 = (Button) findViewById(R.id.button_ip6_24m_b2); 更改为 button_ip6_24m_b2 = (Button) v.findViewById(R.id.button_ip6_24m_b2);

您想在刚刚膨胀的视图中找到视图。