【问题标题】:How to implement a button inside a Fragment and have it open a new Activity?如何在 Fragment 中实现一个按钮并让它打开一个新的 Activity?
【发布时间】:2015-09-22 19:28:22
【问题描述】:

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

我收到错误:

无法解析方法“findViewById(int)”我也得到了 错误:无法解析构造函数 '意图(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;
    }
  }
}

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

<?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>

谁能帮忙?

【问题讨论】:

    标签: android button fragment


    【解决方案1】:

    您必须在布局中找到视图。在 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));
    

    【讨论】:

    • 它有效,谢谢!看起来我放错了 return v;你和 se_bastiaan 的答案都是正确的:D
    【解决方案2】:

    改变 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。因此,您需要使用父活动来启动新活动。同样的原因,Intent 类的第一个参数必须是 getActivity(),应该是 Context。

    您可能希望将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);

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 2020-02-19
      相关资源
      最近更新 更多