[英]How to start a new activity from a fragment calss by clicking on Linearlayout within fragment? below is my code but this is not working
当我点击片段中的linearlayout时,它不会给出任何错误,但不会启动新的活动。 我的代码中有错误吗? 请有人帮助我。 这个片段是我项目的xml文件代码。
Fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
>
<LinearLayout
android:id="@+id/Lbid"
android:layout_width="wrap_content"
android:layout_marginTop="30dp"
android:layout_height="wrap_content">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/pbb"
android:layout_marginLeft="15dp"
android:layout_width="180dp"
android:layout_height="25dp"
android:progress="50"
android:progressDrawable="@drawable/bidonpb"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bid on"
android:id="@+id/tv"
android:layout_marginLeft="2dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/Lap"
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/pbap"
android:layout_marginLeft="15dp"
android:layout_width="180dp"
android:layout_height="25dp"
android:progress="60"
android:progressDrawable="@drawable/appb"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awaiting Payments"
android:layout_marginLeft="2dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/LO"
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/pbo"
android:layout_marginLeft="15dp"
android:layout_width="180dp"
android:layout_height="25dp"
android:progress="20"
android:progressDrawable="@drawable/opb"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overdue"
android:layout_marginLeft="2dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/LA"
android:layout_height="wrap_content">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/pba"
android:layout_marginLeft="15dp"
android:layout_width="180dp"
android:layout_height="25dp"
android:progress="45"
android:progressDrawable="@drawable/assignedpb"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Assigned"
android:layout_marginLeft="2dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这个片段是我项目的java类代码。
FragmentOne.java
info.androidhive.listviewfeed;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
LinearLayout lbid =(LinearLayout) view.findViewById(R.id.Lbid);
lbid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ProjectDetails.class);
startActivity(intent);
}
});
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
你的onCreateView()
方法有问题。 你回来了
return inflater.inflate(
R.layout.fragment_one, container, false);
这会在没有您添加的侦听器的情况下生成新视图。 你应该做
return view;
因为视图是包含您添加的侦听器的布局的视图。
您应该根据自己的意图使用Intent.FLAG_ACTIVITY_NEW_TASK。
//喜欢这个intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.