weixin_38891462
weixin_38891462
2018-06-17 17:28
采纳率: 100%
浏览 948

想用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条回答 默认 最新

  • lichaoziz
    lichaoziz 2018-06-18 09:19
    已采纳

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

    点赞 评论
  • qq_35556006
    有点甜0612 2018-06-18 04:54

    AndroidManifest.xml添加了FruitProduce.class没?

    点赞 评论
  • lichaoziz
    lichaoziz 2018-06-18 05:32

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

    点赞 评论
  • berrytyu
    earthyan 2018-06-18 06:31

    AndroidManifest.xml没添加FruitProduce.class 吧

    点赞 评论
  • weixin_38891462
    weixin_38891462 2018-06-18 08:31

    _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的记录,我看不懂。。

    点赞 评论
  • weixin_38891462
    weixin_38891462 2018-06-18 12:38

    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"));
    }
    

    }

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

    点赞 评论

相关推荐