我的按钮无法通过单击来进行下一个活动

时间:2019-09-07 20:22:34

标签: java android

我在项目中实现了底部导航栏。当我声明并初始化按钮,然后为它们设置onClickListener时。没有logcat中的任何错误,onclicklistener不能正常工作。我认为我无法理解其逻辑错误,请参阅代码并指导我。 (如果您认为该问题不符合社区标准,那么我很抱歉,因为我是Java的初学者)

我搜索了很多与此相关的问题,但对我没有任何帮助。

这是我的主要活动:

 public class HomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    //I added this if statement to keep the selected fragment when rotating the device
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new HomeFragment()).commit();
    }
   }

     private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_notifications:
                        selectedFragment = new NotificationFragment();
                        break;
                    case R.id.nav_search:
                        selectedFragment = new SearchFragment();
                        break;
                }

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();

                return true;
            }
        };
  }

这是我的第一个名为HomeFragment的片段:

public class HomeFragment extends Fragment implements View.OnClickListener {

Button btnFertilizers;


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

    btnFertilizers = (Button)v.findViewById(R.id.btnFertilizers);

    return inflater.inflate(R.layout.fragment_home, container, false);


}

@Override
public void onClick(View view) {
    btnFertilizers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(),Fertilizers.class);
            getActivity().startActivity(intent);
        }
    });
}

Logcat显示零错误。 我相信这是问题所在。这是因为click功能不适用于Fertilizers.class:

   @Override
public void onClick(View view) {
    btnFertilizers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(),Fertilizers.class);
            getActivity().startActivity(intent);
        }
    });
}

这是我的FragmentHome.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light">



<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    >

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

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="230dp"
                android:orientation="vertical"
                android:gravity="center"
                android:background="@drawable/agriculture_home">

                <ImageView
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:src="@drawable/e_agriculture_logo"
                    android:layout_gravity="center" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="E-Agriculture"
                    android:textColor="#ffffff"
                    android:layout_gravity="center"
                    android:textStyle="bold"
                    android:textSize="29sp"
                    android:layout_marginTop="10dp"
                    />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="215dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="21dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="0dp"
                    android:orientation="horizontal">

                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/fertilizers"
                                android:layout_width="144dp"
                                android:layout_height="113dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/fertilizers_png" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/fertilizers"
                                android:text="Fertilizers"
                                android:textSize="20sp"
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="0dp"
                                />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btnFertilizers"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/seeds"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/seed" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/seeds"
                                android:text="Seeds"
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:textSize="20sp"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"
                                />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btn_seeds"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="21dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="0dp"
                    android:orientation="horizontal">

                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/deseases"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/deseases" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/deseases"
                                android:text="Diseases"
                                android:textSize="20sp"
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"
                                />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btn_deseases"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/imageViewPesticides"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/pesticides" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/imageViewPesticides"
                                android:text="Pesticides"
                                android:textSize="20sp"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:layout_marginBottom="10dp"
                                />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btnPesticides"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="21dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="0dp"
                    android:orientation="horizontal">

                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/imageViewWeatherForecast"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/weatherforecast" />

                            <TextView
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/imageViewWeatherForecast"
                                android:text="Weather Forecast"
                                android:textSize="20sp"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"
                                />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btnWeatherForecast"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/imageViewArticles"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/video" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/imageViewArticles"
                                android:text="Videos"
                                android:textSize="20sp"
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"
                                />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btnArticles"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="21dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="0dp"
                    android:orientation="horizontal">

                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/imageViewFeedback"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/feedback" />

                            <TextView
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/imageViewFeedback"
                                android:text="Feedback"
                                android:textSize="20sp"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"
                                />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btnFeedback"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:layout_width="125dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:id="@+id/imageViewLocation"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_centerInParent="true"
                                android:src="@drawable/location_icon" />

                            <TextView
                                android:gravity="center"
                                android:textColor="@color/colorBlack"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                 android:layout_below="@+id/imageViewLocation"
                                android:text="Location"
                                android:textSize="20sp"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"
                                tools:ignore="NotSibling" />
                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/btnLocation"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>

                </LinearLayout>

            </LinearLayout>


        </RelativeLayout>

    </LinearLayout>

 </ScrollView>
  </RelativeLayout>

这是我的MainActivity.xml文件:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_navigation"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_navigation"
    app:itemIconTint="@color/colorWhite"
    app:itemTextColor="@color/colorWhite"
    android:background="@color/colorGreen"/>

 </RelativeLayout>

我应该如何设置onClickListener来访问我想要的活动?

2 个答案:

答案 0 :(得分:0)

您要在onclick方法中调用另一个onClick过滤器,因此它将无法正常工作。您应该像这样更改代码:

python -c "print 'a' * 1034 + 'the_address'" | /task2/vuln

在您的onCreateViewMethod中返回v。

@Override
public void onClick(View view) {

     if (view.getId() == R.id.btnFertilizers) {
       Intent intent = new Intent(getActivity(),Fertilizers.class);
       getActivity().startActivity(intent);

}

答案 1 :(得分:0)

将此代码移至onCreateView

btnFertilizers.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(),Fertilizers.class);
        getActivity().startActivity(intent);
    }
});

删除此

implements View.OnClickListener

删除此

@Override
public void onClick(View view) { }