im trying to move the info of a user from one activity to the other, but everytime i run the app it crashes at the "startActivity(i)" Line (i checked everything else, it only crashes at that line).
that's the MainActivity Code
package com.example.hw1511;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.hw1511.Model.User;
import org.w3c.dom.Text;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements Serializable {
private EditText Name;
private EditText Age;
private EditText Birth;
private Button AddUser;
private Button Send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = findViewById(R.id.Name);
Age = findViewById(R.id.Age);
Birth = findViewById(R.id.Birth);
AddUser = findViewById(R.id.AddUser);
Send = findViewById(R.id.Send);
Intent i = new Intent(MainActivity.this, SecondActivity.class);
List<User> list1 = new LinkedList<>();
AddUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Name.getText().toString().trim().length() > 0 && Age.getText().toString().trim().length() > 0 && Birth.getText().toString().trim().length() > 0) {
User u = new User(Name.getText().toString(), Integer.parseInt(Age.getText().toString()), Birth.getText().toString());
list1.add(u);
Name.setText("");
Age.setText("");
Birth.setText("");
}
else {
Toast.makeText(MainActivity.this, "Fill All", Toast.LENGTH_SHORT).show();
}
}
});
Send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i.putExtra("Users", (Serializable) list1);
startActivity(i);
}
});
}
}
and that's the SecondActivty Code (it's preety much empty)
package com.example.hw1511;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.hw1511.Model.User;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.Attributes;
public class SecondActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
please if anyone help me understand what is making my app crash everytime i press the send button! it would really help me a lot.
Your User objects need to implement Serializable as well.
Also, since LinkedLists are Serializable you can use putSerializable(String key, Serializable value) instead of putExtra
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed last year.
My Application is crashed when I click on ItemView of RecyclerView, There is no error showing in code.
The data are load in toast but not pass to the other class or what don't know, but when I click any Item the appliaction is crash..
This is my Main RecyclerView Class
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class Admin extends AppCompatActivity {
Button addUser;
RecyclerView mainUserRecyclerView;
UserAdapter adapter;
FirebaseDatabase database;
ArrayList<UserInfo> userInfoArrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
mainUserRecyclerView = findViewById(R.id.mainUserRecyclerView);
mainUserRecyclerView.setLayoutManager(new LinearLayoutManager(this));
addUser = findViewById(R.id.addUserBtn);
database= FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference().child("userInfo");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot : snapshot.getChildren()){
UserInfo userInfo = dataSnapshot.getValue(UserInfo.class);
userInfoArrayList.add(userInfo);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Toast.makeText(Admin.this,"Failed to get data",Toast.LENGTH_SHORT).show();
}
});
adapter = new UserAdapter(Admin.this,userInfoArrayList);
mainUserRecyclerView.setAdapter(adapter);
}
public void setAddUser(View view){
Intent intent = new Intent(this,user_detail.class);
startActivity(intent);
}
}
This is my Adapter Class
package com.example.rent;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.viewHolder> {
Context admin;
ArrayList<UserInfo> userInfoArrayList;
public UserAdapter(Admin admin, ArrayList<UserInfo> userInfoArrayList) {
this.admin = admin;
this.userInfoArrayList = userInfoArrayList;
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(admin).inflate(R.layout.user_list_item,parent,false);
return new viewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull UserAdapter.viewHolder holder, int position) {
UserInfo userInfo = userInfoArrayList.get(position);
holder.user_name.setText(userInfo.getShopName());
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(admin,Display_User.class);
//Toast.makeText(admin, ""+userInfo.getOneMonthRent(), Toast.LENGTH_SHORT).show();
intent.putExtra("Name",userInfo.getShopName());
intent.putExtra("Rent",userInfo.getOneMonthRent());
intent.putExtra("Complex",userInfo.getComplexName());
intent.putExtra("PaidRent",userInfo.getPaidAmount());
intent.putExtra("DMonth",userInfo.getDefaultMonth());
intent.putExtra("PendingRent",userInfo.getPendingAmount());
admin.startActivity(intent);
});
}
#Override
public int getItemCount() {
return userInfoArrayList.size();
}
public static class viewHolder extends RecyclerView.ViewHolder {
TextView user_name;
public viewHolder(#NonNull View itemView) {
super(itemView);
user_name = itemView.findViewById(R.id.tvUserName);
}
}
}
And The display User class where I have to fetch the data
package com.example.rent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class Display_User extends AppCompatActivity {
TextView tvName,tvRent,tvComplex,tvPaidRent,tvDefaultMonth,tvPendingRent;
String Sname, rent, complex, paidRent, defaultMonth, pendingRent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_user);
Sname = getIntent().getStringExtra("Name");
rent = getIntent().getStringExtra("Rent");
complex = getIntent().getStringExtra("Complex");
paidRent = getIntent().getStringExtra("PaidRent");
defaultMonth = getIntent().getStringExtra("DMonth");
pendingRent = getIntent().getStringExtra("Pending");
tvName = findViewById(R.id.tvName);
tvName.setText(Sname);
tvRent = findViewById(R.id.tvRent);
tvRent.setText(rent);
tvComplex = findViewById(R.id.tvComplex);
tvComplex.setText(complex);
tvPaidRent = findViewById(R.id.tvPaidRent);
tvPaidRent.setText(paidRent);
tvDefaultMonth = findViewById(R.id.tvDefault);
tvDefaultMonth.setText(defaultMonth);
tvPendingRent = findViewById(R.id.tvPending);
tvPendingRent.setText(pendingRent);
}
}
Have you declared the Activity on the manifest file? And the intent ?
basically im trying to make it so LoginScreen goes to Category screen .
package Screens;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.hr111.User.R;
import Utils.DisplayUtils;
import Utils.PlanUtils;
import Utils.SharedPreferencesUtils;
public class CategoryScreen extends AppCompatActivity {
Button btnExit;
Button btnChemistry;
Button btnBiology;
Button btnPhysics;
TextView textPlayerName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_screen);
btnExit = findViewById(R.id.btnExit);
textPlayerName = findViewById(R.id.textViewName);
btnBiology = findViewById(R.id.btnBiology);
btnPhysics = findViewById(R.id.btnPhysics);
btnChemistry = findViewById(R.id.btnChemistry);
String username = SharedPreferencesUtils.getStringPreference(CategoryScreen.this, "username");
textPlayerName.append(username);
btnBiology.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToQuestions(btnBiology.getText().toString());
}
});
btnPhysics.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToQuestions(btnPhysics.getText().toString());
}
});
btnChemistry.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToQuestions(btnChemistry.getText().toString());
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DisplayUtils.DisplayScript(CategoryScreen.this, "Exit", "Cikmak Istiyor Musunuz?",
"Hayır", "Evet", null, null);
}
});
}
void goToQuestions(String CategoryName){
SharedPreferencesUtils.settingStringPreference(CategoryScreen.this, "category", CategoryName);
SharedPreferencesUtils.settingIntegerPreference(CategoryScreen.this, "questionCount", 1);
SharedPreferencesUtils.settingIntegerPreference(CategoryScreen.this, "score", 0);
PlanUtils.GoToActivity(CategoryScreen.this, GameScreen.class);
}
}
package Screens;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.hr111.User.MainActivity;
import com.hr111.User.R;
import Utils.DisplayUtils;
import Utils.ExcerciseClass;
import Utils.PlanUtils;
import Utils.SharedPreferencesUtils;
public class LoginScreen extends AppCompatActivity implements View.OnClickListener {
Button btnNext;
TextView username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
btnNext = findViewById(R.id.buttonLogin);
username = findViewById(R.id.plaintextName);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = username.getText().toString().trim();
if(nameCheck(name))
{
SharedPreferencesUtils.settingStringPreference(LoginScreen.this, "playername", name);
PlanUtils.GoToActivity(LoginScreen.this, CategoryScreen.class);
}
else
{
DisplayUtils.DisplayScript(LoginScreen.this, "ERROR!", "Gecerli bir oyuncu adi seciniz!", null, null, null, null);
}
}
});
}
boolean nameCheck(String name){
if(name == null) return false;
if(name.length() == 0) return false;
return true;
}
#Override
public void onClick(View v) {
}
}
Both screens are in different Classes.
But it simply crashes before LoginScreen can reach to Category Screen. Any clue why this is not working? I have been trying to figure out and i used different variations of codes but all led me to to nothing. I have also tried on a different project but that also led me to nowhere. Thanks in advance
Since I am new to MVVM and Firebase, I tried creating a simple app, that signs up a User. The SignUp worked, but a weird error and crash of the application came with it. Since I am also trying to understand MVVM, I would appreciate tipps for better code. Anything that makes this better helps. Thanks
MainActivity
package com.example.testingapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MainViewmodel model;
EditText editEmail;
EditText editPassword;
Button btnSignUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editEmail = findViewById(R.id.editEmail);
editPassword = findViewById(R.id.editPassword);
btnSignUp = findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(this);
model = ViewModelProviders.of(this).get(MainViewmodel.class);
}
#Override
public void onClick(View view) {
model.email = editEmail.getText().toString().trim();
model.password = editPassword.getText().toString().trim();
model.signUp();
}
}
MainViewmodel
package com.example.testingapp;
import androidx.lifecycle.ViewModel;
public class MainViewmodel extends ViewModel {
repo repository;
String email;
String password;
public MainViewmodel(){
if(repository == null){
repository = new repo();
}
}
public void signUp(){
repository.firebaseSignUp(email, password);
}
}
Repo
package com.example.testingapp;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import java.util.concurrent.Executor;
import static android.content.ContentValues.TAG;
public class repo {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
public void firebaseSignUp(String email, String password){
Log.d(TAG, "firebaseSignUp: Success" + email + password);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
}
});
}
}
you can remove (Executor) this,. for me it is work.
I am not sure why I am getting this error: imgur.com/a/Hxz5O
Everything seems to be in the right methods and everything so it is a mystery to me why I am getting the error.
Here is my code:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
});
}
}
You are declaring the View.OnClickListener inside another View.OnClickListener for no reason. Remove the second one e.g.
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
}
});
You need setContentView() and your clickListener not correct.
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myView);
SuSe = (Button)findViewById(R.id.MyButtonId);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
Im trying to make a basic program where you press a button and it adds to an integer. Im having trouble updating the text view.
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num;
TextView numview;
private void updateNumber(){
TextView numview = (TextView)findViewById(R.id.Number);
numview.setText(num);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
numview.setText(num);
}
});
}
Help would be great im trying to get into android and books havent been very helpful
There are a few mistakes here. First, you are never calling updateNumber() so numviewis never set. Moreover, you have two TextView numView once global in your MainActivity and again in updateNumber(). There is a better way to do this. But first go to activity_main.xml file and within the button tag add the the line android:onClick="onClick" something like this :
<Button
android:id="#+id/button1"
...
android:onClick="onClick"/>
Next in your MainActivity.java, add the following lines of code :
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num = 0;
TextView numview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numview = (TextView)findViewById(R.id.Number);
}
public void onClick(View view){
num += 1;
numview.setText(String.valueOf(num));
}
}
onClick() will be automatically called when your button is clicked because of the android:onClick property.
you have two different objects with same name and it would cause error.
one of them is defined in your class (TextView numview;) and the other one is defined in your function update number.
in your onClick method you want to change numView defined in your class and it's not initialized. you have to add change your code :
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num;
TextView numview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numview = (TextView)findViewById(R.id.Number);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
numview.setText(num);
}
});
}
Put this code in your onClick function
num = num + 1;
updateNumber();
You are never calling updateNumber()
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num=0;
TextView numview;
private void updateNumber(int number){
numview = (TextView)findViewById(R.id.Number);
numview.setText(number);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
updateNumber(num);
}
});
}