i'm making an app that has a sign in or log in button, the log in button works perfectly but the sign up button gives me a fatal error, making the AVD to stop running the app, this is the code of the sign up activity
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.signup_activity);
// Set up the signup form.
usernameView = (EditText) findViewById(R.id.username);
passwordView = (EditText) findViewById(R.id.password);
passwordAgainView = (EditText) findViewById(R.id.passwordAgain);
// Set up the submit button click handler
findViewById(R.id.action_button).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Validate the sign up data
boolean validationError = false;
StringBuilder validationErrorMessage =
new StringBuilder(getResources().getString(R.string.error_intro));
if (isEmpty(usernameView)) {
validationError = true;
validationErrorMessage.append(getResources().getString(R.string.error_blank_username));
}
if (isEmpty(passwordView)) {
if (validationError) {
validationErrorMessage.append(getResources().getString(R.string.error_join));
}
validationError = true;
validationErrorMessage.append(getResources().getString(R.string.error_blank_password));
}
if (!isMatching(passwordView, passwordAgainView)) {
if (validationError) {
validationErrorMessage.append(getResources().getString(R.string.error_join));
}
validationError = true;
validationErrorMessage.append(getResources().getString(
R.string.error_mismatched_passwords));
}
validationErrorMessage.append(getResources().getString(R.string.error_end));
// If there is a validation error, display the error
if (validationError) {
Toast.makeText(SignUpActivity.this, validationErrorMessage.toString(), Toast.LENGTH_LONG)
.show();
return;
}
// Set up a progress dialog
final ProgressDialog dlg = new ProgressDialog(SignUpActivity.this);
dlg.setTitle("Please wait.");
dlg.setMessage("Signing up. Please wait.");
dlg.show();
// Set up a new Parse user
ParseUser user = new ParseUser();
user.setUsername(usernameView.getText().toString());
user.setPassword(passwordView.getText().toString());
// Call the Parse signup method
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
dlg.dismiss();
if (e != null) {
// Show the error message
Toast.makeText(SignUpActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
} else {
// Start an intent for the dispatch activity
Intent intent = new Intent(SignUpActivity.this, DispatchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
}
});
}
private boolean isEmpty(EditText etText) {
if (etText.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}
private boolean isMatching(EditText etText1, EditText etText2) {
if (etText1.getText().toString().equals(etText2.getText().toString())) {
return true;
} else {
return false;
}
}
and here is the manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name=".ParseStarter">
<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:name=".SignUpOrLogInActivity"
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=".DispatchActivity"/>
<activity android:name=".LoginActivity" />
<activity android:name=".SignUpActivity" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
</application>
here is the logCat
Process: com.sebasdeldihotmail.mediocre11, PID: 2020
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sebasdeldihotmail.mediocre11/com.sebasdeldihotmail.mediocre11.SignUpActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.sebasdeldihotmail.mediocre11.SignUpActivity.onCreate(SignUpActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
here is the code of the class that has the button that calls the sign up activity
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.signinorsignup_activity);
// Log in button click handler
((Button) findViewById(R.id.login)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Starts an intent of the log in activity
startActivity(new Intent(SignUpOrLogInActivity.this, LoginActivity.class));
}
});
// Sign up button click handler
((Button) findViewById(R.id.signup)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Starts an intent for the sign up activity
startActivity(new Intent(SignUpOrLogInActivity.this, SignUpActivity.class));
}
});
}
}
and here is the layout of the sign up
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#67AAE4">
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_username"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="#+id/passwordAgain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password_again"
android:imeActionLabel="#string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
</LinearLayout>
and here is the layout where the sign up button appears
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#67AAE4">
<Button
android:id="#+id/signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign Up"/>
<Button
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"/>
<com.facebook.widget.LoginButton
android:id="#+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
Thank you very much for reading :) .
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
It appears your signup_activity.xml layout does not have a view with id action_button, and findViewById() returns null.
The view you are adding the onClickListener to, r.id.action_button, is not found, hence the NullPointerException.
So make sure you have it in the current view, signup_activity.xml.
More:
To make your 'onCreate'-method less chunky, and to avoid this to happen again, consider using android:onClick on your button in the layoutfile, signup_activity.xml.
<Button
android:id="#+id/action_button"
android:onClick="onSignUpClick"
...
/>
Then in SignUpActivity.java add the method:
public void onSignUpClick(View view) {
// add code here instead of creating onClickListener in onCreate...
}
Related
I'm trying to build an app and have a little bit of a problem.
Android Monitor :
08-31 07:41:59.705 10118-10118/com.example.user.rotateit E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.rotateit/com.example.user.rotateit.HasilActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.user.rotateit.HasilActivity.onCreate(HasilActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
HasilActivity.Java :
public class HasilActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hasil);
int choice = 0;
final Random random = new Random();
choice = random.nextInt(2)+0;
EditText et_black = (EditText) findViewById(R.id.et_black);
EditText et_white = (EditText) findViewById(R.id.et_white);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
String et_blackText = et_black.getText().toString();
String et_whiteText = et_white.getText().toString();
if (choice == 0) {
tv_result.setText(et_blackText);
}
if (choice == 1) {
tv_result.setText(et_whiteText);
}
}
}
XML (activity_hasil):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.example.user.rotateit.HasilActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/tv_result"/>
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView btn_start = (ImageView) findViewById(R.id.btn_start);
btn_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, HasilActivity.class);
startActivity(i);
}
});
}
}
XML (activity_main) :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="#42079D"
tools:context="com.example.user.rotateit.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/et_black"
style="#style/EditText1"
android:background="#0B0503"
android:textColor="#FEFCFB"
/>
<EditText
android:id="#+id/et_white"
style="#style/EditText1"
android:background="#FEFCFB"
android:textColor="#0B0503"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_start"
android:id="#+id/btn_start"
android:layout_gravity="center"/>
</LinearLayout>
</ScrollView>
MANIFEST.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.rotateit">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HasilActivity"></activity>
</application>
</manifest>
The app always stops working when I run the program.
This is the whole program. I'm pretty sure that the problem is in the editText.
Basically, i want the program itself be able to show what the user typed in, in random. So, there are two EditTexts, et_black and et_white. the user will typed in any text in both of the editText, than the program will show either et_black or et_white.
Just started learning android studio.
You have to create a String with the text inside the et_black TextEdit object. After that you can assign this String to the tv_result TextView
String et_blackText = et_black.getText().toString();
if (choice == 0 || choice == 1) {
tv_result.setText(et_blackText);
}
In addition, you have to create the following objects in your XML
EditText et_black = (EditText) findViewById(R.id.et_black);
EditText et_white = (EditText) findViewById(R.id.et_white);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
R.id.et_black,R.id.et_white,R.id.tv_result are not created in your HasilXML file
You are trying to put the EditText et_black as the text of the TextView.
I won't say anything else, just..carefully check your code!!
PS post you manifest.xml
I have just set an onClick method which is written in the activity class to be called whenever the button is clicked. Even before it enters the method, the app crashes.
Error message that I am getting when the floating action button is clicked;
java.lang.IllegalArgumentException: Expected receiver of type com.example.bob.vexteamqueuing.AdminControl,
but got android.support.v7.view.ContextThemeWrapper java.lang.IllegalArgumentException:
Expected receiver of type com.example.bob.vexteamqueuing.AdminControl, but got android.support.v7.view.ContextThemeWrapper
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
XML Code for the activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/NoActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_admin_control" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
android:onClick="createNewTournament"
android:clickable="true" />
AdminControl activity Java code:
public class AdminControl extends AppCompatActivity {
Firebase ref;
public static List <Tournament> tournaments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
if (tournaments == null){
tournaments = new ArrayList<>();
}
}
public void createNewTournament(View v) {
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
}
On Create - Tournament Creator
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament_creator);
/*b = (FloatingActionButton) findViewById(R.id.fab);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
beginTournament(v);
}
});*/
}
Manifest file - may not be necessary but just provided
<?xml version="1.0" encoding="utf-8"?>
<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:name=".VEXQueuing"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Initial">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AdminLogin"
android:label="#string/title_activity_admin_login"
android:parentActivityName=".Initial">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.bob.vexteamqueuing.Initial" />
</activity>
<activity android:name=".TournamentCreator" />
<activity
android:name=".AdminControl"
android:label="#string/title_activity_admin_control"
android:parentActivityName=".Initial"
android:theme="#style/NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.bob.vexteamqueuing.Initial" />
</activity>
</application>
Your problem should be solved removing your android:onClick="createNewTournament" event from your layout
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
android:clickable="true" />
And adding a listener to R.id.fab in your onCreate, like this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
if (tournaments == null){
tournaments = new ArrayList<>();
}
FloatingActionButton myFab = (FloatingActionButton)findViewById(R.id.fab);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewTournament(v);
}
});
}
The same problem happen in this question EditText OnClick Exception and was fixed using listener.
Hope this helps!!
The previous solutions (by #GueorguiObregon and #MuhammadFaisalHyder) work but were not the thing that I was wishing. I found out the problem came from setting the android:theme attribute to the view (in my case), and also is related to the AppCompat library (see this).
So I simply removed the android: namespace from this line (from the view's style):
<item name="android:theme">#style/someTheme</item>
and made it likes:
<item name="theme">#style/someTheme</item>
and it works fine.
The amazing thing is the problem is only on the high-level APIs (23 I tested) and on low-level APIs (16 and 19 I tested) both ways (with or without android: namespace) work.
Also, see #MateiSuica comment below if you want to insert theme directly to the element (without using a style).
why not doing it the typical way? Try normally like by Declaring
public class AdminControl extends AppCompatActivity {
//....
FloatingActionButton mFAB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
//....
mFAB = (FloatingActionButton) findViewById(R.id.fab);
mFAB.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
});
}
And use XML for floating button like this:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
/>
This is the common way and clean way to do it, hope i helped.
I'm trying to implement a login Activity. In login Activity, I have to enter Mobile number and password and then click the Login button.
Here is my activity_login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
tools:context="com.myayubo.LoginActivity"
android:fillViewport="false">
<RelativeLayout
android:id="#+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp">
<LinearLayout
android:id="#+id/LinearLayouttxtayubo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textAyubo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ayubo"
android:layout_gravity="center"
android:typeface="normal"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="35dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayouttxtexplorer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/LinearLayouttxtayubo"
android:orientation="vertical">
<TextView
android:id="#+id/textExplore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explore places nearby you"
android:layout_centerHorizontal="true"
android:typeface="serif"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:textSize="12dp"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:layout_marginTop="224dp"
android:layout_below="#+id/RelativeLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:id="#+id/mobileNo"
android:layout_width="250dp"
android:layout_height="30dp"
android:hint="Mobile Number"
android:textColorHint="#FFFFFF"
android:gravity="center"
android:inputType="phone"
android:layout_gravity="center"
android:background="#drawable/shape"
android:layout_marginTop="10dp"
android:textSize="15dp"
>
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="250dp"
android:layout_height="30dp"
android:hint="Password"
android:textColorHint="#FFFFFF"
android:inputType="textPassword"
android:layout_gravity="center"
android:background="#drawable/shape"
android:layout_marginTop="10dp"
android:gravity="center"
android:textSize="15dp"/>
<Button
android:id="#+id/loginBtn"
android:text="Login"
android:layout_width="260dp"
android:layout_height="38dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:alpha="0.5" />
<Button
android:id="#+id/fbbtn"
android:text="Connect with Facebook"
android:layout_width="250dp"
android:layout_height="26dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#ADD8E6"
android:alpha="0.5"
/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fbbtn"
android:layout_marginTop="20dp"
android:text="or Sign Up"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
And Here is my LoginActivity.java
package com.myayubo;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.myayubo.services.MessageHandler;
import com.myayubo.services.ServiceHandler;
import org.json.JSONObject;
public class LoginActivity extends ActionBarActivity implements View.OnClickListener {
public final static String URL = "";
public static String Uid;
private EditText contact;
private EditText password;
private Button login;
private Button fbBtn;
private TextView signUp;
private boolean errorStatus;
private ServiceHandler sh = new ServiceHandler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
contact = (EditText) findViewById(R.id.mobileNo);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.loginBtn);
fbBtn = (Button) findViewById(R.id.fbbtn);
signUp = (TextView) findViewById(R.id.signUp);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!ServiceHandler.isOnline(getApplicationContext())){
MessageHandler.showMessage("No network connection",
getApplicationContext());
}
new Thread(new Runnable() {
public void run() {
Looper.prepare();
String mobile = contact.getText().toString();
String passwrd = password.getText().toString();
if (mobile.length() == 0){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage(
"Please Enter Your Contact Number",
getApplicationContext());
errorStatus = true;
}
});
;
}
if (passwrd.length() == 0){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage(
"Please Enter Your Password",
getApplicationContext());
errorStatus = true;
}
});
;
}
String jsonStr = null;
if (!errorStatus) {
if (!ServiceHandler.isOnline(getApplicationContext())) {
MessageHandler.showMessage("No network connection",
getApplicationContext());
} else {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// notify user you are online
try{
runOnUiThread(new Runnable() {
public void run() {
}
});
;
jsonStr = sh.makeServiceCall(URL + "/" + mobile + "/"
+ passwrd, ServiceHandler.GET);
System.out.println(URL + "/" + mobile + "/"
+ passwrd);
}
catch (Exception e){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage("No network connection",
getApplicationContext());
}
});
;
}
}
if (jsonStr != null) {
String status = "";
String msg = "";
try {
JSONObject jsonObj = new JSONObject(jsonStr);
runOnUiThread(new Runnable() {
public void run() {
}
});
;
if (jsonObj != null
&& jsonObj.has("status")) {
status = jsonObj.getString("status");
msg = jsonObj.getString("message");
if(jsonObj.has("uid"))
Uid = jsonObj.getString("uid");
System.out.println(jsonObj);
if (status.equals("OK")) {
Intent myIntent = new Intent(
getBaseContext(),
ExtractMenu.class);
startActivityForResult(myIntent, 0);
} else if (status.equals("ERROR")) {
final String errorMsg = msg;
runOnUiThread(new Runnable() {
public void run() {
MessageHandler
.showMessage(
errorMsg,
getApplicationContext());
}
});
;
} else {
runOnUiThread(new Runnable() {
public void run() {
MessageHandler
.showMessage(
"Oops..! something wrong with the service. Please try again Later.",
getApplicationContext());
}
});
;
}
}
} catch (Exception e) {
System.out
.println("Creation of json object failed");
}
}
}
}
}).start();
}
});
signUp.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(this, sign_up.class);
startActivity(intent);
}
}
Looks like something is wrong here. Because, when I'm trying to run this code, my app crashes. It shows the following error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.LoginActivity}: java.lang.NullPointerException
But, without intent for signUp button it works.
Here is my logcat.
11-05 10:36:58.359 27532-27532/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myayubo, PID: 27532
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.LoginActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myayubo.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Please some1 tell me what to do with this error.
Thaks in advance.
-Edit -
Here is the Manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myayubo"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--
To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect
option is required to comply with the Google+ Sign-In developer policies
-->
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
<activity
android:name=".Splash"
android:label="Ayubo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ExtractMenu"
android:label="#string/title_activity_extract_menu" >
</activity>
<activity
android:name=".sign_up"
android:label="#string/title_activity_sign_up" >
</activity>
</application>
</manifest>
you are getting NPE because you are trying to add view which is not in your xml file
signUp = (TextView) findViewById(R.id.signUp);
there is no text view in your xml with this id. check it out if you remove this your code will run perfectly
NullPointerException Because there isn't any TextView with id signUp in activity_login.xml layout.
In your onCreate Method
signUp = (TextView) findViewById(R.id.signUp);
But as your activity_login.xml does not contain TextView with id signUp it return null,
And again you try to set listener on that TextView which is null so NullPointerException
signUp.setOnClickListener(this);
Looks like you fogot to set id here,
<TextView
android:id="#+id/text2" //android:id="#+id/signUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fbbtn"
android:layout_marginTop="20dp"
android:text="or Sign Up"
android:layout_gravity="center"/>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can someone please assist me, no mater what i do i can't get around this problem of the app not working. I am using Android studio.
My Logcat
10-23 07:29:21.312 31399-31399/org.jaco.todo D/dalvikvm﹕ Late-enabling CheckJNI
10-23 07:29:21.512 31399-31402/org.jaco.todo D/dalvikvm﹕ GC_CONCURRENT freed 82K, 2% free 11049K/11271K, paused 11ms+0ms, total 16ms
10-23 07:29:21.532 31399-31399/org.jaco.todo D/AndroidRuntime﹕ Shutting down VM
10-23 07:29:21.536 31399-31399/org.jaco.todo W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62d9288)
10-23 07:29:21.540 31399-31399/org.jaco.todo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jaco.todo/org.jaco.todo.TodoActivity}: java.lang.IllegalStateException: To use this functionality, add this to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: To use this functionality, add this to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
at com.parse.Parse.requirePermission(Parse.java:495)
at com.parse.ParseCommandCache.runEventuallyAsync(ParseCommandCache.java:250)
at com.parse.ParseAnalytics.trackAppOpened(ParseAnalytics.java:61)
at com.parse.ParseAnalytics.trackAppOpened(ParseAnalytics.java:32)
at org.jaco.todo.TodoActivity.onCreate(TodoActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
10-23 07:29:58.452 31399-31399/? I/Process﹕ Sending signal. PID: 31399 SIG: 9
My TodoActivity.java
public class TodoActivity extends Activity implements OnItemClickListener {
private EditText mTaskInput;
private ListView mListView;
private TaskAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
Parse.initialize(this, "9YsgDgjMOqulfH9JYXVWZG5a6EBVf1SbMyz7gXH4", "CZ60kHo0V9cG190DAoIhB781BL3mRfdy6FSSPy6D");
ParseAnalytics.trackAppOpened(getIntent());
ParseObject.registerSubclass(Task.class);
ParseUser currentUser = ParseUser.getCurrentUser();
if(currentUser == null){
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
mAdapter = new TaskAdapter(this, new ArrayList<Task>());
mTaskInput = (EditText) findViewById(R.id.task_input);
mListView = (ListView) findViewById(R.id.task_list);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
updateData();
}
public void updateData(){
ParseQuery<Task> query = ParseQuery.getQuery(Task.class);
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK);
query.findInBackground(new FindCallback<Task>() {
#Override
public void done(List<Task> tasks, ParseException error) {
if(tasks != null){
mAdapter.clear();
for (int i = 0; i < tasks.size(); i++) {
mAdapter.add(tasks.get(i));
}
}
}
});
}
public void createTask(View v) {
if (mTaskInput.getText().length() > 0){
Task t = new Task();
t.setACL(new ParseACL(ParseUser.getCurrentUser()));
t.setUser(ParseUser.getCurrentUser());
t.setDescription(mTaskInput.getText().toString());
t.setCompleted(false);
t.saveEventually();
mAdapter.insert(t, 0);
mTaskInput.setText("");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.todo, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sign_in:
ParseUser.logOut();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
return true;
}
return false;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Task task = mAdapter.getItem(position);
TextView taskDescription = (TextView) view.findViewById(R.id.task_description);
task.setCompleted(!task.isCompleted());
if(task.isCompleted()){
taskDescription.setPaintFlags(taskDescription.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}else{
taskDescription.setPaintFlags(taskDescription.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
task.saveEventually();
}
}
my activity_todo.xml
<LinearLayout 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"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".TodoActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/task_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:hint="input_hint">
<requestFocus />
</EditText>
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="createTask"
android:text="submit_button" />
</LinearLayout>
<ListView
android:id="#+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.jaco.todo" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".TodoActivity"
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>
Any ideas?
Add your TodoActivity class with package name in your Manifest File and also add next permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
I am trying to add a facebook login to my app. Unfortunatly I am getting an error. I defined the app id in my strings.xml and add meta-data to my manifest. Although I added the facebook activity and the permission to use the Internet but the error is still there. I am of the opionen that there is a problem with the layout but I don't see one. Maybe you could help me.
EDIT: I changed the position of the initialization and the content view function but now I am getting a new error. I looked it up and found out that it is normally a problem with the app id but I have implemented the id using meta data in my manifest.
The error is the following:
10-07 10:06:36.249 4682-4748/de.homeproducts.ineedhelp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: de.homeproducts.ineedhelp, PID: 4682
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
at com.facebook.internal.Utility.queryAppSettings(Utility.java:802)
at com.facebook.login.widget.LoginButton$1.doInBackground(LoginButton.java:502)
at com.facebook.login.widget.LoginButton$1.doInBackground(LoginButton.java:499)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
This is my Activity class:
public ImageView view = null;
public LoginButton loginButton = null;
public CallbackManager callbackManager = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) this.findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich!", Toast.LENGTH_LONG);
}
#Override
public void onCancel() {
Toast toast = Toast.makeText(getApplicationContext(), "Login abgebrochen!", Toast.LENGTH_LONG);
}
#Override
public void onError(FacebookException e) {
Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen!", Toast.LENGTH_LONG);
}
});
And this 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView android:id="#+id/hello_world" android:src="#drawable/icon" android:layout_width="128dp"
android:layout_height="128dp" android:layout_centerHorizontal="true"/>
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
The manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.homeproducts.ineedhelp" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/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="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:label="#string/app_name" />
</application>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/app_id"/>
</manifest>
You have to write :
FacebookSdk.sdkInitialize(this.getApplicationContext());
Before:
setContentView(R.layout.activity_main);
See Facebook Sdk Has Not Been Initilized