C
C
Chesterfield252021-10-25 13:26:39
Java
Chesterfield25, 2021-10-25 13:26:39

What is the correct way to pass data from one Activity to another Activity?

I have a simple application that interacts with firebase
In the firebase base, there are only two values ​​\u200b\u200bthis name and url
61767c11eac04760398764.png
617685a10742d333429438.png

My Item class

public class ItemFirebase {

    String name, url;

    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }
}


My Adapte

public class MyAdapter extends RecyclerView.Adapter <MyAdapter.MyViewHolder> {

    MainActivity context;

    ArrayList<ItemFirebase> list;

    public MyAdapter(MainActivity context, ArrayList<ItemFirebase> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        return  new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        ItemFirebase itemFirebase = list.get(position);
        holder.name.setText(itemFirebase.getName());
        holder.data.setText(itemFirebase.getUrl());


    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name, data;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.name);
            data = itemView.findViewById(R.id.url);

        }
    }
}


MainActivity
public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    DatabaseReference database;
    MyAdapter myAdapter;
    ArrayList<ItemFirebase> list;
    Button view;
    TextView data;

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

        recyclerView = findViewById(R.id.materialList);
        view = findViewById(R.id.view);
        data = findViewById(R.id.url);
        database = FirebaseDatabase.getInstance().getReference("Items");
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        list = new ArrayList<>();
        myAdapter = new MyAdapter(this,list);
        recyclerView.setAdapter(myAdapter);

        database.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot dataSnapshot : snapshot.getChildren()){

                    ItemFirebase itemFirebase = dataSnapshot.getValue(ItemFirebase.class);
                    list.add(itemFirebase);


                }
                myAdapter.notifyDataSetChanged();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        view.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
            intent.putExtra("data", data.getText().toString());
            startActivity(intent);
            finish();
        });


    }
}


WebViewActivity
public class WebViewActivity extends AppCompatActivity {

    TextView url;
    WebView webView;

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

        url.findViewById(R.id.url);

        String data = getIntent().getStringExtra("data");

        webView = findViewById(R.id.webView);
        // включаем поддержку JavaScript
        webView.getSettings().setJavaScriptEnabled(true);
        // указываем страницу загрузки
        webView.loadUrl(url.getText().toString() + " " + data);

    }

}


There are no errors in the code itself, but when you try to open the application, it crashes
and shows an error indicating line 69 in MainActivity, the line itself view.setOnClickListener(view -> {

Error
Process: com.buratinoapps.newairdrop, PID: 18901
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.buratinoapps.newairdrop/com.buratinoapps.newairdrop.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-10-25
@Chesterfield25

R.id.view is not in the markup.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question