繁体   English   中英

不使用按钮时,为什么新活动不会从此 OnClick 方法启动?

[英]Why does a new activity not start from this OnClick method when not using a button?

我在 Android Studio 中工作,并且正在使用回收器视图从数据库创建项目列表。 在 RecyclerViewAdapter 中的 OnClick 函数中,我的目标是使用来自数据库的额外消息启动一个新活动(该函数可以正常工作并且在这种情况下不是问题)。 单击屏幕时,应用程序崩溃而不是加载新活动。

任何阻止应用程序崩溃的帮助将不胜感激。 谢谢

//RecyclerViewAdapter
package com.example.penrice9.myapplication;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Penrice9 on 09/10/2018.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private static final String TAG = "RecyclerViewAdapter";
    public static String EXTRA_MESSAGE = "com.example.penrice9.myapplication.MainActivity";
    private ArrayList<String> eventName = new ArrayList<>();
    private ArrayList<Bitmap> images = new ArrayList<>();
    private Context context;
    public LoginScreen ls;
    public Database db;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    public RecyclerViewAdapter(ArrayList<String> eventName, ArrayList<Bitmap> images, Context context) {
            this.eventName = eventName;
        this.images = images;
        this.context = context;
    }



    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: called.");

        holder.image.setImageBitmap(images.get(position));

        holder.name.setText(eventName.get(position));

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, eventName.get(position) + " clicked");
                Intent intent = new Intent(context, eventScreen.class);
                intent.putExtra("name", eventName.get(position));
                context.startActivity(intent);


            }
        });
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        RelativeLayout layout;
        ImageView image;
        CardView card;
        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            layout = itemView.findViewById(R.id.layout);
            image = itemView.findViewById(R.id.image);
            card = itemView.findViewById(R.id.card);
        }
    }


    }


//MainActivity



package com.example.penrice9.myapplication;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    public static String EXTRA_MESSAGE = "com.example.penrice9.myapplication.eventScreen";
    private ArrayList<String> eventName = new ArrayList<>();
    private ArrayList<Bitmap> images = new ArrayList<>();
    private Context context;
    public RecyclerViewAdapter rva;


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


    public void getImages() {
        images.add(BitmapFactory.decodeFile("https://upload.wikimedia.org/wikipedia/commons/thumb/9/90/Stamford_Bridge_Clear_Skies.JPG/1200px-Stamford_Bridge_Clear_Skies.JPG"));
        eventName.add("Chelsea vs Liverpool");

        images.add(BitmapFactory.decodeFile("https://upload.wikimedia.org/wikipedia/commons/thumb/9/90/Stamford_Bridge_Clear_Skies.JPG/1200px-Stamford_Bridge_Clear_Skies.JPG"));
        eventName.add("Chelsea vs Cardiff");

        images.add(BitmapFactory.decodeFile("https://static.independent.co.uk/s3fs-public/thumbnails/image/2016/11/03/12/west-ham.jpg"));
        eventName.add("West Ham vs Chelsea");

        images.add(BitmapFactory.decodeFile("http://stadiumastro-kentico.s3.amazonaws.com/stadiumastro/media/perform-article/2018/mac/12/toumba-cropped_w0t88ccil23v100heat0tve2z.jpg?ext=.jpg"));
        eventName.add("PAOK vs Chelsea");

        images.add(BitmapFactory.decodeFile("http://www.stadiumguide.com/wp-content/uploads/standrews_front.jpg"));
        eventName.add("Birmingham City vs West Brom ");

        images.add(BitmapFactory.decodeFile("https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Selhurst_Park_Holmesdale_Stand.jpg/1200px-Selhurst_Park_Holmesdale_Stand.jpg"));
        eventName.add("Crystal Palace vs Wolves");

        images.add(BitmapFactory.decodeFile("https://www.thenational.ae/image/policy:1.626869:1504962545/Pawan-Singh-The-National.jpg?f=16x9&w=1200&$p$f$w=4fbc2ca"));
        eventName.add("Pakistan vs Bangladesh");

        images.add(BitmapFactory.decodeFile("http://images.supersport.com/Bloemfontein-Chevrolet-Park-general%20view-120117-G600.jpg"));
        eventName.add("South Africa vs Zimbabwe");

        images.add(BitmapFactory.decodeFile("http://www.kkr.in/img/stadium/eden_garden_new.jpg"));
        eventName.add("India vs West Indies");

        images.add(BitmapFactory.decodeFile("https://www.fanpass.co.uk/blog/wp-content/uploads/2016/03/O2-Arena-tickets.jpg"));
        eventName.add("Michael McIntyre");

        images.add(BitmapFactory.decodeFile("https://www.fanpass.co.uk/blog/wp-content/uploads/2016/03/O2-Arena-tickets.jpg"));
        eventName.add("Enrique Iglesias");

        images.add(BitmapFactory.decodeFile("https://www.fanpass.co.uk/blog/wp-content/uploads/2016/03/O2-Arena-tickets.jpg"));
        eventName.add("Jess Glyne");

        createView();
    }

    public void createView() {
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(eventName, images, this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }

    public void newPage(String message) {
        Intent intent = new Intent(this, eventScreen.class);
        intent.putExtra(EXTRA_MESSAGE,message);
        startActivity(intent);
    }
}


