簡體   English   中英

Android Studio CardView onClick java.lang.NullPointerException

[英]Android Studio CardView onClick java.lang.NullPointerException

我正在嘗試為網格布局中的每個卡片視圖添加一個點擊偵聽器,但出現以下錯誤。

java.lang.RuntimeException: 無法啟動活動 ComponentInfo{com.example.uspherejda/com.example.uspherejda.HomeScreen}: java.lang.NullPointerException: 嘗試調用虛擬方法 'void androidx.cardview.widget.CardView.setOnClickListener( android.view.View$OnClickListener)' 在一個空對象引用上

引起:java.lang.NullPointerException:嘗試在空對象引用上調用虛方法“void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)”

public class HomeScreen extends AppCompatActivity implements View.OnClickListener{
    //SQL
    private SatFromHelper dbHelper;
    private SQLiteDatabase db;

    private CardView satList, addSat, satType, satCountries, aboutMe, goCrazy;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);

        dbHelper = new SatFromHelper(this);
        db = dbHelper.getWritableDatabase();
        //call the Home fragment
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
        //Set up a custom bar using a custom
        ActionBar actionBar = getSupportActionBar();
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.start_dark_blue)));
        actionBar.setDisplayShowCustomEnabled(true);
        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.custom_bar_image, null);
        actionBar.setCustomView(view);
        //Card views.
        satList = (CardView) findViewById(R.id.crdList);
        addSat = (CardView) findViewById(R.id.crdForm);
        satType = (CardView) findViewById(R.id.crdSatTypes);
        satCountries = (CardView) findViewById(R.id.crdCountries);
        aboutMe = (CardView) findViewById(R.id.crdAboutMe);
        goCrazy = (CardView) findViewById(R.id.crdGoCrayCraaaaay);

         satList.setOnClickListener(this);
         addSat.setOnClickListener(this);
         satType.setOnClickListener(this);
         satCountries.setOnClickListener(this);
         aboutMe.setOnClickListener(this);
         goCrazy.setOnClickListener(this);

        //Bottom Navigation
        BottomNavigationView bottomNav = findViewById(R.id.nav_menu);
        //Item listener when one of hte items below from the nav_bar is selected
        bottomNav.setOnItemSelectedListener(item -> {
            Fragment selectedFragment = null;
            switch(item.getItemId()){
                case R.id.nav_home:
                    selectedFragment = new HomeFragment();
                    break;
                case R.id.nav_list:
                    selectedFragment = new ListFragment(dbHelper, db);
                    break;
                case R.id.nav_add:
                    selectedFragment = new AddFragment(dbHelper, db);
                    break;
            }
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
            return true;
        });
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.crdList: startActivity(new Intent(getApplicationContext(), ListFragment.class));
            break;
        }

    }
    //Close the db when the activity onDestroy
    @Override
    public void onDestroy() {
        super.onDestroy();
        dbHelper.close();
        db.close();
    }
}

XML 代碼:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="My Dashboard"
                    android:textSize="40sp"
                    android:textStyle="bold"
                    android:textColor="@color/white"
                    android:layout_margin="20dp" />
                <GridLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:rowCount="4"
                    android:columnCount="2"
                    android:layout_margin="5dp"
                    android:alignmentMode="alignMargins"
                    android:layout_gravity="center_horizontal"
                    android:columnOrderPreserved="false">
                        <androidx.cardview.widget.CardView
                                android:id="@+id/crdList"
                                android:layout_width="170dp"
                                android:layout_height="170dp"
                                app:cardCornerRadius="12dp"
                                app:cardElevation="5dp"
                                app:cardBackgroundColor="@color/center_light_blue"
                                android:layout_margin="10dp">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical"
                            android:gravity="center">
                                <ImageView
                                    android:layout_width="84dp"
                                    android:layout_height="84dp"
                                    android:src="@drawable/satdashboard"/>

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Satellite List"
                                    android:textSize="16sp"
                                    android:layout_marginTop="20dp"
                                    android:textColor="@color/white"/>
                        </LinearLayout>
                        </androidx.cardview.widget.CardView>

                        <androidx.cardview.widget.CardView
                            android:id="@+id/crdForm"
                            android:layout_width="170dp"
                            android:layout_height="170dp"
                            app:cardCornerRadius="12dp"
                            app:cardElevation="5dp"
                            app:cardBackgroundColor="@color/center_light_blue"
                            android:layout_margin="10dp">
                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="vertical"
                                    android:gravity="center">
                                        <ImageView
                                            android:layout_marginLeft="10dp"
                                            android:layout_width="84dp"
                                            android:layout_height="84dp"
                                            android:src="@drawable/formdashboard"/>

                                        <TextView
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:text="Add a Satellite"
                                            android:textSize="16sp"
                                            android:layout_marginTop="20dp"
                                            android:textColor="@color/white"/>
                                </LinearLayout>
                        </androidx.cardview.widget.CardView>

                        <androidx.cardview.widget.CardView
                            android:id="@+id/crdSatTypes"
                            android:layout_width="170dp"
                            android:layout_height="170dp"
                            app:cardCornerRadius="12dp"
                            app:cardElevation="5dp"
                            app:cardBackgroundColor="@color/center_light_blue"
                            android:layout_margin="10dp">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical"
                            android:gravity="center">
                                <ImageView
                                    android:layout_width="84dp"
                                    android:layout_height="84dp"
                                    android:src="@drawable/sattypes"/>

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Types of Satellites"
                                    android:textSize="16sp"
                                    android:layout_marginTop="20dp"
                                    android:textColor="@color/white"/>
                        </LinearLayout>

                </androidx.cardview.widget.CardView>

                        <androidx.cardview.widget.CardView
                            android:id="@+id/crdCountries"
                            android:layout_width="170dp"
                            android:layout_height="170dp"
                            android:layout_margin="10dp"
                            app:cardBackgroundColor="@color/center_light_blue"
                            app:cardCornerRadius="12dp"
                            app:cardElevation="5dp">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:gravity="center"
                                    android:orientation="vertical">

                                        <ImageView
                                            android:layout_width="84dp"
                                            android:layout_height="84dp"
                                            android:src="@drawable/countrysats" />

                                        <TextView
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:layout_marginTop="20dp"
                                            android:text="Satellite Countries"
                                            android:textColor="@color/white"
                                            android:textSize="16sp" />
                                </LinearLayout>

                        </androidx.cardview.widget.CardView>

                        <androidx.cardview.widget.CardView
                            android:id="@+id/crdAboutMe"
                            android:layout_width="170dp"
                            android:layout_height="170dp"
                            android:layout_margin="10dp"
                            app:cardBackgroundColor="@color/center_light_blue"
                            app:cardCornerRadius="12dp"
                            app:cardElevation="5dp">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:gravity="center"
                                    android:orientation="vertical">

                                        <ImageView
                                            android:layout_width="84dp"
                                            android:layout_height="84dp"
                                            android:src="@drawable/aboutme" />

                                        <TextView
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:layout_marginTop="20dp"
                                            android:text="About Me"
                                            android:textColor="@color/white"
                                            android:textSize="16sp" />
                                </LinearLayout>

                        </androidx.cardview.widget.CardView>

                        <androidx.cardview.widget.CardView
                            android:id="@+id/crdGoCrayCraaaaay"
                            android:layout_width="170dp"
                            android:layout_height="170dp"
                            android:layout_margin="10dp"
                            app:cardBackgroundColor="@color/center_light_blue"
                            app:cardCornerRadius="12dp"
                            app:cardElevation="5dp">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:gravity="center"
                                    android:orientation="vertical">

                                        <ImageView
                                            android:layout_width="84dp"
                                            android:layout_height="84dp"
                                            android:src="@drawable/gocrazy" />

                                        <TextView
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:layout_marginTop="20dp"
                                            android:text="Go Crazy!"
                                            android:textColor="@color/white"
                                            android:textSize="16sp" />
                                </LinearLayout>

                        </androidx.cardview.widget.CardView>

                </GridLayout>

        </LinearLayout>

</ScrollView>
  1. listfragment 是一個片段還是一個活動。

  2. 如果 listfragment 是一個片段,你不能像那樣調用片段。

我幾乎可以肯定您的 xml 引用中有什么錯誤:

tools:context=".HomeFragment"

應該是:

tools:context=".HomeScreen"

你確定這個 xml 屬於你的活動 HomeScreen?

java代碼屬於一個活動,它的xml屬於一個片段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM