空对象引用不断出现 - void android.widget.GridView.setAdapter(android.



这是我的主要

public class MainActivity2 extends AppCompatActivity {
GridView gridView;
CustomAdapter customAdapter;
String[] fruitNames = {"Blade Runner 2049","Black Mass","Joker","RiverDale"};
int[] fruitImages = {R.drawable.blader,R.drawable.blackm,R.drawable.joker,R.drawable.riverdale};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_data);
gridView = findViewById(R.id.gridview);
gridView.setAdapter(customAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Toast.makeText(getApplicationContext(),fruitNames[i],Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),NowShowingActivity.class);
intent.putExtra("name",fruitNames[i]);
intent.putExtra("image",fruitImages[i]);
startActivity(intent);
}
});
}
private class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return fruitImages.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = getLayoutInflater().inflate(R.layout.row_data, null);
//getting view in row_data
TextView name = view1.findViewById(R.id.fruits);
ImageView image = view1.findViewById(R.id.images);
name.setText(fruitNames[i]);
image.setImageResource(fruitImages[i]);
return view1;
}
}

}

这是它将要去的活动

public class NowShowingActivity extends AppCompatActivity {
TextView gridData;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nowshowing_grid);
gridData = findViewById(R.id.griddata);
imageView = findViewById(R.id.imageView);
Intent intent = getIntent();
String receivedName =  intent.getStringExtra("name");
int receivedImage = intent.getIntExtra("image",0);
gridData.setText(receivedName);
imageView.setImageResource(receivedImage);
//enable back Button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}

}

这是activity_main2

<androidx.coordinatorlayout.widget.CoordinatorLayout 
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=".MainActivity2">
<TextView
android:id="@+id/griddata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp"
android:text="Blade Runner 2049"
android:textColor="#000"
android:textSize="30dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/blader"
android:layout_centerHorizontal="true"
android:layout_below="@+id/griddata"
android:layout_marginTop="27dp" />
<include layout="@layout/nowshowing_grid" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是nowshowing_grid活动主文件

<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e5e5e5"
android:horizontalSpacing="1dp"
android:numColumns="2"
android:verticalSpacing="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

这就是row_data

<RelativeLayout
android:id="@+id/gridviewdata"
android:layout_width="190dp"
android:background="#fff"
android:layout_height="180dp">
<TextView
android:id="@+id/fruits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Blade Runner 2049"
android:textStyle="normal|italic"
android:textSize="25dp" />
<ImageView
android:id="@+id/images"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/blader" />
</RelativeLayout>

这是错误消息

由以下原因引起:java.lang.NullPointer异常:尝试调用虚拟方法
'void android.widget.GridView.setAdapter(android.widget.ListAdapter(' 在 com.example.mycinema.MainActivity2.onCreate(MainActivity2.java:28( 的

空对象引用上

当我点击按钮转到下一个活动以查看网格视图时,就会发生这种情况。

CustomAdapter 为空,您需要先初始化它,然后再调用setAdapter

customAdapter = new CustomAdapter();
gridView.setAdapter(customAdapter);

相关内容

  • 没有找到相关文章

最新更新