This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 9 months ago.
My Homepage has a problem, the app failed to start when i run it. but, when i comment out the code on .Homepage in the AndroidManifest the app can run.
Error at Logcat:
2022-05-31 13:28:30.991 5606-5606/com.example.limbangfoodhuntfortourism E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.limbangfoodhuntfortourism, PID: 5606
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.limbangfoodhuntfortourism/com.example.limbangfoodhuntfortourism.Homepage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3016)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1716)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:232)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.example.limbangfoodhuntfortourism.Homepage.onCreate(Homepage.java:24)
at android.app.Activity.performCreate(Activity.java:6974)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3016)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1716)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:232)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
this is the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.limbangfoodhuntfortourism, PID: 27159
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.limbangfoodhuntfortourism/com.example.limbangfoodhuntfortourism.Homepage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
This is my Log in code:
package com.example.limbangfoodhuntfortourism;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Login extends AppCompatActivity {
EditText mEmail, mPassword;
Button mLoginButton;
TextView mCreateLinkSignUp, mForgotPassword;
FirebaseAuth fAuth;
ProgressBar progressBar;
ImageView mLogo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEmail = findViewById(R.id.email);
mPassword = findViewById(R.id.password);
progressBar = findViewById(R.id.progressBarLogin);
mLoginButton = findViewById(R.id.loginButton);
mCreateLinkSignUp = findViewById(R.id.signUpNow);
mForgotPassword = findViewById(R.id.forgotPassword);
fAuth = FirebaseAuth.getInstance();
mLoginButton.setOnClickListener((view) -> {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)) {
mEmail.setError("Email is Required.");
return;
}
if(TextUtils.isEmpty(password)) {
mPassword.setError("Password is Required.");
return;
}
if(password.length() < 6) {
mPassword.setError("Password must be >= 6 characters");
return;
}
progressBar.setVisibility(View.VISIBLE);
// authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener((task) -> {
if(task.isSuccessful()) {
Toast.makeText(Login.this, "Logged in Successfully!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Homepage.class));
} else {
Toast.makeText(Login.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
});
mCreateLinkSignUp.setOnClickListener((view) -> {
startActivity(new Intent(getApplicationContext(), Register.class));
});
mLogo = findViewById(R.id.imageView);
mLogo.setImageResource(R.drawable.main_logo);
mForgotPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final EditText resetMail = new EditText(view.getContext());
final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(view.getContext());
passwordResetDialog.setTitle("Reset Password?");
passwordResetDialog.setMessage("Enter Your Email To Received Reset Link.");
passwordResetDialog.setView(resetMail);
passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// extract the mail and send reset link
String mail = resetMail.getText().toString();
fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void unused) {
Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Login.this, "Error! Reset Link is Not Sent." + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// close the dialog
}
});
passwordResetDialog.create().show();
}
});
}
}
this is my Homepage:
package com.example.limbangfoodhuntfortourism;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
public class Homepage extends AppCompatActivity {
ImageView mLogo;
TextView mLogoutLink;
FirebaseAuth fAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
mLogoutLink = findViewById(R.id.Logout);
mLogo = findViewById(R.id.imageView);
mLogo.setImageResource(R.drawable.main_logo);
fAuth = FirebaseAuth.getInstance();
mLogoutLink.setOnClickListener((view) -> {
startActivity(new Intent(getApplicationContext(), Logout.class));
});
}
}
This is my AndroidManifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.limbangfoodhuntfortourism">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.LimbangFoodHuntForTourism"
tools:targetApi="31">
<activity
android:name=".Logout"
android:exported="true" />
<activity
android:name=".RestaurantDetails"
android:exported="true" />
<activity
android:name=".Homepage"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:exported="true" />
<activity
android:name=".Register"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true" />
</application>
</manifest>
try this in your login activity
mLogo = findViewById(R.id.imageView);
mLogo.setImageDrawable(ContextCompat.getDrawable(getActivity(),
R.drawable.main_logo));
lemme know if it works out for you
Related
I'm trying to implement sign up and login activities within my app. It's using the firebase login attributes and is already connected to firebase. My sign up screen loads and is functional, but when I click the button to do the log in with an already made account, the activity doesn't load.
Sign up java
package com.sjappdev.houstonrocketsteamapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class SignupActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private Button btnSignIn, btnSignUp, btnResetPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignIn = (Button) findViewById(R.id.sign_in_button);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
btnResetPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//startActivity(new Intent(SignupActivity.this, ResetPasswordActivity.class));
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(SignupActivity.this, LoginActivityB.class);
startActivity(i);
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
}
});
}
});
}
#Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
Login java
package com.sjappdev.houstonrocketsteamapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class LoginActivityB extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnLogin, btnReset;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivityB.this, MainActivity.class));
finish();
}
// set the view now
setContentView(R.layout.activity_login);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignup = (Button) findViewById(R.id.btn_signup);
btnLogin = (Button) findViewById(R.id.btn_login);
btnReset = (Button) findViewById(R.id.btn_reset_password);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivityB.this, SignupActivity.class));
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// startActivity(new Intent(LoginActivityB.this, ResetPasswordActivity.class));
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivityB.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
// there was an error
if (password.length() < 6) {
inputPassword.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(LoginActivityB.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(LoginActivityB.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
});
}
}
Here's the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.sjappdev.houstonrocketsteamapp">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#mipmap/rocketsicon"
android:label="#string/app_name"
android:roundIcon="#mipmap/rocketsicon"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".SignupActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="#string/app_name">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <data android:mimeType="image/png" /> -->
</intent-filter>
</activity>
<activity
android:name=".PlayersActivity"
android:label="#string/app_name">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <data android:mimeType="image/png" /> -->
</intent-filter>
</activity>
<activity
android:name=".StaffActivity"
android:label="#string/app_name">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <data android:mimeType="image/png" /> -->
</intent-filter>
</activity>
<activity
android:name=".Settings"
android:label="#string/app_name">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <data android:mimeType="image/png" /> -->
</intent-filter>
</activity>
<activity
android:name=".AboutActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="#string/app_name">
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <data android:mimeType="image/png" /> -->
</intent-filter>
</activity>
<activity
android:name=".EditPrefs"
android:label="#string/app_name">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <data android:mimeType="image/png" /> -->
</intent-filter>
</activity>
<activity
android:name=".LoginActivityB"
android:label="#string/app_name">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <data android:mimeType="image/png" /> -->
</intent-filter>
</activity>
</application>
</manifest>
My guess was the problem lied in the manifest, but it seems to be fine. Just not sure why the activity isn't working. It's error free. Could the error be my lack of toolbar or action bar?
Thanks,
Samuel
What is the error you are getting ?
what does your mainactivity class look like?
if the auth for the current user is NOT NULL, meaning, if there is auth, it will load your mainactivity class. It will skip all other code. try removing finish(); from this too, it might do something.
public class LoginActivityB extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnLogin, btnReset;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivityB.this, MainActivity.class));
finish();
}
This question already has answers here:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
(46 answers)
Closed 7 years ago.
i'm getting this error when i launch my app, how can i solve this?
12112-12112/com.javacodegeeks.androidqrcodeexample E/AndroidRuntime﹕
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.javacodegeeks.androidqrcodeexample/com.javacodegeeks.androidqrcodeexample.Login}:
java.lang.NullPointerException
this is my .xml file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javacodegeeks.androidqrcodeexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.javacodegeeks.androidqrcodeexample.AndroidBarcodeQrExample"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.javacodegeeks.androidqrcodeexample.Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and here my activity :
package com.javacodegeeks.androidqrcodeexample;
/**
* Created by Andre on 14/06/15.
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
EditText username = (EditText)findViewById(R.id.editText);
EditText password = (EditText)findViewById(R.id.editText2);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void login(View view) {
if (username.getText().toString().equals("admin") && password.getText().toString().equals("admin")) {
Toast toast = Toast.makeText(this, "CORRECT PW", Toast.LENGTH_LONG);
toast.show();
//correcct password
Intent i = new Intent(getApplicationContext(), AndroidBarcodeQrExample.class);
i.putExtra("Disco", username.getText().toString());
startActivity(i);
} else {
Toast toast = Toast.makeText(this, "UNCORRECT PW", Toast.LENGTH_LONG);
toast.show();
//wrong password
}
}
}
Move lines:
EditText username = (EditText)findViewById(R.id.editText);
EditText password = (EditText)findViewById(R.id.editText2);
inside onCreate
EditText username;
EditText password;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText)findViewById(R.id.editText);
password = (EditText)findViewById(R.id.editText2);
}
I started to learn Android a few days ago, and I'm stuck developing this app, what it does is basically connect to an API using volley, but when I try to run the app in my smartphone I get the following error:
-25 10:17:46.851 24476-24476/com.representemais E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rep, PID: 24476
java.lang.NullPointerException
at com.rep.app.principal.InicioActivity$AutenticacaoLocalTask$1.onResponse(InicioActivity.java:89)
at com.representemais.app.principal.InicioActivity$AutenticacaoLocalTask$1.onResponse(InicioActivity.java:84)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
This is the InicioActivity:
package com.rep.app.principal;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.rep.R;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class InicioActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AutenticacaoLocalTask mAutenticacaoLocalTask = new AutenticacaoLocalTask();
mAutenticacaoLocalTask.execute((Void) null);
}
private TextView txtDisplay;
RequestQueue queue = Volley.newRequestQueue(this);
public class AutenticacaoLocalTask extends AsyncTask<Void, Void, Boolean> {
#Override
protected Boolean doInBackground(Void... params) {
try {
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
String url = "http://192.168.1.15/rep-api/api/clients";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
txtDisplay.setText("Response => "+response.toString());
findViewById(R.id.progressBar1).setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
}
})
{
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0bDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");
return headers;
}
};
queue.add(jsonObjReq);
return true;
} catch (Exception e) {
Log.e("RM", e.getMessage());
return false;
}
}
#Override
protected void onPostExecute(final Boolean success) {
}
#Override
protected void onCancelled() {
}
}
And the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rep"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".app.principal.InicioActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".app.login.LoginActivity"
android:configChanges="keyboardHidden"
android:label="#string/app_name">
</activity>
<activity
android:name=".app.principal.MainActivity"
android:label="#string/app_name"></activity>
<activity android:name=".app.cliente.ClienteDetalheActivity"
android:label="#string/app_name"></activity>
<activity android:name=".app.login.LoginTelaBloqueada"
android:label="#string/app_name"></activity>
</application>
</manifest>
I think your forget to
setContentView(R.layout.your_layout);
You should set layout in your onCreate(...) method and then reference TextView.
Correct:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
.........
........
}
At a quick glance, it looks like you aren't setting the content view, which would cause findViewById to return null. Attempting to set the text in the non-existing TextView is what seems to be the direct cause of the crash.
This is my main page code:
package com.example.splasher;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
public static String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// hide statusbar of Android
// could also be done later
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText field= (EditText) findViewById (R.id.editText1);
field.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus) ((EditText)v).selectAll();
}
});
text=field.getText().toString();
try{
startActivity(new Intent("com.example.splasher.View"));
}
catch(ActivityNotFoundException e){
}
}
});
}
}
This is the code of my called activity:
package com.example.splasher;
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class View extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// hide statusbar of Android
// could also be done later
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.view);
// TextView textview=(TextView) findViewById (R.id.textView1);
// textview.setText(MainActivity.text);
}}
And this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.splasher"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".View"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.splasher.View" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I can't work out why I get this error. Any ideas? Names are all the same and with same upper, lower case letters. They are all described in manifest as well.
In your manifest, type the activity action in caps.
< action android:name="COM.EXAMPLE.SPLASHER.VIEW" / >
this is done for all activities except for the main activity
There is MainActivity.this.startActivity(new Intent(MainActivity.this, Views.class)); Views class with 's' !
there is no need of the intent filter which you have applied in the View activity..Just keep one intent filter in your MainActivity
Replace the start activity line code with following:
startActivity(new Intent(MainActivity.this,View.class));
Try to replace your startActivity call by MainActivity.this.startActivity in your MainActivity.
I think there is a scope trouble.
My app crashes when changing activity Here is my code,
I was following a tutorial on youtube, Everything seems to be exactly like his code. See if you can help
Everything seems to be inorder, I copyed everything exactly,
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.example.helloworld.MAINACTIVITY" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity:
package com.example.helloworld;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
display.setText("Your Total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
display.setText("Your Total is " + counter);
}
});
}
}
Splash.java:
package com.example.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.example.helloworld.MAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
Logcat:
07-19 20:35:36.068: E/AndroidRuntime(1442): FATAL EXCEPTION: Thread-74
07-19 20:35:36.068: E/AndroidRuntime(1442): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.helloworld.MAINACTIVITY }
07-19 20:35:36.068: E/AndroidRuntime(1442): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512)
07-19 20:35:36.068: E/AndroidRuntime(1442): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
07-19 20:35:36.068: E/AndroidRuntime(1442): at android.app.Activity.startActivityForResult(Activity.java:3190)
07-19 20:35:36.068: E/AndroidRuntime(1442): at android.app.Activity.startActivity(Activity.java:3297)
07-19 20:35:36.068: E/AndroidRuntime(1442): at com.example.helloworld.Splash$1.run(Splash.java:23)
Try changing:
Intent openStartingPoint = new Intent("com.example.helloworld.MAINACTIVITY");
To:
Intent openStartingPoint = new Intent(Splash.this, MainActivity.class);
I think the application crashes because you misspelled "DEFAULT" in the intent-filter.