想用intent开始另外一个活动,编译没有报错,并且能够显示第一个活动

这是我自己做的一个小实验,想通过点击按键事件来打开第二个活动,可是运行的时候有按键打开第二活动的动作时候,程序就自动退出了,不晓得为什么。

public class FruitTitleFragment extends Fragment {

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fruit_title,container,false);
    return view;
}

static class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{

    List<Fruit> FruitList;

     class ViewHolder extends RecyclerView.ViewHolder{

        ImageView FruitImage;
        TextView FruitTitle;
         View FruitView;

        public ViewHolder(View view){
            super(view);
            FruitImage = (ImageView) view.findViewById(R.id.fruit_picture);
            FruitTitle = (TextView) view.findViewById(R.id.fruit_title);
            FruitView = view;
        }
    }

     public FruitAdapter(List<Fruit> FruitList){
        this.FruitList = FruitList;
    }

    public FruitAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final Context context = parent.getContext();
        final View view = LayoutInflater.from(context).inflate(R.layout.fruit_item,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        holder.FruitView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               int position = holder.getAdapterPosition();
                Fruit fruit = FruitList.get(position);
                actionStart(context,fruit.getFruit_Produce(),fruit.getFruit_Name());
            }
        });
        return holder;
    }

    public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
        Fruit fruit = FruitList.get(position);
        holder.FruitTitle.setText(fruit.getFruit_Name());
        holder.FruitImage.setImageResource(fruit.getFruit_image_Resource());
    }

    public int getItemCount() {
        return FruitList.size();
    }
}

public static void actionStart(Context context,String fruit_produce,String fruit_name){
    Intent intent = new Intent(context,FruitProduce.class);
    intent.putExtra("produce",fruit_produce);
    intent.putExtra("name",fruit_name);
    context.startActivity(intent);
}

}
在一个活动里,我用Toast测试过,按键是可以正常发挥作用的,问题应该就是出在intent打开第二个活动那里。

6个回答

你的下一个activity是申请了什么权限么,这个问题好像是自带的虚拟机才会出现的问题,你可以尝试用真机调试一下试试

weixin_38891462
weixin_38891462 回复lichaoziz: 创建第二个活动的时候,因为名字打错,布局和活动不是配套生成的,我以为在setContentView把布局的名字改一下就行。刚才,把活动和布局全部删了重新写,现在运行了。
大约 2 年之前 回复
lichaoziz
lichaoziz 可以把下一个activity的code粘一下
大约 2 年之前 回复

AndroidManifest.xml添加了FruitProduce.class没?

weixin_38891462
weixin_38891462 添加了,是添加活动的时候自动生成的
大约 2 年之前 回复

运行的时候下面android monitors-->logcat里面可以看到运行时错误,基本可以看出来问题的原因

AndroidManifest.xml没添加FruitProduce.class 吧

weixin_38891462
weixin_38891462 添加了,是添加活动的时候自动生成的
大约 2 年之前 回复

_06-18 16:20:31.610 12540-12540/wang.fruit_fragmenttest W/System: ClassLoader referenced unknown path: /data/app/wang.fruit_fragmenttest-2/lib/arm64
06-18 16:20:31.796 12540-12540/wang.fruit_fragmenttest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-18 16:20:31.833 12540-12593/wang.fruit_fragmenttest W/System: ClassLoader referenced unknown path: /system/app/MiuiContentCatcher/lib/arm64
06-18 16:20:31.835 12540-12593/wang.fruit_fragmenttest D/ApplicationLoaders: ignored Vulkan layer search path /system/app/MiuiContentCatcher/lib/arm64:/system/lib64:/vendor/lib64 for namespace 0x7b593770f0
06-18 16:20:31.845 12540-12540/wang.fruit_fragmenttest D/AccessibilityManager: current package=wang.fruit_fragmenttest, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=true, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
06-18 16:20:32.166 12540-12540/wang.fruit_fragmenttest I/gralloc: Open gralloc successfully!, name = gpu0
06-18 16:20:32.276 12540-12612/wang.fruit_fragmenttest I/mali: No configuration file found, attempting to use environment
06-18 16:20:32.276 12540-12612/wang.fruit_fragmenttest I/mali: Application name set to: Untitled
06-18 16:20:32.276 12540-12612/wang.fruit_fragmenttest I/mali: Output directory set to: /data/data/com.mali.testjava
06-18 16:20:32.299 12540-12612/wang.fruit_fragmenttest I/OpenGLRenderer: Initialized EGL, version 1.4
06-18 16:20:32.299 12540-12612/wang.fruit_fragmenttest D/OpenGLRenderer: Swap behavior 0
这个是logcat的记录,我看不懂。。

public class FruitProduce extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fruit_produce2);
    TextView fruit_produce = (TextView) findViewById(R.id.fruit_produce);
    TextView fruit_name = (TextView) findViewById(R.id.fruit_title);
    Intent intent= getIntent();
    fruit_produce.setText(intent.getStringExtra("produce"));
    fruit_name.setText(intent.getStringExtra("name"));
}

}

我用真机试的时候,也是点击的时候,程序会直接闪退

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问