试图从适配器调用,但我发现这是更正确的方法。在日志中,所有传输都是正确的,我从适配器调用此方法。 Itried chnging toContext()但它也可以工作。
错误:
Process: pl.michalz.hideonscrollexample, PID: 2078
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:128)
at android.content.Intent.<init>(Intent.java:4449)
at pl.michalz.hideonscrollexample.fragment.PartThreeFragment.infoActivityCall(PartThreeFragment.java:122)
at pl.michalz.hideonscrollexample.adapter.parttwo.MyAdapter$ViewHolder.onClick(MyAdapter.java:83)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在此代码中:
public class PartThreeFragment extends Fragment {
//pridetas
ArrayList<Receptas> receptasAList = new ArrayList<>();
MyAdapter myAdapter;
RecyclerView recyclerView;
RequestQueue requestQueue;
Receptas receptas;
InfoActivity infoActivity = new InfoActivity();
private final String url1 = "http://smkbaig.esy.es/get_info_test.php";
public static PartThreeFragment createInstance() {
PartThreeFragment partThreeFragment = new PartThreeFragment();
Log.w("myApp", ">>partThree)");
return partThreeFragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.w("myApp", "partThree>>");
View view = inflater.inflate(R.layout.fragment_part_three, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
myAdapter = new MyAdapter(getActivity());
recyclerView.setAdapter(myAdapter);
volleyJson(url1, "");
return view;
}
public void volleyJson(String u, String q) {
String url = "";
String query = "";
if(u != ""){
url = u;
}
if(q != ""){
query = q;
}
Log.w("myApp", "volleyJson -started- ");
requestQueue = Volley.newRequestQueue(this.getActivity());
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
receptasAList.clear();
JSONArray ja = response.getJSONArray("receptai");
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
receptas = new Receptas();
receptas.setPav(jsonObject.getString("pav"));
//receptas.setApras(jsonObject.getString("apras"));
receptas.setIngred_sk(jsonObject.getString("ingred_sk"));
receptasAList.add(receptas);
Log.w("myApp", "receptasAList.add(receptas); " + receptasAList.get(0).getPav());
}
} catch (JSONException e) {
e.printStackTrace();
}
//atliekamas po getRecceptas()
myAdapter.setReceptasList(receptasAList);
Log.w("myApp", "myAdapter.setReceptasList(receptasAList);");
// myAdapter.notifyDataSetChanged();
//mTextView.setText("Response: " + response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}
);
requestQueue.add(jsObjRequest);
Log.w("myApp", "requestQueue.add(jsObjRequest);");
}
public void refresh(String newTxt, Boolean submit){
final List<Receptas> receptasFilter = myAdapter.filter(receptasAList, newTxt);
myAdapter.setFilter(receptasFilter, submit, getActivity());
}
public void infoActivityCall(int position, Receptas receptas)
{
Log.w("myApp", "infoActivityCall return = " + position + " ; " + " re " + receptas.getPav());
// - - - - - - - - - E R R O R H E R E - - - - - - - - -
Intent intent = new Intent(getActivity(), InfoActivity.class);
startActivity(intent);
}
}
编辑1:
从ViewHolder
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
班级致电
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView itemTextViewPav, itemTextViewIngred_sk;
ArrayList<Receptas> receptasL;
PartThreeFragment partThreeFragment = new PartThreeFragment();
public ViewHolder(View itemView, ArrayList<Receptas> receptasL) {
super(itemView);
this.receptasL = receptasL;
itemTextViewPav = (TextView) itemView.findViewById(R.id.itemTextViewPav);
itemTextViewIngred_sk = (TextView) itemView.findViewById(R.id.itemTextViewIngred_sk);
// Attach a click listener to the entire row view
itemView.setOnClickListener(this);
}
//- - - - - - - - - - - - - - C L I C K L I S T E N E R - - - - - - - - - - - - - - - -
// Handles the row being being clicked
@Override
public void onClick(View view) {
int position = getLayoutPosition(); // gets item position
Receptas receptas = receptasL.get(position);
// We can access the data within the views
Toast.makeText(view.getContext(), "Click /VH + ", Toast.LENGTH_SHORT).show();
partThreeFragment.infoActivityCall(position, receptas);
}
}
答案 0 :(得分:2)
首先,从片段中打开另一个活动不是一个好习惯,因此请查看this approach以将您的片段与活动进行通信,然后创建您的意图,如:
new Intent(ActualClass.this, NewClass.class);
答案 1 :(得分:1)
我认为错误就在这一行
Volley.newRequestQueue(this.getActivity());
所以我建议您创建一个类型为活动的全局变量数据
Activity mActivity;
并在此{<1}}中保存当前活动
mActivity
然后每次都使用@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}
。
答案 2 :(得分:0)
ViewHolder中的适配器onClick侦听器中的上下文为null,您可以放置适配器代码吗?
答案 3 :(得分:0)
我想在Activity中调用的函数:
public void infoActivity(Context context){
Intent intent = new Intent(PartThreeActivity.this, InfoActivity.class);
startActivity(intent);
}
infoActivity()
调用适配器ViewHolder。
我将mActivity上下文传递给片段myAdapter = new MyAdapter(mActivity);
:
正如@Shree Krishnam所写,我将活动保存为全局变量数据类型:
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
mActivity = activity;
}
在适配器中我保存了它:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
Context context;
//...code
public MyAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
//...code
}
然后将context
传递给ViewHolder
并像这样使用它:
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//...code...
@Override
public void onClick(View view) {
//...code
((PartThreeActivity)context).infoActivity(context);
}
}