Changing value in Edittext - java

I am making a little app were the user needs te scan a QRcode from a product to register how much of this product he used.
I want the user to have the possbility to put in the value from the code manually, becuase the QR is printed on a piece of papier that gets easily damaged.
I have implemented a barcodescanner using ZXing, and I am able to open this.
However, when I scan a code and want to assing the value of it to my EditText field, I get the folowing error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: be.jacops.juniorvandamme.verbruikpxs, PID: 29933
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at be.jacops.juniorvandamme.verbruikpxs.ActivityQr.handleResult(ActivityQr.java:59)
at me.dm7.barcodescanner.zxing.ZXingScannerView$1.run(ZXingScannerView.java:148)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
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:769)
This also results in the app crashing.
The code from this activity is the folowing:
package be.jacops.juniorvandamme.verbruikpxs;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ActivityQr extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
Button btnToUsage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
btnToUsage = findViewById(R.id.btnToUsage);
btnToUsage.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
EditText txtNumber = findViewById(R.id.txtNumber);
String strNumber = txtNumber.getText().toString();
if(TextUtils.isEmpty(strNumber)){
txtNumber.setError("Geef een Bobijnnummer in, of scan de QR code.");
return;
} else {
Intent i = new Intent(getApplicationContext(), ActivityUsage.class);
startActivity(i);
}
}
});
}
public void qrScanner(View view){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
public void onPause(){
super.onPause();
Intent i = new Intent(getApplicationContext(), ActivityUsage.class);
startActivity(i);
}
public void handleResult(Result rawResult){
final EditText txtNumber = findViewById(R.id.txtNumber);
String txtTemp = rawResult.getText().toString();
Log.e("QRcapture", txtTemp);
txtNumber.setText(txtTemp);
}
}
The XML file for this is the folowing:
<?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=".ActivityQr">
<Button
android:id="#+id/btnToUsage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Volgende"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.68" />
<Button
android:id="#+id/btnToScanner"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:layout_marginBottom="56dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Gebruik de scanner"
android:onClick="qrScanner"
app:layout_constraintBottom_toTopOf="#+id/btnToUsage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:text="Geef het bobijnnummer in."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
android:text="#string/bobijnnummer"
app:layout_constraintBottom_toTopOf="#+id/btnToScanner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.697" />
</android.support.constraint.ConstraintLayout>
I've been looking for a fix all day searching on multiple sites (one of which is here). I can't seem to find an answer.

try declaring your fields outside any particular method. In this case declare your EditText outside onCreate and assign the reference inside it rather than inside the listener. I think this is a scope error as the reference is not assigned at runtime when the call is made.

Related

java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button

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

There is a FATAL EXCEPTION:main in my project, MainActivity is not working

MainActivity is not working.
I tried to run it on Emulator but the application is forced to stop.
I'm trying to jump from MainActivity to UserActivity
MainActivity.java
package com.example.kanishk.coachingfinder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clickHere = (Button)findViewById(R.id.clickHere);
clickHere.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent mainIntent = new Intent(MainActivity.this,UserActivity.class);
startActivity(mainIntent);
}
});
}
}
LogCat (Arguments are on next line in many lines of codes)
04-12 03:44:51.709 4607-4607/com.example.kanishk.coachingfinder
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kanishk.coachingfinder, PID: 4607
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.kanishk.coachingfinder/
com.example.kanishk.coachingfinder.MainActivity}:
android.view.InflateException: Binary XML file line #10: Error inflating
class ImageView
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
Below is activity_main.xml file. I am not able to find out the problem in it.
<?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:background="#ccd9ff"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kanishk.coachingfinder.MainActivity">
<ImageView
android:layout_width="385dp"
android:layout_height="305dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#mipmap/cfinder"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.888"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.161" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Welcome to"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textColor="#91b4ff"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/textView3"
android:layout_width="364dp"
android:layout_height="96dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="COACHING FINDER"
android:textAlignment="center"
android:textColor="#ff7f50"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintVertical_bias="0.0"
tools:ignore="MissingConstraints" />
<Button
android:id="#+id/clickHere"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="79dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="132dp"
android:text="CLICK HERE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.427"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.018" />
</android.support.constraint.ConstraintLayout>
I suspect that this is due to a configuration issue in your XML layout. This issue had a similar exception:
InflateException: Binary XML file line #8: Error inflating class ImageView
I would check the source of your image, whether it exists in the correct location and what the file format is.
Also posting the XML would help a lot in debugging.
While I'm not entirely sure what you're trying to do, try doing this:
Intent mainIntent = new Intent(getApplicationContext(), UserActivity.class);
Here's my resource, since I've done this before:
https://www.youtube.com/watch?v=6ow3L39Wxmg
Skip to about 21:45 to take a look at a similar line of code.

