gpt4 book ai didi

java - void android.widget.GridView.setAdapter(android.widget.ListAdapter)

转载 作者:行者123 更新时间:2023-12-01 18:05:14 25 4
gpt4 key购买 nike

LogCat

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
at ye.ansarallah.al_ansar.HomeActivity.onCreate(HomeActivity.java:37)

首页 Activity :

 public class HomeActivity extends AppCompatActivity {


Integer[] MyImageArray = {
R.drawable.w1, R.drawable.w2, R.drawable.w3
};
GridView MyGridView;
ImageView MyCurrentWallpaper;
Drawable MyDrawable;
WallpaperManager MyWallmanger;


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


MyGridView = findViewById(R.id.mygridview);
MyCurrentWallpaper = findViewById(R.id.myImageView);
MyGridView.setAdapter(new ImageAdapter(getApplicationContext()));
UpdateMyWallpaper();


}

private void UpdateMyWallpaper() {

MyWallmanger = WallpaperManager.getInstance(getApplicationContext());
MyDrawable = MyWallmanger.getDrawable();
MyCurrentWallpaper.setImageDrawable(MyDrawable);
}

public class ImageAdapter extends BaseAdapter {

Context myContext;

ImageAdapter(Context applicationContext) {

myContext = applicationContext;
}

@Override
public int getCount() {
return MyImageArray.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ImageView GridImageView;


if (view == null) {

GridImageView = new ImageView(myContext);
GridImageView.setLayoutParams(new GridView.LayoutParams(512, 512));
GridImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
GridImageView = (ImageView) view;

}
GridImageView.setImageResource(MyImageArray[i]);
GridImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
MyWallmanger.setResource(MyImageArray[i]);
} catch (IOException e) {
e.printStackTrace();
}

UpdateMyWallpaper();
}
});

return GridImageView;


}
}
}

activity_home.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeActivity">

<ImageView
android:id="@+id/myImageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="centerCrop"

android:src="@drawable/w1" />


<GridView
android:id="@+id/mygridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidth"
android:verticalSpacing="1dp" />

</LinearLayout>

最佳答案

MyGridView 为空。说明layout中没有id=R.id.mygridview的view。

可能是因为您使用了 setContentView(R.layout.activity_ma​​in),但为 Activity_home.XML 提供了源 xml。

关于java - void android.widget.GridView.setAdapter(android.widget.ListAdapter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60569197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com