当我从列表中选择项目时,我的应用程序一直崩溃。我已经尝试了以前有关此问题的所有问题。我知道这个问题与意图和背景有关。我似乎无法弄清楚为什么这不起作用。一些请帮助
package com.example.f4f;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class RecipesAdapter extends BaseAdapter implements OnClickListener{
private Context context;
private MainActivity activity;
private LayoutInflater layoutInflater;
private ArrayList<Recipes> recipes;
public RecipesAdapter(Context context){
this.context = context;
}
public RecipesAdapter (MainActivity a, LayoutInflater l, ArrayList<Recipes> data)
{
this.activity = a;
this.layoutInflater = l;
this.recipes = data;
}
@Override
public int getCount(){
return this.recipes.size();
}
@Override
public boolean areAllItemsEnabled ()
{
return true;
}
@Override
public Object getItem(int arg0){
return null;
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
MyViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate (R.layout.list_items, parent, false);
View myView = (View) convertView.findViewById(R.id.listitem);
if(pos % 2 == 0){
myView.setBackgroundColor(Color.parseColor("#d3d3d3"));
}
holder = new MyViewHolder();
holder.recipeName = (TextView) convertView.findViewById(R.id.textView1);
holder.rating = (TextView) convertView.findViewById(R.id.textView2);
//holder.storyDesp = (TextView) convertView.findViewById(R.id.textView3);
convertView.setTag(holder);
}
else {
holder = (MyViewHolder) convertView.getTag();
}
convertView.setOnClickListener(this);
Recipes recipe = recipes.get(pos);
holder.recipe = recipe;
holder.recipeName.setText(recipe.getrecipeName());
holder.rating.setText(recipe.getRating());
//holder.storyDesp.setText(news.getDesp());
return convertView;
}
@Override
public void onClick(View v) {
MyViewHolder holder = (MyViewHolder) v.getTag();
if (v instanceof View) {
//String selectedrecipe = holder.recipe.getID();
//String selectedrecipeurl = "http://www.yummly.com/recipe/external/"+selectedrecipe;
//Uri uri = Uri.parse("http://www.yummly.com/recipe/external/"+selectedrecipe);
Intent intent = new Intent(this.context, SelectedRecipeWebView.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
public static class MyViewHolder {
public TextView recipeName, rating;
public Recipes recipe;
}
}
09-08 01:10:05.933:D / dalvikvm(3346):GC_FOR_ALLOC释放21K,3%免费3931K / 4028K,暂停2ms,总计2ms 09-08 01:10:05.933:D / dalvikvm(3346):GC_FOR_ALLOC释放5K,3%释放4147K / 4252K,暂停2ms,总计2ms 09-08 01:10:05.937:I / dalvikvm-heap(3346):将堆(frag case)增长到6.537MB,用于2536932字节分配 09-08 01:10:05.941:D / dalvikvm(3346):GC_FOR_ALLOC释放<1K,2%自由6624K / 6732K,暂停3ms,总计3ms 09-08 01:10:06.001:W / EGL_genymotion(3346):eglSurfaceAttrib未实现 09-08 01:10:07.153:D / AndroidRuntime(3346):关闭VM 09-08 01:10:07.153:W / dalvikvm(3346):threadid = 1:线程退出未捕获的异常(组= 0xa4d28b20) 09-08 01:10:07.153:E / AndroidRuntime(3346):致命异常:主要 09-08 01:10:07.153:E / AndroidRuntime(3346):进程:com.example.f4f,PID:3346 09-08 01:10:07.153:E / AndroidRuntime(3346):android.content.ActivityNotFoundException:无法找到显式活动类{com.example.f4f / com.example.f4f.SelectedRecipeWebView};你有没有在AndroidManifest.xml中声明这个活动? 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.app.Activity.startActivityForResult(Activity.java:3424) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.app.Activity.startActivityForResult(Activity.java:3385) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.app.Activity.startActivity(Activity.java:3627) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.app.Activity.startActivity(Activity.java:3595) 09-08 01:10:07.153:E / AndroidRuntime(3346):at com.example.f4f.RecipesAdapter.onClick(RecipesAdapter.java:99) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.view.View.performClick(View.java:4438) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.view.View $ PerformClick.run(View.java:18422) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.os.Handler.handleCallback(Handler.java:733) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.os.Handler.dispatchMessage(Handler.java:95) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.os.Looper.loop(Looper.java:136) 09-08 01:10:07.153:E / AndroidRuntime(3346):在android.app.ActivityThread.main(ActivityThread.java:5017) 09-08 01:10:07.153:E / AndroidRuntime(3346):at java.lang.reflect.Method.invokeNative(Native Method) 09-08 01:10:07.153:E / AndroidRuntime(3346):at java.lang.reflect.Method.invoke(Method.java:515) 09-08 01:10:07.153:E / AndroidRuntime(3346):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 09-08 01:10:07.153:E / AndroidRuntime(3346):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 09-08 01:10:07.153:E / AndroidRuntime(3346):at dalvik.system.NativeStart.main(Native Method) 09-08 01:10:09.529:D / dalvikvm(3388):延迟启用CheckJNI 09-08 01:10:09.585:D / dalvikvm(3388):GC_FOR_ALLOC释放37K,3%免费3582K / 3692K,暂停2ms,总计2ms 09-08 01:10:09.621:D / libEGL(3388):已加载/system/lib/egl/libEGL_genymotion.so 09-08 01:10:09.621:D /(3388):HostConnection :: get()建立新主机连接0xb8e43568,tid 3388 09-08 01:10:09.629:D / libEGL(3388):已加载/system/lib/egl/libGLESv1_CM_genymotion.so 09-08 01:10:09.629:D / libEGL(3388):已加载/system/lib/egl/libGLESv2_genymotion.so 09-08 01:10:09.653:W / EGL_genymotion(3388):eglSurfaceAttrib未实现 09-08 01:10:09.653:E / OpenGLRenderer(3388):从GradienCache获取MAX_TEXTURE_SIZE 09-08 01:10:09.653:E / OpenGLRenderer(3388):MAX_TEXTURE_SIZE:16384 09-08 01:10:09.661:E / OpenGLRenderer(3388):从Caches :: initConstraints()获取MAX_TEXTURE_SIZE 09-08 01:10:09.661:E / OpenGLRenderer(3388):MAX_TEXTURE_SIZE:16384 09-08 01:10:09.661:D / OpenGLRenderer(3388):启用调试模式0
答案 0 :(得分:1)
您只在RecipesAdapter(Context上下文)中初始化了上下文。如果在创建RecipesAdapter实例时未使用此构造函数,则在引用它时,上下文具有空值。您需要初始化此值。
从这个类的外观来看,看起来你使用了另一个构造函数。我推断这是因为你的layoutInflater设置好了,显然你没有问题。
编辑:要传递当前上下文的实例,可以将构造函数中的activity字段替换为RecipesAdapter(Context context,LayoutInflater l,ArrayList data)。我这样说是因为这是完成此操作的正常方式(也因为您似乎没有在任何地方使用您的MainActivity字段)。