I am attempting to get Strings from 2 Edittexts within a Dialog Fragment, but my app keeps crashing when the activity starts. It is some sort of issue with being able to bind the edittexts from the dialog. I am not sure of the correct way to bind them.
Here is the crash:
09-24 11:34:30.366 16147-16147/com.epicodus.concertaid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.epicodus.concertaid, PID: 16147
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epicodus.concertaid/com.epicodus.concertaid.ui.UserProfileActivity}: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
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.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
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)
Caused by: java.lang.IllegalStateException: Required view 'userEmailEditText' with ID 2131558627 for field 'mUserEmailEditText' was not found. If this view is optional add '#Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:15)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:8)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
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)
And here is the Activity:
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.epicodus.concertaid.Constants;
import com.epicodus.concertaid.R;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import butterknife.Bind;
import butterknife.ButterKnife;
public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener {
#Bind(R.id.summaryTextView) TextView mSummaryTextView;
#Bind(R.id.deleteAccountButton) Button mDeleteAccountButton;
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
private Firebase mFirebaseRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ButterKnife.bind(this);
mDeleteAccountButton.setOnClickListener(this);
//GET REFERENCE TO FIREBASE
mFirebaseRef = new Firebase(Constants.FIREBASE_URL);
//SETS FONT FOR TITLE
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MUSICNET.ttf");
mSummaryTextView.setTypeface(tf);
}
#Override
public void onClick(View view) {
if(view == mDeleteAccountButton) {
createAlertDialog();
}
}
public void deleteUser(String userEmail, String userPassword) {
Firebase.ResultHandler handler = new Firebase.ResultHandler() {
#Override
public void onSuccess() {
Toast.makeText(UserProfileActivity.this, "User deleted Successflly", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FirebaseError firebaseError) {
Toast.makeText(UserProfileActivity.this, "There was an error, please try again", Toast.LENGTH_LONG).show();
}
};
mFirebaseRef.removeUser(userEmail, userPassword, handler );
}
public void createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
LayoutInflater inflater = UserProfileActivity.this.getLayoutInflater();
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
String userEmail = mUserEmailEditText.getText().toString();
String userPassword = mUserPasswordEditText.getText().toString();
deleteUser(userEmail, userPassword);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
This is the layout for the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.epicodus.concertaid.ui.UserProfileActivity"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:src="#drawable/background1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account Details"
android:textColor="#0288D1"
android:id="#+id/summaryTextView"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Photo"
android:textColor="#0288D1"
android:background="#drawable/buttonshape"
android:id="#+id/addPhotoButton"
android:layout_below="#+id/summaryTextView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/userPhotoImageView"
android:layout_marginTop="25dp"
android:layout_below="#+id/addPhotoButton"
android:layout_centerHorizontal="true"
android:contentDescription="#string/current_user_s_image"
android:src="#drawable/blank_user" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Email"
android:textColor="#0288D1"
android:id="#+id/changeEmailButton"
android:background="#drawable/buttonshape"
android:layout_below="#+id/userPhotoImageView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Password"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:background="#drawable/buttonshape"
android:id="#+id/changePasswordEmailButton"
android:layout_below="#+id/changeEmailButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Account"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:id="#+id/deleteAccountButton"
android:layout_below="#+id/changePasswordEmailButton"
android:layout_centerHorizontal="true"
android:background="#drawable/buttonshape" />
Here is the layout for the AlertDialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/userEmailEditText"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Enter Email" />
<EditText
android:id="#+id/userPasswordEditText"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="Enter Password"/>
</LinearLayout>
Any example to show how to correctly Bind the mUserEmailEditText and mUserPasswordEditText
Thank you
When you use bind like that is supposed to be from a existing ID in your activity layout, since you dont have it in your xml it will crash.. Dont bind the EditText and just create them in the AlertDialog, and im use setParams to change their layout
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
You can not bind the view from the xml which is not inflated as part of Activity's view. So remove #Bind from above line as follows.
EditText mUserEmailEditText;
EditText mUserPasswordEditText;
and inflate them while creating your dialog in createAlertDialog() method. So replace following 2 lines
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
with
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
mUserEmailEditText = rootView.findViewById(R.id.userEmailEditText);
mUserPasswordEditText = rootView.findViewById(R.id.userPasswordEditText);
builder.setView(rootView);
Related
i made a simple betting app for me and my friend.
I press one button from the bottom to set the value of the bid. and after that if i win i press i win and otherwise i press i lost. when i press i lost or i win the text view get the value of the bet. I dont have any errors , i dont know why is it crashing when i open the app.
here is the code:
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int totalMoney= 0;
int betValue = 0;
String savedMoney;
TextView textView = (TextView) findViewById(R.id.textView);
public void clickP(View view){
totalMoney= totalMoney - betValue;
}
public void clickC(View view){
totalMoney= totalMoney + betValue;
}
public void click1(View view){
betValue = 1;
}
public void click2(View view){
betValue = 2;
}
public void click3(View view){
betValue = 3;
}
public void click4(View view){
betValue = 4;
}
public void click5(View view){
betValue = 5;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savedMoney = textView.getText().toString();
totalMoney = Integer.parseInt(savedbani);
SharedPreferences sharedPreferences = this.getSharedPreferences("com.markusappcompany.baniipemasa", Context.MODE_PRIVATE);
sharedPreferences.edit().putInt("Money", totalMoney).apply();
sharedPreferences.getInt("Money", totalMoney);
textView.setText(totalMoney);
}
}
This is what i get in Logcat:
05-22 19:54:52.918 20536-20536/com.markusappcompany.baniipemasa E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.markusappcompany.baniipemasa, PID: 20536
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.markusappcompany.baniipemasa/com.markusappcompany.baniipemasa.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:338)
at android.widget.TextView.setText(TextView.java:5494)
at com.markusappcompany.baniipemasa.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.markusappcompany.baniipemasa.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="117dp"
android:layout_marginEnd="1dp"
android:layout_marginStart="80dp"
android:layout_marginTop="228dp"
android:text="I LOST"
app:layout_constraintBottom_toTopOf="#+id/button3"
app:layout_constraintEnd_toStartOf="#+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="117dp"
android:layout_marginEnd="80dp"
android:layout_marginStart="1dp"
android:layout_marginTop="228dp"
android:text="I WIN"
app:layout_constraintBottom_toTopOf="#+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="70dp"
android:layout_marginStart="16dp"
android:text="1 EUR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button4"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:layout_marginTop="117dp"
android:text="2 EUR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button5"
app:layout_constraintStart_toEndOf="#+id/button3"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:layout_marginTop="117dp"
android:text="3 EUR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button6"
app:layout_constraintStart_toEndOf="#+id/button4"
app:layout_constraintTop_toBottomOf="#+id/button2" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="117dp"
android:text="4 LEI"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button5"
app:layout_constraintTop_toBottomOf="#+id/button2" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="148dp"
android:layout_marginStart="148dp"
android:text="5 LEI"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="163dp"
android:layout_marginEnd="221dp"
android:layout_marginStart="190dp"
android:layout_marginTop="16dp"
android:text="0"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="25dp"
android:text="Total money:"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="#+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
thanks in advance!
First of all you remove all click methods your activity should look like this:
public class MainActivity extends AppCompatActivity {
int totalMoney= 0;
int betValue = 0;
String savedMoney;
TextView textView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
Button yourButton = findViewById(R.layout.yourButtonId)
Button yourButton2 = findViewById(R.layout.yourButtonId2)
// for all Buttons
}}
You also must give the button an id in the activity_main file. After that you have to implement an onClickListener for all thes buttons like this:
yourButton1.setOnClickListener(this);
// the same for every Button
then you add this line:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
at the end the listener:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.yourButtonID:
// when button with this ID is clicked do your stuff here
case R.id.otherButtonID:
// when button with this ID is clicked do your stuff here
}
}
Here the explanation first you find the buttons and the textView after that you set an onclicklistener so when you click one button the button call the method onClick. The switch statment is called an finds out which button is called.
Place your Textview's findTextViewById inside OnCreateView.
Try to find the Buttons and textViews in the onCreate method.
Also try to handle the Buttons with OnClickListener like this:
Button yourButtton = findViewById(R.id.yourButttonID);
yourButtton.setOnClickListener(this);
And this for all Buttons.
then:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.yourButtonID:
// when button with this ID is clicked
case R.id.otherButtonID:
// when button with this ID is clicked
}
}
Problem:
I have a java/xml file in android studio. I need to play two buttons; One for "Map page" and second for "Create new account" (Signup activity). The first button work, but the second close my app. What is the problem?
What I know:
if it is empty xml file the button for to go to the xml file work.
The errors:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sampleapp, PID: 8062
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sampleapp/com.example.sampleapp.SignUpActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
at com.example.sampleapp.SignUpActivity.onCreate(SignUpActivity.java:33)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
Scripts:
LoginActivity.java:
package com.example.sampleapp;
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;
public class LoginActivity extends AppCompatActivity {
public EditText username;
public EditText password;
public TextView errorView;
public Button login;
public Button createNewAccount;
public void initMapPage(){
Intent mapPage = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mapPage);
finish();
}
public void initSignUpPage(){
Intent signUpPage = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(signUpPage);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.UsernameBox);
password = (EditText) findViewById(R.id.PasswordBox);
errorView = (TextView) findViewById(R.id.errorView);
login = (Button) findViewById(R.id.LoginButton);
createNewAccount = (Button) findViewById(R.id.SignupButton);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String errorMsg = "";
if (username.getText().toString().equals("") || password.getText().toString().equals("")) {
if (username.getText().toString().equals("")) {
errorMsg += "Username block is empty\n";
}
if (password.getText().toString().equals("")) {
errorMsg += "Password block is empty\n";
}
errorView.setText(errorMsg);
}
else {
initMapPage();
}
}
});
createNewAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
initSignUpPage();
}
});
}
}
activity_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".LoginActivity">
<TextView
android:id="#+id/WelcomeBox"
android:layout_width="210dp"
android:layout_height="101dp"
android:layout_marginTop="64dp"
android:text="Welcome \nTo Our App!"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/UsernameBox"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/WelcomeBox" />
<EditText
android:id="#+id/PasswordBox"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/UsernameBox" />
<Button
android:id="#+id/SignupButton"
android:layout_width="237dp"
android:layout_height="55dp"
android:layout_marginTop="24dp"
android:text="Create New Acount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<Button
android:id="#+id/LoginButton"
android:layout_width="115dp"
android:layout_height="63dp"
android:layout_marginTop="12dp"
android:text="LOGIN"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/errorView" />
<TextView
android:id="#+id/textView4"
android:layout_width="60dp"
android:layout_height="29dp"
android:layout_marginTop="24dp"
android:text="OR"
android:textAlignment="center"
android:textColor="#D3000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LoginButton" />
<TextView
android:id="#+id/errorView"
android:layout_width="209dp"
android:layout_height="62dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
android:textAlignment="center"
android:textColor="#FF0000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/PasswordBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
SignUpActivity.java:
package com.example.sampleapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SignUpActivity extends AppCompatActivity {
public Button backToLogin;
public Button SignUp;
public EditText username;
public EditText password;
public EditText email;
public void initLoginPage(){
Intent loginPage = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(loginPage);
finish();
}
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
SignUp = (Button) findViewById(R.id.suButton);
backToLogin = (Button) findViewById(R.id.back_to_login);
username = (EditText) findViewById(R.id.UsernameBoxSignUp);
password = (EditText) findViewById(R.id.PasswordBoxSignUp);
email = (EditText) findViewById(R.id.EmailBox);
backToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
initLoginPage();
}
});
}
}
activity_sign_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".SignUpActivity">
<EditText
android:id="#+id/UsernameBoxSignUp"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="54dp"
android:layout_marginTop="160dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/back_to_login" />
<EditText
android:id="#+id/PasswordBoxSignUp"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="55dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/EmailBox" />
<Button
android:id="#+id/suButton"
android:layout_width="170dp"
android:layout_height="53dp"
android:layout_marginStart="120dp"
android:layout_marginTop="36dp"
android:text="SignUp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/PasswordBoxSignUp" />
<EditText
android:id="#+id/EmailBox"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="44dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/UsernameBoxSignUp" />
<ImageButton
android:id="#+id/back_to_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="54dp"
android:layout_marginTop="28dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="?attr/actionModeCloseDrawable" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is that ImageButton (and thus also the appcompat version) is a subclass of ImageView, not of Button. You should either change the cast to that, or just remove it entirely if you're only using View methods like setOnClickListener.
Is the username or password text set up?
Android:text=""
A hint is just a hint, not the actual response. So it gets an error when it looking for something that's not even set up yet.
Your calling SignUpActivity twice
Place the intent
Intent signUpPage = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(signUpPage);
on onClick method
and don't forget to add android:onClick="OnCreateAccount" on your button
I'm unable to get data(text) from an EditText to a TextView via a Button.
Logic should be fine and I can't really find the mistake.
I get the error as soon as I click the button which triggeres "sendMessage(View v)".
I can't append text to "chat" but I don't know why.
MainActivity.java:
package com.chattr.neonardo.chattr;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView chat;
EditText message;
Button send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chat = (TextView) findViewById(R.id.chat);
message = (EditText) findViewById(R.id.message);
send = (Button) findViewById(R.id.send);
Log.d("test", "test2");
/* send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chat.append("\n" + message.getText().toString());
}
});*/
}
public void onMatchTap(View v) {
Toast myToast = Toast.makeText(getApplicationContext(), R.string.searching, Toast.LENGTH_LONG);
myToast.show();
setContentView(R.layout.chat_main);
Log.d("test", "test4");
}
public void sendMessage(View v) {
Log.d("test", "test6");
chat.append("\n" + message.getText().toString());
Log.d("test", "test7");
}
}
I've tried something with the onClickListener but it didn't work so I commented it out.
main_activity.xml:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.chattr.neonardo.chattr.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/chattr"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="91dp"
android:onClick="onMatchTap"
android:text="#string/start_chat" />
I'm working with two different xml's because I'm changing scenes.
chat_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="#+id/chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.00"
android:autoLink="web"
android:text="#string/chat_connection"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/message"
android:layout_width="301dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="#string/defaultMessage" />
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:elevation="0dp"
android:onClick="sendMessage"
android:text="Send" />
</LinearLayout>
That's the error:
Process: com.chattr.neonardo.chattr, PID: 24340
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference
at com.chattr.neonardo.chattr.MainActivity.sendMessage(MainActivity.java:47)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Remove setContentView(R.layout.chat_main);
You should use Intent to open a new activity inside onClick method.
Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);
Should be
public void onMatchTap(View v) {
Toast myToast = Toast.makeText(getApplicationContext(), R.string.searching, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,ChatMain.class);
startActivity(intent);
Log.d("test", "test4");
}
chat = (TextView) findViewById(R.id.chat);
This TextView is Define in your chat_main.xml file and you try to bind it in your Main Activity.that's why it will gives you NullPointerException.
I created a new activity in my android studio app and I have a button that performs an action when click. I am not sure what's causing the error as this happens when I actually run the activity. If I remove the onClick code, the activity runs. Thanks for any help. I'm getting this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.masesk.kalkulator, PID: 4102
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.masesk.kalkulator/com.masesk.kalkulator.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.masesk.kalkulator.Main2Activity.onCreate(Main2Activity.java:29)
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)
This is my Main2Activity.java code:
package com.masesk.kalkulator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class Main2Activity extends AppCompatActivity {
private ToggleButton textToBinary;
private Button trace;
private EditText text;
private EditText binary;
private String textInput;
private String binaryInput;
StringBuilder binaryText = new StringBuilder();
#Override
protected void onCreate(Bundle savedInstanceState) {
this.textToBinary = (ToggleButton)findViewById(R.id.textToBinary);
this.trace = (Button)findViewById(R.id.trace);
this.text = (EditText)findViewById(R.id.text);
this.binary = (EditText)findViewById(R.id.binary);
trace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(textToBinary.isActivated() == false){
textInput = text.getText().toString();
byte[] bytes = textInput.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binaryText.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binaryText.append(' ');
}
}
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public void onResume(){
this.binary = (EditText)findViewById(R.id.binary);
binary.setText(binaryText.toString(), TextView.BufferType.EDITABLE);
}
}
and here is my XML code
<?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:id="#+id/content_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.masesk.kalkulator.Main2Activity"
tools:showIn="#layout/activity_main2">
<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textToBinary"
android:layout_alignParentEnd="true"
android:textOff="Binary To Text"
android:textOn="Text to Binary" />
<TextView
android:text="Text:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textToBinary"
android:layout_alignParentStart="true"
android:id="#+id/textView2"
android:layout_alignParentEnd="true" />
<EditText
android:background="#drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true"
android:id="#+id/text"
android:layout_alignParentEnd="true" />
<TextView
android:text="Binary Code:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:layout_alignParentStart="true"
android:id="#+id/textView3" />
<EditText
android:background="#drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:layout_below="#+id/textView3"
android:layout_alignParentStart="true"
android:id="#+id/binary"
android:layout_alignParentEnd="true" />
<Button
android:text="convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/binary"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="#+id/trace"
android:layout_alignParentEnd="true" />
</RelativeLayout>
In the OnCreate method, you should always first put
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
This is because the view is not inflated until you have set it with setContentView. This means any findViewById call before will return null.
Additionally, you must also set the super.onResume(...) call at the very beginning of onResume(), as the Android documentation states. This also goes for the other lifecycle methods as well. This should also solve your setText problem.
I am creating address book, but when I am adding new number and getting button from XML I am getting NullPointerException.
Here is the code that causes the error
package oo.seniorlauncher.com.o_seniorlauncher;
import oo.seniorlauncher.com.o_seniorlauncher.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class newNumber extends Activity {
boolean ready = false;
EditText nameField;
EditText numberField;
Context context;
String name;
String number;
String place= "";
public boolean ready(){
return ready;
}
public void run(String plac,Context context){
this.context = context;
place = plac;
MainFrame frame = new MainFrame();
((Activity) context).setContentView(R.layout.adnumber);
set();
}public void sey(){
// this causes the error Button button= (Button)findViewById(R.id.readyButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nameField = (EditText) findViewById(R.id.NameField);
numberField = (EditText)findViewById(R.id.Number);
name = nameField.getText().toString();
number =numberField.getText().toString();
MainFrame frame = new MainFrame();
frame.adName(name,place);
frame.adNumber(number, place);
((Activity) context).setContentView(R.layout.adnumber);
}
});
}
and here is the error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2072)
at oo.seniorlauncher.com.o_seniorlauncher.newNumber.set(newNumber.java:60)
at oo.seniorlauncher.com.o_seniorlauncher.newNumber.run(newNumber.java:58)
at oo.seniorlauncher.com.o_seniorlauncher.MainFrame$3.onClick(MainFrame.java:116)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
and here is my XMl-file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="oo.seniorlauncher.com.o_seniorlauncher.newNumber">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Syötä"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:height="100dp"
android:textSize="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="numero"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:width="250dp"
android:textSize="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ja nimi"
android:id="#+id/textView3"
android:height="100dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="66dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="50dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/NameField"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="50dp"
android:layout_centerVertical="true"
android:text="Nimi" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/Number"
android:layout_below="#+id/NameField"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="50dp"
android:text="Numero"
android:phoneNumber="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valmis"
android:id="#+id/readyButton"
android:layout_below="#+id/Number"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="75dp" />
</RelativeLayout>
I have tried almost everything and I don't know what causes the error.
You must set content view in onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adnumber);
}
Your activity should have an onCreate() method, where you initialize your views.
Like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adnumber);
//initialize your views here
nameField = (EditText) findViewById(R.id.NameField);
numberField = (EditText)findViewById(R.id.Number);
}
Dont listen to the others that are suggesting moving the setContentView() to onCreate()...It's truly the best but you can set it where ever you want...until you do that you just have a black screen. Some just dont know how Android works and just copied code from tutorials. The View hierarchies you want to build , inflate and fill into an activity can be done whenever you want so everything is gonna be all right ;)
That being said show us the xml files
edit: before you do any findviewbyid you must set the content vew of course so that you can reference the single objects inside your memory else you get your nullpointer exception :)
You must set the content view to the activity by setContentView("your_layout_ID") and code for get all widget from XML file should be put on onCreate function.