//Screen to be loaded to, called "eventScreen.java"
package com.example.penrice9.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

public class eventScreen extends AppCompatActivity {
    public Database db;
    public TextView name, location, date, website;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_screen);
        name = findViewById(R.id.name);
        location = findViewById(R.id.location);
        date = findViewById(R.id.date);
        website = findViewById(R.id.website);


        if (getIntent().hasExtra("name")) {
            String eventName = getIntent().getStringExtra("name");
            loadInfo(eventName);
        }
        else{
            Toast.makeText(this, "No message", Toast.LENGTH_LONG).show();
        }




    }

    public void loadInfo(String title){
        String [] info = db.getEventData(title);
        name.setText("the ");
        location.setText("the");
        date.setText("the");
        website.setText("the");
    }
}


//CardView XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/card">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Event"
            android:id="@+id/name"
            android:textSize="50sp"
            android:textAlignment="center"/>

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_gravity="center"            />

    </LinearLayout>

    </android.support.v7.widget.CardView>


//xml containing recycler view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.penrice9.myapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="90dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Events"
                android:textAlignment="center"
                android:textSize="50sp"/>


        </RelativeLayout>


        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recyclerView">

        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

</RelativeLayout>




    --------- beginning of crash
10-18 08:20:37.625 3148-3148/com.example.penrice9.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.penrice9.myapplication, PID: 3148
                                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.penrice9.myapplication/com.example.penrice9.myapplication.eventScreen}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      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)
                                                                                   Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                                      at com.example.penrice9.myapplication.Database.getEventData(Database.java:92)
                                                                                      at com.example.penrice9.myapplication.eventScreen.loadInfo(eventScreen.java:36)
                                                                                      at com.example.penrice9.myapplication.eventScreen.onCreate(eventScreen.java:24)
                                                                                      at android.app.Activity.performCreate(Activity.java:6237)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      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 eventScreen extends AppCompatActivity {
    public Database db; // db is NULL
    public TextView name, location, date, website;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_screen);
        name = findViewById(R.id.name);
        location = findViewById(R.id.location);
        date = findViewById(R.id.date);
        website = findViewById(R.id.website);

        // You should initialize your DataBase db here before calling loadInfo()

        if (getIntent().hasExtra("name")) {
            String eventName = getIntent().getStringExtra("name");
            loadInfo(eventName);
        }
        else{
            Toast.makeText(this, "No message", Toast.LENGTH_LONG).show();
        }




    }

    public void loadInfo(String title){
        // db is still NULL
        // The crash happens here when you call --> db.getEventData() --> null.getEventData(title)
        String [] info = db.getEventData(title);
        name.setText("the ");
        location.setText("the");
        date.setText("the");
        website.setText("the");
    }
}

两个最可能的问题是 (1) eventScreen活动未在您的 AndroidManifest.xml 中声明或 (2) context变量为空。

要解决 (1),请将其添加到您的清单中:

<activity
    android:name=".eventScreen"/>

要解决 (2),请使用 ViewHolder 的 itemView 中的保证非空Context

Context c = v.getContext();
Log.d(TAG, eventName.get(position) + " clicked");
Intent intent = new Intent(c, eventScreen.class);
intent.putExtra("name", eventName.get(position));
c.startActivity(intent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM