在为我的CardView设置onclick列表器时获得Nullpointer异常。
错误是:
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.support.v7.widget.CardView.setOnClickListener(android.view.View $ OnClickListener)” 在com.jaysurya.project_scrollwithtap.MainActivity.onCreate(MainActivity.java:27)
这是我的 mainactivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
CardView chap1,chap2,chap3,chap4,chap5;
int chapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chap1 = findViewById(R.id.chap1);
chap2 = findViewById(R.id.chap2);
chap3 = findViewById(R.id.chap3);
chap4 = findViewById(R.id.chap4);
chap5 = findViewById(R.id.chap5);
chap1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chapter = 1;
openchapters(chapter);
}
});
chap2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chapter = 2;
openchapters(chapter);
}
});
chap3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chapter = 3;
openchapters(chapter);
}
});
chap4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chapter = 4;
openchapters(chapter);
}
});
chap5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chapter = 5;
openchapters(chapter);
}
});
}
private void openchapters(int chapter){
Intent intent = new Intent(this, Main1Activity.class);
intent.putExtra("chapter_no",chapter);
startActivity(intent);
}
}
这是我的 activity_main.xml
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:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:background="@drawable/jhyti"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="185dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:gravity="center_vertical"
android:src="@drawable/logo" />
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp">
<!--ROW 1 - -!>
<!- -CARD 1-->
<android.support.v7.widget.CardView
android:id="@+id/chap1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="@animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/p2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CHAPTER 1"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:text="CHAPTER 1"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</android.support.v7.widget.CardView>
<!--CARD 2-->
<android.support.v7.widget.CardView
android:id="@+id/chap2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="@animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/p4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CHAPTER 2"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:text="CHAPTER 2"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText"/>
</android.support.v7.widget.CardView>
<!--CARD 3-->
<android.support.v7.widget.CardView
android:id="@+id/chap3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="@animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/p5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHAPTER 3"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TextView
android:id="@+id/textView4"
android:layout_width="155dp"
android:layout_height="33dp"
android:text="CHAPTER 3"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText"/>
</android.support.v7.widget.CardView>
<!--CARD 4-->
<android.support.v7.widget.CardView
android:id="@+id/chap4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="@animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/p3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHAPTER 4"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TextView
android:id="@+id/textView5"
android:layout_width="155dp"
android:layout_height="wrap_content"
android:text="CHAPTER 4"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</android.support.v7.widget.CardView>
<!--CARD 5-->
<android.support.v7.widget.CardView
android:id="@+id/chap5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="@animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="191dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/p1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHAPTER 5"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:text="CHAPTER 5"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
对于java.lang.OutOfMemoryError
,您可以在清单文件中添加以下行:
android:largeHeap="true"
像这样:
<application
......
android:largeHeap="true"
......
</application>
有关大堆以及何时使用它的更多详细信息,请参考this答案。