У меня простое приложение которое взаимодействует с firebase
В firebase базе всего два значения это имя и url
Мой Item class
public class ItemFirebase {
String name, url;
public String getName() {
return name;
}
public String getUrl() {
return url;
}
}
Мой 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);
}
}
В самом коде ошибок нет но при попытке открыть приложение оно вылетает
и показывает ошибку с указанием на строку 69 в MainActivity сама строка
view.setOnClickListener(view -> {
Ошибка
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