【问题标题】:How to connect fragment and layout如何连接片段和布局
【发布时间】:2020-06-16 04:21:55
【问题描述】:

我正在尝试在我的项目中设置点击方法,但是我无法将我的布局连接到给定的片段。当您按下 Instagram 的图标时,它应该会打开个人 Instagram,但它只会崩溃。

片段代码:

 public class SupportFragment extends Fragment {

    @Nullable
    ImageView vedoIg;
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_support,container,false);
        vedoIg = v.findViewById(R.id.vedo_ig);
        vedoIg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("http://instagram.com/v");
                Intent instagram = new Intent(Intent.ACTION_VIEW, uri);
                instagram.setPackage("com.instagram.android");
                try {
                    startActivity(instagram);
                }
                catch (Exception e){
                    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://instagram.com/_u/v")));
                }
            }
        });
        return inflater.inflate(R.layout.fragment_support,container,false);
    }

   }

布局文件夹:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/pozadina"
    tools:context=".SupportFragment">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="Contact us: "
    android:textColor="#ffffff"
    android:textSize="30dp"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="90dp"
    android:text="Vedo"
    android:textColor="#ffffff"
    android:textSize="30dp"
    android:textStyle="bold" />


<ImageView
    android:id="@+id/vedo_ig"
    android:layout_width="72dp"
    android:layout_height="48dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="150dp"
    app:srcCompat="@drawable/w_instagram"/>

任何提示都意味着世界。在此先感谢:D

【问题讨论】:

  • 覆盖 onViewCreated 并移动您的监听器代码。
  • return v 而不是再次膨胀。另外,堆栈跟踪是什么?
  • 谢谢@BenP。它奏效了,我只是设置了 return v. much love
  • 当应用程序崩溃时,您应该能够在 Android Studio 的 Logcat 选项卡中看到发生的日志。堆栈跟踪是该日志的错误部分(它将显示为红色)。 developer.android.com/studio/debug/stacktraces
  • @RedBassett 好的,谢谢,现在我会知道的 :)

标签: android android-studio android-fragments android-activity layout


【解决方案1】:

这段代码有问题:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_support,container,false);
    vedoIg = v.findViewById(R.id.vedo_ig);
    // ...
    return inflater.inflate(R.layout.fragment_support,container,false);
}

在第一行中,您正在扩充视图的一个副本...但是您 return 是同一视图的不同副本。这意味着用户看到的内容与您一直在搜索并设置点击侦听器的视图不匹配。

将最后一行更改为return v 以解决此问题。

【讨论】:

    【解决方案2】:

    请尝试https://www.instagram.com/v/你必须将http改为https

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 2015-08-13
      • 2011-01-13
      • 2010-11-16
      相关资源
      最近更新 更多