【发布时间】: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