Android SeekBar Application - Unfortunately, project has stopped - java

I am new to Android dev and for my project, I am creating two seekbars. The first seekbar has a minimum of 50 and a maximum of 180 while the second seekbar has a minimum of 180 and a maximum of 400. I also added textview labels below each seekbar to show the current value of each. My code compiled fine without any errors and displayed fine on the Android emulator, but when I clicked on the slider to change the value of the seekbar, my app ended up crashing and saying, "Unfortunately, your project has stopped." Below is my code:
MainActivity.java
package com.example.myname.projectname;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.R.*;
public class MainActivity extends ActionBarActivity implements OnSeekBarChangeListener {
private SeekBar lowBar;
private SeekBar highBar;
private TextView lowtext;
private TextView hightext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializevariables();
lowBar = (SeekBar)findViewById(R.id.lowbar);
highBar = (SeekBar)findViewById(R.id.highbar);
lowtext = (TextView)findViewById(R.id.lowval);
hightext = (TextView)findViewById(R.id.highval);
lowBar.setOnSeekBarChangeListener(this);
highBar.setOnSeekBarChangeListener(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_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);
}
private void initializevariables(){
lowBar = (SeekBar) findViewById(R.id.lowbar);
highBar = (SeekBar) findViewById(R.id.highbar);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progress = 0;
int lowbarmin = progress + 50;
int highbarmin = progress + 180;
lowtext.setText(lowbarmin);
hightext.setText(highbarmin);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
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">
<EditText android:text="CGM Alarm System" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lowbar"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:indeterminate="false"
android:progress="0"
android:max="180"/>
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/highbar"
android:layout_below="#+id/lowbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="66dp"
android:layout_alignRight="#+id/lowbar"
android:layout_alignEnd="#+id/lowbar"
android:progress="0"
android:max="400"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="50"
android:id="#+id/lowval"
android:layout_below="#+id/lowbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="180"
android:id="#+id/highval"
android:layout_below="#+id/highbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.projectname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myname.projectname.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>
</application>
</manifest>
Thank you in advance!

Your seekbar's minimum value is 50, you can not increase +50;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
int lowbarmin = progress + 1;
int highbarmin = progress + 1;
lowtext.setText(lowbarmin);
hightext.setText(highbarmin);
}

Crash is probably because of these lines :
lowtext.setText(lowbarmin);
hightext.setText(highbarmin);
When you do this and the value of lowbarmin and highbarmin are integers, android looks for string resources with those values as their resourceId and it cannot find one.
So what you instead want to do is, pass string values to setText() :
lowtext.setText(Integer.toString(lowbarmin));
hightext.setText(Integer.toString(highbarmin));

Related

Why Save Button doesn't work in my Sample Android App

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.

Invalid session token on SignUp activity Android (Parse backend)

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();

Android - Adding different themes for different activity not being applied

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

When I am running my first android app using Genymotion emulator its giving error unfortunately "app name" has stopped. Please help me

I am giving the details of all my files below.Please check them and let me know where I am doing the mistake due to which the error that the "unfortunately "app name" has stopped" error is coming when I am running my first android app using android SDK. Since I am a beginner in android development Please explain the solution nicely.
My MainActivity.java file :
package com.example.sunny.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.greetings_view);
}
public void showGreetings(View view)
{
String message= "welcome to my app";
textView.setText(message);
}
#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);
}
}
My activity_main.xml file is as follows:
<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"
android:id="#+id/greetings_view">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="greetings here"
android:id="#+id/greetings_view"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="show greetings"
android:layout_below="#+id/greetings_view"
android:onClick="My button"
android:id="#+id/greetings_view"
/>
</RelativeLayout>
My AndroidManifest.xml file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sunny.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sunny.myfirstapp.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>
</application>
My logcat :
06-26 10:37:23.137 31265-31265/? D/dalvikvm﹕ Late-enabling CheckJNI
06-26 10:37:24.013 31265-31265/com.example.sunny.myfirstapp D/AndroidRuntime﹕ Shutting down VM
06-26 10:37:24.021 31265-31265/com.example.sunny.myfirstapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62be288)
06-26 10:37:24.025 31265-31265/com.example.sunny.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sunny.myfirstapp/com.example.sunny.myfirstapp.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
all your views have the same ID greetings_view, elements must have no ID (blank) or different IDs.
ps.: in the future, the relevant log cat is the Stacktrace, is the part that have the direct message related to your crash. The log you posted is just some random stuff from your phone.
just try like this , it will Work
package com.example.sunny.myfirstapp;
public class MainActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.greetings_view);
String message= "welcome to my app";
textView.setText(message);
}
}
Remove from android:id="#+id/greetings_view" Relative Layout , You given that id in Two places TextvieW and Relative Layout
simple textview 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=".MainActivity" >
<TextView
android:id="#+id/greetings_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>

Adding more buttons

I'm working on a app. Past days i'm trying to get more then one button to work on my page. But i can't figur it out. Can some one help? I'm new to Development :)
Layout (xml)
Activity_main
<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.rust.rustapp.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button"
android:onClick="button1Click"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="38dp"
android:text="Button"
android:onClick="button1Click"
/>
</RelativeLayout>
Java:
MainActivty:
package com.rust.rustapp;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
private void button1Click()
{
startActivity(new Intent(MainActivity.this, Intro.class));
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
button1Click();
break;
}
}
});
}
#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);
}
}
Intro.xml (Button1 is opening this page)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/test"
>
</LinearLayout>
Intro.java
package com.rust.rustapp;
import android.app.Activity;
import android.os.Bundle;
public class Intro extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rust.rustapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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=".Intro"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
The new layout name is: FunPage
Thx!
From the code you pasted for MainActivity, it looks like you are mixing two different ways to handle button clicks.
If you can to register the click listener from your layout xml, the method corresponding to the onClick attribute needs to be public and not declared as a method inside your button1 listener.
An alternative is to listen to click events solely with your Java code. If you do this, then remove the onClick attribute from your layout XML for that button. There are many, many examples out there.
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//TODO -- In your case, start the "Intro" activity
}
}
It might help if you extract that method to start the Intro activity to a method outside of the click listener (i.e., as a method of MainActivity).

Categories