I'm trying to populate a listview by using an intent [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm trying to have a user load my app to the main activity containing a listview, then move to the second activity and enter their "name" and "comments", pass it back to the main activity and show it in a toast as well as put it in the listview. However, when i try, my app crashes. Why?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground"
tools:context="com.example.MainActivity">
<Button
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Us"
android:layout_marginRight="46dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.982"
android:layout_marginEnd="46dp" />
<Button
android:id="#+id/comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Comments"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/contact"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.418"
app:layout_constraintVertical_bias="0.982"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ListView
android:id="#+id/ListViewMain"
android:layout_width="207dp"
android:layout_height="243dp"
android:layout_marginLeft="8dp"
android:textColor="#android:color/white"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintVertical_bias="0.253" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Comments"
android:textColor="#android:color/white"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.062"
app:layout_constraintHorizontal_bias="0.456" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button comments;
private Button contact;
ListView lv;
private String Name = "";
private String Comments = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
lv = (ListView) findViewById(R.id.ListViewMain);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
Name = extras.getString("NAME");
Comments = extras.getString("COMMENTS");
}
String[] values = new String[] { Name, Comments};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
lv.setAdapter(adapter);
}
private void initViews(){
comments = (Button) findViewById(R.id.comments);
contact = (Button) findViewById(R.id.contact);
}
private void initListeners(){
comments.setOnClickListener(this);
contact.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.comments:
Context context = getApplicationContext();
CharSequence text = "My name";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent intent2 = new Intent(getApplicationContext(), CommentsActivity.class);
startActivity(intent2);
break;
case R.id.contact:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, "myemail");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello!");
startActivity(Intent.createChooser(intent, "Send Email"));
break;
}
}
}
activity_comments.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"
android:background="#color/colorBackground"
tools:context="com.example.CommentsActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Comments:"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.378"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.087" />
<EditText
android:id="#+id/COMMENTS"
android:layout_width="316dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:inputType="text"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.432" />
<EditText
android:id="#+id/NAME"
android:layout_width="316dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:inputType="text"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.174" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.894" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="#android:color/white"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.14"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.079" />
</android.support.constraint.ConstraintLayout>
CommentsActivity.java
package com.example;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CommentsActivity extends AppCompatActivity implements View.OnClickListener {
private Button submit;
private EditText edNAME;
private EditText edCOMMENTS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
initViews();
initListeners();
edNAME = (EditText)findViewById(R.id.NAME);
edCOMMENTS = (EditText)findViewById(R.id.COMMENTS);
}
private void initListeners(){
submit.setOnClickListener(this);
}
private void initViews(){
submit = (Button) findViewById(R.id.submit);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit:
CharSequence text = edNAME.getText().toString() + " - " + edCOMMENTS.getText().toString();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("NAME", edNAME.getText());
intent.putExtra("COMMENTS", edCOMMENTS.getText());
startActivity(intent);
break;
}
}
}
error message:
12-22 14:31:58.765 1253-1253/com.example E/AndroidRuntime: FATAL
EXCEPTION: main
Process: com.example, PID: 1253
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String java.lang.Object.toString()' on a null object
reference
at
android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
at android.widget.AbsListView.obtainView(AbsListView.java:2372)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2171)
at android.view.View.layout(View.java:19586)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at
android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
at android.view.View.layout(View.java:19586)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19586)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at
android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:434)
at android.view.View.layout(View.java:19586)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19586)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19586)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
at android.view.View.layout(View.java:19586)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at
android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
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)
[ 12-22 14:31:58.766 1253: 1347 D/ ]
SurfaceInterface::setAsyncMode: set async mode 1
There must be at least one null value in the array that you passed in ArrayAdapter. Please log the values of the variables Name and Comments in MainActivity class before initializing the ArrayAdapter.

How to make ImageButton clickable in Android Studio?

I have done an immense amount of research and for whatever reason whatever I try I cannot get an ImageButton to be clickable in Android studio. I have tried numerous things but I must be missing something. I will past XML file below and then Java below that. When I put setOnClickListener method I get a cannot resolve message and same for when I do onClickListener. I would like the button to link to a webpage. Please help!
<?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.autismacademyed.www.autismacademy.AutismAcademy">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.173" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<ImageButton
android:id="#+id/imageButtonYellow"
android:layout_width="109dp"
android:layout_height="125dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#null"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:srcCompat="#mipmap/ic_yellowpuzzlepiece"
android:onClick="onClick"/>
</android.support.constraint.ConstraintLayout>
Here is Java:
package com.autismacademyed.www.autismacademy;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
ImageButton imageButtonYellow = (ImageButton)findViewById(R.id.imageButtonYellow);
imageButtonYellow.setOnClickListener(new View.onClickListener()
public void onClick (View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
Remove the setOnClickListener since you already specify in your view that the onclick function for the button is onClick. To avoid confusion rename your button android:onClick="onClick", like android:onClick="imageButtonOnClick".
And in your java code you can just use this
package com.autismacademyed.www.autismacademy;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
public void imageButtonOnClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
Just copy and paste this two file will work for you. You declare two click listener that's why its not working.
xml file
<?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.autismacademyed.www.autismacademy.AutismAcademy">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.173" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<ImageButton
android:id="#+id/imageButtonYellow"
android:layout_width="109dp"
android:layout_height="125dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#null"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:srcCompat="#mipmap/ic_yellowpuzzlepiece"
android:onClick="buttonClick"/>
</android.support.constraint.ConstraintLayout>
jAVAFILE
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
public void buttonClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}

NullPointerException when setting OnClickListener in method

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.

Categories

Resources