空对象引用不断发生-无效的android.widget.GridView.setAdapter(android.widget.ListAdapter)

时间:2019-12-28 22:29:11

标签: android pointers exception gridview null

这是我的主要

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.NullPointerException:尝试调用虚拟方法
  

上的空对象引用上的“ void android.widget.GridView.setAdapter(android.widget.ListAdapter)”   com.example.mycinema.MainActivity2.onCreate(MainActivity2.java:28)

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

1 个答案:

答案 0 :(得分:0)

CustomAdapter为null,您需要在调用django.contrib.auth.models之前先对其进行初始化:

from groups_manager.models import Group, GroupType, Member
from django.contrib.auth.models import Group as AGroup

def register_view(request):
    form = RegisterForm(request.POST or None)
    if form.is_valid():
        user = form.save()
        new_group_name = form.cleaned_data['new_group_name']
        new_group, created = Group.objects.update_or_create(name=new_group_name)
        user.groups.add(AGroup.objects.get(name=new_group_name))
        user.save()
        password = form.cleaned_data.get('password1')
        new_user = authenticate(username=user.username, password=password)
        login(request, new_user)
        return redirect('home')
    return render(request, 'accounts/form.html', {'form': form, 'title': 'Üye Ol'})