gpt4 book ai didi

android - Intent 在我的 RecyclerView 中不起作用

转载 作者:行者123 更新时间:2023-11-29 20:48:40 25 4
gpt4 key购买 nike

我使用这段代码:

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

public MyAdapter (Context context, ArrayList<PersonData>peopleDataSet) {
this.context = context;
this.peopleDataSet = peopleDataSet;
}

Context context;

private ArrayList<PersonData> peopleDataSet;

public static class MyViewHolder extends RecyclerView.ViewHolder {



TextView textViewName;
TextView textViewEmail;
ImageView imageViewIcon;
Button button;

public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
this.textViewEmail = (TextView) itemView.findViewById(R.id.textViewEmail);
this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView);
this.button = (Button) itemView.findViewById(R.id.button);
}
}

public MyAdapter(ArrayList<PersonData> people) {
this.peopleDataSet = people;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_layout, parent, false);

view.setOnClickListener(MainActivity.myOnClickListener);

MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

TextView textViewName = holder.textViewName;
final TextView textViewEmail = holder.textViewEmail;
ImageView imageView = holder.imageViewIcon;
Button button = holder.button;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(context, MainActivity2Activity.class);
context.startActivity(myIntent);
}
});
textViewName.setText(peopleDataSet.get(listPosition).getName());
textViewEmail.setText(peopleDataSet.get(listPosition).getEmail());
imageView.setImageResource(peopleDataSet.get(listPosition).getImage());



}
@Override
public int getItemCount() {
return peopleDataSet.size();
}
}

但是当我按下按钮时出现以下错误:尝试在空对象引用上调用虚方法 java.lang.String android.content.Context.getPackageName()

还有一个问题,如何识别我的列表中的每个按钮,以便它们启动不同的 Activity。

MainActivity2Activity.java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity2Activity extends ActionBarActivity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

逻辑猫

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4000)
at info.osoap.myapplicationzzz.MyAdapter$1.onClick(MyAdapter.java:70)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

MainActivity.java

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<PersonData> people;
static View.OnClickListener myOnClickListener;
private static ArrayList<Integer> removedItems;

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




myOnClickListener = new MyOnClickListener(this);

recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);

layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());

people = new ArrayList<PersonData>();
for (int i = 0; i < MyData.nameArray.length; i++) {
people.add(new PersonData(
MyData.nameArray[i],
MyData.emailArray[i],
MyData.drawableArray[i],
MyData.id_[i]
));
}

removedItems = new ArrayList<Integer>();

adapter = new MyAdapter(people);
recyclerView.setAdapter(adapter);
}


private static class MyOnClickListener implements View.OnClickListener {

private final Context context;

private MyOnClickListener(Context context) {
this.context = context;
}

@Override
public void onClick(View v) {
removeItem(v);
}

private void removeItem(View v) {
int selectedItemPosition = recyclerView.getChildPosition(v);
RecyclerView.ViewHolder viewHolder
= recyclerView.findViewHolderForPosition(selectedItemPosition);
TextView textViewName
= (TextView) viewHolder.itemView.findViewById(R.id.textViewName);
String selectedName = (String) textViewName.getText();
int selectedItemId = -1;
for (int i = 0; i < MyData.nameArray.length; i++) {
if (selectedName.equals(MyData.nameArray[i])) {
selectedItemId = MyData.id_[i];
}
}
removedItems.add(selectedItemId);
people.remove(selectedItemPosition);
adapter.notifyItemRemoved(selectedItemPosition);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.action_add_item) {
// check if any items to add
if (removedItems.size() != 0) {
addRemovedItemToList();
} else {
Toast.makeText(this, "Nothing to add", Toast.LENGTH_SHORT).show();
}
}
return true;
}

private void addRemovedItemToList() {
int addItemAtListPosition = 3;
people.add(addItemAtListPosition, new PersonData(
MyData.nameArray[removedItems.get(0)],
MyData.emailArray[removedItems.get(0)],
MyData.drawableArray[removedItems.get(0)],
MyData.id_[removedItems.get(0)]
));
adapter.notifyItemInserted(addItemAtListPosition);
removedItems.remove(0);
}
}

最佳答案

可能 contextnull 试试看:

 public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), MainActivity2Activity.class);
v.getContext().startActivity(myIntent);
}

关于android - Intent 在我的 RecyclerView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740086/

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