I've been tring for 2 days to get this to work but I couldn't,I keep getting this exception com.parse.ParseRequest$ParseRequestException: invalid session token.
I'm not an experienced android developper and it's my first time with parse so I'd appreciate it if you can post a detailed answer.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parse.starter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<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="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".ParseApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="#string/parse_app_id" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="#string/parse_client_key" />
<activity
android:name=".ParseStarterProjectActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SignUp"
android:label="#string/title_activity_sign_up" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
SignUp.java
package com.parse.starter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
public class SignUp extends Activity {
private Button sign_up_b;
private TextView email;
private TextView password;
private TextView password_conf;
private TextView firstName;
private TextView lastName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
sign_up_b = (Button)findViewById(R.id.email_sign_up_button);
email = (TextView)findViewById(R.id.email);
password = (TextView)findViewById(R.id.password);
password_conf = (TextView)findViewById(R.id.confirm_password);
firstName = (TextView)findViewById(R.id.first_name);
lastName = (TextView)findViewById(R.id.last_name);
sign_up_b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
attemptSignUp();
}
});
}
private void attemptSignUp(){
// Create the ParseUser
ParseUser user = new ParseUser();
// Set core properties
user.setUsername(email.getText().toString());
user.setPassword(password.getText().toString());
user.setEmail(email.getText().toString());
// Set custom properties
user.put("lName", lastName.getText().toString());
user.put("fName", firstName.getText().toString());
// Invoke signUpInBackground
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(com.parse.ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
//Intent i = new Intent(getBaseContext(),Splash.class);
//startActivity(i);
} else {
e.printStackTrace();
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sign_up, 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);
}
}
signup.xml
<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="com.parse.starter.SignUp">
<ScrollView android:id="#+id/sign_up_form" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/sign_up_infos" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<EditText android:id="#+id/first_name" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="#string/prompt_first"
android:maxLines="1"
android:singleLine="true" />
<EditText android:id="#+id/last_name" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="#string/prompt_last"
android:maxLines="1"
android:singleLine="true" />
<EditText android:id="#+id/email" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="#string/prompt_email"
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/confirm_password" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="#string/prompt_confirm_password"
android:inputType="textPassword"
android:maxLines="1" android:singleLine="true" />
<Button android:id="#+id/email_sign_up_button" style="?android:textAppearanceSmall"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:text="#string/action_sign_up"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
I've tried the answer in this posts but I kept getting the same exception
com.parse.ParseRequest$ParseRequestException: invalid session token
For future reference all I had to do is uninstall the application from the emulator than run it again and the problem was solved.
You need to logout the user in Login\Signup view controllers.
Try this in viewDidLoad:
ParseUser.getCurrentUser().logOut();
Related
I am beginner in Android system and was trying to create a simple registration from. In this app I created all the needed code but Save button doesn't give any response when clicking.
Here is the code of registration form :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewName"
android:text="Student's Name :"
android:layout_marginTop="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewFather"
android:text="Father's Name :"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/txtViewName"
android:layout_marginTop="29dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewMother"
android:text="Mother's Name :"
android:layout_below="#+id/txtViewFather"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewAge"
android:text="Age :"
android:layout_below="#+id/txtViewMother"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewClass"
android:text="Class :"
android:layout_below="#id/txtViewAge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:id="#+id/txtEditName"
android:ems="10"
android:layout_toRightOf="#+id/txtViewName"
android:layout_above="#+id/txtViewFather"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:id="#+id/txtEditFather"
android:ems="10"
android:layout_toRightOf="#+id/txtViewFather"
android:layout_above="#+id/txtViewMother"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:id="#+id/txtEditMother"
android:ems="10"
android:layout_above="#+id/txtViewAge"
android:layout_toRightOf="#+id/txtViewMother"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/txtEditAge"
android:ems="10"
android:layout_above="#+id/txtViewClass"
android:layout_toRightOf="#+id/txtViewAge"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtEditClass"
android:layout_alignBottom="#id/txtViewClass"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnBack"
android:text="Back"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnSave"
android:text="Save"
android:layout_toLeftOf="#+id/btnBack"
android:layout_alignParentBottom="true" />
</RelativeLayout>
and here is the StudentDetail.java class
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import java.util.ArrayList;
public class StudentDetail extends AppCompatActivity implements android.view.View.OnClickListener {
Button btnSave;
Button btnBack;
EditText txtEditName;
EditText txtEditFather;
EditText txtEditMother;
EditText txtEditAge;
EditText txtEditClass;
private int _student_id = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry_form);
btnSave = (Button) findViewById(R.id.btnSave);
btnBack = (Button) findViewById(R.id.btnBack);
txtEditName = (EditText) findViewById(R.id.txtEditName);
txtEditFather = (EditText) findViewById(R.id.txtEditFather);
txtEditMother = (EditText) findViewById(R.id.txtEditMother);
txtEditAge = (EditText) findViewById(R.id.txtEditAge);
txtEditClass= (EditText) findViewById(R.id.txtEditClass);
btnSave.setOnClickListener(this);
btnBack.setOnClickListener(this);
_student_id = 0;
Intent intent = getIntent();
_student_id = intent.getIntExtra("student_Id", 0);
}
#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_main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view) {
if (view == findViewById(R.id.btnSave)){
StudentRepo repo = new StudentRepo(this);
Student student = new Student();
student.name = txtEditName.getText().toString();
student.father_name = txtEditFather.getText().toString();
student.mother_name = txtEditMother.getText().toString();
student.age = Integer.parseInt(txtEditAge.getText().toString());
student.student_class = txtEditClass.getText().toString();
student.student_id =_student_id;
if (_student_id==0){
_student_id = repo.insert(student);
Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
}
} else if(view== findViewById(R.id.btnBack)){
finish();
}
}
}
Here is the AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentregistration" >
<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.example.studentregistration.StudentDetail"
android:label="#string/title_activity_entry_form" >
</activity>
</application>
</manifest>
Now when I click on Save button nothing happens..
So can somebody help me out from this situation.
Thanks in advance.
Inside onClick function, use switch-case on the view Id as below:
#override
public void onClick(View view) {
switch(view.getId()) {
case R.id.btnSave:
// Your custom save code goes here ...
break;
case R.id.btnBack:
// ...
break;
}
}
Try this code:
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
StudentRepo repo = new StudentRepo(StudentDetail .this);
Student student = new Student();
student.name = txtEditName.getText().toString();
student.father_name = txtEditFather.getText().toString();
student.mother_name = txtEditMother.getText().toString();
student.age = Integer.parseInt(txtEditAge.getText().toString());
student.student_class = txtEditClass.getText().toString();
student.student_id =_student_id;
if (_student_id==0){
_student_id = repo.insert(student);
Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
}
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
instead of
btnSave.setOnClickListener(this);
btnBack.setOnClickListener(this);
Oh thanks to all of you to give time to my question and suggesting me to do to fix the bug.
But after reviewing all the code line to line I found that bug myself.
The bug was launching wrong activity in AndroidManifest.xml file.
To fix this I removed the tag from MainActivity tag and placed this to StudentDetail activity tag and everything started working fine.
The updated code was :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentregistration" >
<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" >
</activity>
<activity
android:name="com.example.studentregistration.StudentDetail"
android:label="#string/title_activity_entry_form" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks once again to all.
I am new to android. I want to create a app with different theme for all the activities. I searched a lot for this but i cant find the correct solution.
Here the code
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample.webservice" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.sample.webservice.Usermain"
android:label="#string/title_activity_usermain"
android:theme="#style/UserMainTheme"
android:parentActivityName="com.example.sample.webservice.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sample.webservice.MainActivity" />
</activity>
</application>
</manifest>
Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:background">#color/white</item>
<item name="android:windowBackground">#color/white</item>
<item name="android:colorBackground">#color/white</item>
</style>
<style name="UserMainTheme" parent="android:Theme.Holo.Light">
<item name="android:background">#color/white</item>
<item name="android:windowBackground">#color/white</item>
<item name="android:colorBackground">#color/white</item>
</style>
</resources>
activity_main.xml
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:fontFamily="sans-serif"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/username"
android:id="#+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/username"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/password"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/password"
android:layout_gravity="right"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login"
android:id="#+id/login_button"
android:layout_marginTop="#dimen/login_top_margin"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
activity_usermain.xml
<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="com.example.sample.webservice.Usermain">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity.java
package com.example.sample.webservice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button login = (Button) findViewById(R.id.login_button);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_usermain);
}
});
}
#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_main, 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);
}
}
Here the screen shot of output:
MainActivity Screen
Usermain Screen
the above two screens having the same theme. Please help me in this thanks in advance
I forgot to start activity with Intent. That's why the style is not applied.
have to change this
public void onClick(View v) {
setContentView(R.layout.activity_usermain);
}
with this one in MainActivity.java
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Usermain.class);
startActivity(intent);
}
Its working good.
brother i also made an activity that wasn't much complex either just to test this issue but i didn't need to initiate any code. as u have described in the answer. It is working fine for me. All i did was change my Android Manifest file a bit and styles.xml.
after only these 2 changes i was good to go
I am a novice and I'm just creating a simple Android app in which there are 2 buttons that take you to different pages when clicked, which have different functions.
However, my app crashes on start-up, simply saying
Unfortunately [app name] has stopped
and I don't understand
As I have barely made any changes to my MainActivity.java, the code seems to be well written, and eclipse displays no errors... Any help, please? I have searched around Stack, but I can't find anything specific to this...
Here is my MainActivity.java:
package com.example.helper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**Called upon when the user clicks the 'Abbreviations' button*/
public void listDefinitions (View view){
Intent intent = new Intent(this, ListDefinitions.class);
startActivity(intent);
}
/**Called upon when the user clicks the 'Assistant' button*/
public void openAssistant (View view){
Intent intent = new Intent(this, Assistant.class);
startActivity(intent);
}
}
Here is my activity_main.xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.legittest.MainActivity" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/intro_title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="21.5sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="#string/module_selection"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/definitions_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="34dp"
android:layout_marginTop="111dp"
android:text="#string/definitions"
android:onClick="listDefinitions" />
<Button
android:id="#+id/assistant_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/definitions_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/assistant"
android:onClick="openAssistant" />
<TextView
android:id="#+id/definitions_explainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/definitions_button"
android:layout_centerHorizontal="true"
android:text="#string/definitions_info"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/greycolor" />
<TextView
android:id="#+id/pa_explainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/definitions_explainer"
android:layout_below="#+id/assistant_button"
android:text="#string/assistant_info"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/greycolor" />
</RelativeLayout>
My ListDefinitions.java & Assistant.java are untouched at this point.
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.theocratichelper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<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=".ListDefinitions"
android:label="#string/title_activity_list_definitions"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.theocratichelper.MainActivity" />
</activity>
<activity
android:name=".PreachAssistant"
android:label="#string/title_activity_preach_assistant"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.theocratichelper.MainActivity" />
</activity>
</application>
</manifest>
It's your manifest, replace the "last" activity in your manifest by this :
<activity
android:name=".Assistant"
android:label="#string/title_activity_preach_assistant"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.theocratichelper.MainActivity" />
</activity>
Your class name is Assistant and not PreachAssistant i presume.
Just replace the last activity in Android Manifest by this.
<activity
android:name=".Assistant"
android:label="#string/title_activity_preach_assistant"
</activity>
I am writing an app that will eventually use Bluetooth to get a value for two devices' proximity to one another. I am currently trying to use buttons and onClick/onclicklistener etc to bring switch screens from the main UI to a log in or sign up page. Now my app is forcing close on start up and I'm not sure why. Thanks for any help!!
MainActivity:
package com.example.chirp;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter() ;
String status; {
if (bluetooth != null)
{
if (bluetooth.isEnabled())
{
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = mydevicename + ":" + mydeviceaddress;
}
else
{
status = "Bluetooth is not Enabled.";
}
}
Toast.makeText(this, status, Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginbutton = (Button) findViewById(R.id.button1);
loginbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.loginscreen);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void newMessage(View v){
Intent intent = new Intent(this,loginscreen.class);
startActivity(intent);
}
}
activity_main.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:background="#7FFFD4"
android:padding="20dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- <public void loginhere(View view) {
android:
}/>-->
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:text="#string/hello_world"
android:textSize="50sp"
android:typeface="serif" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:layout_gravity="center_horizontal"
android:text="Slide to Quick Find"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button2"
android:layout_width="396dp"
android:layout_height="100dp"
android:layout_marginLeft="75dp"
android:layout_marginTop="300dp"
android:textSize="50sp"
android:text="Sign Up" />
<Button
android:id="#+id/button1"
android:layout_width="396dp"
android:layout_height="100dp"
android:layout_marginLeft="75dp"
android:layout_marginTop="50dp"
android:textSize="50sp"
android:onClick="onclick"
android:text="Log In" />
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chirp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"
android:requiresSmallestWidthDp="320"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.chirp.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.example.chirp.loginscreen"
android:label="CHIRP"
/>
</application>
</manifest>
loginscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#7FFFD4"
android:padding="20dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".login" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Username" />
/
</LinearLayout>
Move this code inside your onCreate method:
if (bluetooth != null) {
if (bluetooth.isEnabled()) {
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = mydevicename + ":" + mydeviceaddress;
} else {
status = "Bluetooth is not Enabled.";
}
//etc...
Currently this code block is outside of any method of your activity and that is for sure not what you want to have.
Here is my login activity class where i want to do login after login it show the map but when i run the app it show unfortunately stopped login
loginActivity.java
package com.ivb.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity{
EditText edtemail,edtPassword;
Button btnLogin;
String strUsername,strPassword;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
edtemail = (EditText)this.findViewById(R.id.edt_email);
edtPassword = (EditText)this.findViewById(R.id.edt_passwrd);
btnLogin = (Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
strUsername = edtemail.getText().toString().trim();
strPassword = edtPassword.getText().toString().trim();
if(strUsername.equals("needin#gmail.com") && strPassword.equals("needin123"))
startActivity(new Intent(LoginActivity.this,LoginSuccess.class).putExtra("usr",(CharSequence)strUsername));
else
Toast.makeText(LoginActivity.this,"Invalid UserName or Password", Toast.LENGTH_LONG).show();
}
});
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
loginsuccess.java
package com.ivb.login;
import javax.security.auth.PrivateCredentialPermission;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
public class LoginSuccess extends Activity {
private GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
Intent in = getIntent();
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText("Welcome \n "+in.getCharSequenceExtra("usr"));
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap =((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ivb.login"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.ivb.login.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission android:name="com.ivb.login.permission.MAPS_RECEIVE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.ivb.login.LoginActivity"
android:label="#string/app_name"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.ivb.login.RegisterActivity"
android:label="Register New Account"></activity>
<activity android:name="com.ivb.login.LoginSuccess"></activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC-2jAJ7MEkho_gJv7KLeb-tHrU2zHDUQU" />
<uses-library android:name="com.google.android.maps"/>
</application>
</manifest>
second.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginSuccess" >
<LinearLayout android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip" >
<ImageView android:src="#drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
</LinearLayout>
<TextView
android:id="#+id/showmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Welcome"
android:textSize="20dip" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TextView>
</RelativeLayout>
login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:orientation="vertical"
android:padding="10dip" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="#drawable/logo1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textColor="#372c24" />
<EditText
android:id="#+id/edt_email"
android:layout_width="209dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:ems="10"
android:hint="Enter Email Address"
android:inputType="textEmailAddress"
android:singleLine="true"
android:typeface="normal" >
<requestFocus />
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password"
android:textColor="#372c24" />
<EditText
android:id="#+id/edt_passwrd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dip"
android:ems="10"
android:hint="Enter password"
android:inputType="textPassword"
android:password="true"
android:singleLine="true"
android:typeface="normal" />
<Button
android:id="#+id/btnLogin"
android:layout_width="133dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:background="#acd28a"
android:clickable="true"
android:gravity="center"
android:paddingLeft="15dip"
android:text="Login" />
<TextView
android:id="#+id/link_to_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip"
android:layout_marginTop="40dip"
android:gravity="center"
android:text="Need an account?sign up"
android:textColor="#0b84aa"
android:textSize="20dip" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
Change from
<TextView
android:id="#+id/showmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Welcome"
android:textSize="20dip" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TextView>
to
<TextView
android:id="#+id/showmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Welcome"
android:textSize="20dip" >
</TextView>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
this is because your < /TextView> was closed after fragment class so you got this error.
Also change here
startActivity(new Intent(LoginActivity.this,LoginSuccess.class).putExtra("usr",strUsername));
and also in your LoginSuccess Activity
Intent in = getIntent();
if (in.getStringExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText("Welcome \n "+in.getStringExtra("usr"));
}
That means you have to declare this part in your manifest file...
<uses-feature android:glEsVersion="0x00020000" android:required="true/>
and must use latest updated Google Play Service.