I get a LOT of errors from LogCat when I try running my app on an emulator. After looking around I think my main error is a the NullPointerException error which I assume is coming from calling the array of usernames from source and comparing them with the username posted on the form.
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
Context context = getApplicationContext();
#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;
}
public void hitSubmit(View button){
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
if(feedbackType.equals("Please Select")){
Toast.makeText(this, "Please select a country.", Toast.LENGTH_SHORT).show();
return;
}
final EditText fnField = (EditText) findViewById(R.id.FN);
String FN = fnField.getText().toString();
if(FN.equals("")){
Toast.makeText(this, "Please enter your first name.", Toast.LENGTH_SHORT).show();
return;
}
final EditText lnField = (EditText) findViewById(R.id.LN);
String LN = lnField.getText().toString();
if(LN.equals("")){
Toast.makeText(this, "Please enter your last name.", Toast.LENGTH_SHORT).show();
return;
}
final EditText phoneField = (EditText) findViewById(R.id.PN);
String PN = phoneField.getText().toString();
if(PN.equals("")){
Toast.makeText(this, "Please enter your phone number.", Toast.LENGTH_SHORT).show();
return;
}
final EditText Email1Field = (EditText) findViewById(R.id.EA);
String EA1 = Email1Field.getText().toString();
if(EA1.equals("")){
Toast.makeText(this, "Please enter an email address.", Toast.LENGTH_SHORT).show();
return;
}
final EditText Email2Field = (EditText) findViewById(R.id.EA2);
String EA2 = Email2Field.getText().toString();
if(!EA2.equals(EA1)){
Toast.makeText(this, "Your email address' do not match", Toast.LENGTH_SHORT).show();
return;
}
final EditText userField = (EditText) findViewById(R.id.User);
String user = userField.getText().toString();
if(!user.equals("")){
Toast.makeText(this, "Please enter a username", Toast.LENGTH_SHORT).show();
return;
}
Resources res = getResources();
String[] usernameArray = res.getStringArray(R.array.usernames);
int userint = usernameArray.length;
for(int x=0;x<userint;x++){
if(user.equals(usernameArray[x])){
Toast.makeText(this, "Username already exists.", Toast.LENGTH_SHORT).show();
return;
}
}
RadioButton rb1, rb2;
rb1 = (RadioButton) findViewById(R.id.button1);
rb2 = (RadioButton) findViewById(R.id.button2);
if(!rb1.isChecked() && !rb2.isChecked()){
Toast.makeText(this, "Please select a gender.", Toast.LENGTH_SHORT).show();
return;
}
finish();
}
public void hitClear(View button){
finish();
startActivity(getIntent());
}
}
--the xml code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Homework 1</string>
<string name="FN">First Name</string>
<string name="LN">Last Name</string>
<string name="PN">Phone Number</string>
<string name="EA">Email Address</string>
<string name="REEA">Re-enter Email</string>
<string name="User">Username</string>
<string name="Pw">Password</string>
<string name="Gender">Gender</string>
<string name="Country">Country</string>
<string name="action_settings">.</string>
<string name="clear">Clear</string>
<string name="submit">Submit</string>
<string name="female">Female</string>
<string name="male">Male</string>
<string name="country_prompt">Choose a country</string>
<string name="countrytype1">Malaysia</string>
<string name="countrytype2">United States</string>
<string name="countrytype3">Indonesia</string>
<string name="countrytype4">France</string>
<string name="countrytype5">Italy</string>
<string name="countrytype6">Singapore</string>
<string name="countrytype7">New Zealand</string>
<string name="countrytype8">India</string>
<string-array name="country_arrays">
<item>Please Select</item>
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
<string-array name="usernames">
<item>Bryan</item>
<item>John</item>
<item>Matt</item>
<item>Mike</item>
</string-array>
</resources>
Thanks.
EDIT: Adding activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
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" >
<EditText
android:id="#+id/FN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/FN"
android:inputType="textCapWords"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/LN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/LN"
android:inputType="textCapWords"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/PN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/PN"
android:inputType="phone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/EA"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:inputType="textEmailAddress"
android:hint="#string/EA"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/EA2"
android:layout_width="fill_parent"
android:inputType="textEmailAddress"
android:layout_height="49dp"
android:hint="#string/REEA"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/User"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/User"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/Pw"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/Pw"
android:inputType="textPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="53dp"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="#string/female" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="#string/male" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView9"
android:layout_width="0dip"
android:layout_height="49dp"
android:layout_weight="1"
android:text="#string/Country"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/SpinnerFeedbackType"
android:layout_width="210dp"
android:layout_height="41dp"
android:entries="#array/country_arrays"
android:prompt="#string/country_prompt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="hitSubmit"
android:text="#string/clear" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:onClick="hitClear"
android:text="#string/submit" />
</LinearLayout>
</LinearLayout>
You don't use Context context = getApplicationContext(); in first line of your activity. it's wrong. If you need get context, you could use itself.
Just use this or this.getBaseContext()
Also, if you checked the error message it shown you special line where occurs error. ex. at com.example.test.MyActivity.(MyActivity.java:47) shows line 47
1st of all why you need getapplication() context in activity, if you can use "this" or MainActivity.class .If want to use Context context = getApplicationContext(); then put it in Oncreate() because it may contain the null value and can cause null point exception wherever used.But keep in mind which/how/where context to be used. Context object depends on where it came from originally.
Below is a table of the common places an application will receive a Context, and in each case what it is useful for: .
For details about context check outWhat is context
Joining to all comments, you can also move the line
context = getApplicationContext();
to the onCreate method
i.e.
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
}
Related
Recently I decided to develop an app in Android. I have used this forum and other tools online to teach myself how to develop a simple app that asks the user for their name and answer few questions. My aim to allow the user to send the answers to any email of their choice. So far I have been able to achieve this:
From: ********#gmail.com
To: ********#gmail.com
Subject: Survey result for John Doe
Name: John Doe
Question 1: true
End of Survey.
My aim is to include in the email not the state of question 1 (i.e. "True") but instead I want to show the answer of their question. For Example, Question 1: B. 26-35.
I have searched online for the past couple of days but I have been having issues to find some help as I'm still new to programming. The closest thing I came across was this SO Question but it is not what I'm looking for. Any help or guidance would be highly appreciated.
MainActivity.java
package com.example.android.sampleupdrs;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
}
public void emailResult (View view)
{
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
RadioGroup radioQ1 = (RadioGroup) findViewById(R.id.radio_Q1);
final RadioButton checkedRadioQ1 = (RadioButton) radioQ1.findViewById(radioQ1.getCheckedRadioButtonId());
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
radioQ1.setOnCheckedChangeListener (new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged (RadioGroup group, int checkedQ1Id)
{
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedQ1Id);
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
if (isCheckedRadioQ1)
{
tv.setText("Checked: " +checkedRadioButton.getText());
}
}
});
String resultsEmail = createEmailSummary(name, isCheckedRadioQ1);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Survery result for " + name);
intent.putExtra(Intent.EXTRA_TEXT, resultsEmail);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
private String createEmailSummary (String name, boolean isCheckedRadioQ1) {
String resultsEmail = "Name: " + name;
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1;
resultsEmail += "\nEnd of Survey";
return resultsEmail;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
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"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/name_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Name"
android:inputType="textCapWords"/>
<TextView
android:text="#string/q1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textView1"/>
<RadioGroup
android:id="#+id/radio_Q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/q1b0_url"
android:text="#string/q1b0string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp" />
<RadioButton
android:id="#+id/q1b1_url"
android:text="#string/q1b1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b0_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b2_url"
android:text="#string/q1b2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b1_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b3_url"
android:text="#string/q1b3string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b2_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b4_url"
android:text="#string/q1b4string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b3_url"
android:layout_marginBottom="30dp"
android:gravity="start" />
</RadioGroup>
<TextView
android:text="#string/q2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textView2"/>
<RadioGroup
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nextQ1"
android:onClick="emailResult"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="right" />
</LinearLayout>
</ScrollView>
strings.xml
<resources>
<string name="app_name">Survey</string>
<string name="q1string">1. Select age group</string>
<string name="q1b0string">A. 18-25</string>
<string name="q1b1string">B. 26-35</string>
<string name="q1b2string">C. 36-45</string>
<string name="q1b3string">D. 46-59</string>
<string name="q1b4string">E. 60+</string>
</resources>
In MainActivity class,
you can get the text of the Radio button in this way:
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
text = "";
if (isCheckedRadioQ1) {
text = checkedRadioQ1.getText().toString();
tv.setText("Checked: " +checkedRadioButton.getText());
}
In method createEmailSummary do
resultsEmail += "\nQuestion 1 " + text;
instead of
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1
You'll have to send text as a parameter to createEmailSummary instead of isCheckedRadioQ1
If I understand correctly, what you need is checkedRadioButton.getText().toString() and not the boolean isCheckedRadioQ1.
This is my XML file. All text in button is large.
I used android:textAllCaps="false", but no result.In what may be a problem?
All literals are declared in the strings.xml.
Nowhere isn't even talk of the big letters. I think problem in invested LinearLayout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_color"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.3"
android:src="#drawable/ico_start" />
<EditText
android:id="#+id/feeld_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/login"
android:phoneNumber="false" />
<EditText
android:id="#+id/feeld_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="bottom"
android:padding="10dp">
<Button
android:id="#+id/button_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/register"
android:textSize="17dp" />
<Button
android:id="#+id/button_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:nestedScrollingEnabled="true"
android:text="#string/login"
android:textSize="17dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<resources>
<string name="app_name">Lesson 6. Registration Form</string>
<string name="register">Register</string>
<string name="login">Login</string>
<string name="password">Password</string>
package com.egoriku.lesson6registrationform;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String login = "egorikftp";
private final String password = "androidN";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView mainImage = (ImageView) findViewById(R.id.imageView);
final EditText loginText = (EditText) findViewById(R.id.feeld_login);
final EditText passwordText = (EditText) findViewById(R.id.feeld_password);
Button buttonRegister = (Button) findViewById(R.id.button_register);
Button buttonLogin = (Button) findViewById(R.id.button_login);
buttonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (loginText.getText().length() == 0) {
loginText.setError("Введите логин");
mainImage.setImageResource(R.drawable.ico_error);
}
if (passwordText.getText().length() == 0) {
passwordText.setError("Введите пароль");
mainImage.setImageResource(R.drawable.ico_error);
} else if (loginText.getText().toString().equals(login) && passwordText.getText().toString().equals(password)) {
Toast.makeText(getApplicationContext(), "Вы успешно вошли в систему!", Toast.LENGTH_LONG).show();
loginText.setText(null);
passwordText.setText(null);
mainImage.setImageResource(R.drawable.ico_ok);
} else {
Toast.makeText(getApplicationContext(), "Логин/Пароль введен неверно!", Toast.LENGTH_SHORT).show();
mainImage.setImageResource(R.drawable.ico_error);
loginText.setText(null);
passwordText.setText(null);
}
}
});
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (loginText.getText().length() == 0) {
loginText.setError("Введите логин");
mainImage.setImageResource(R.drawable.ico_error);
}
if (passwordText.getText().length() == 0) {
passwordText.setError("Введите пароль");
mainImage.setImageResource(R.drawable.ico_error);
} else {
//login = loginText.getText().toString();
//password = passwordText.getText().toString();
Toast.makeText(getApplicationContext(), "Вы успешно зарегистрированы! ", Toast.LENGTH_LONG).show();
loginText.setText(null);
passwordText.setText(null);
mainImage.setImageResource(R.drawable.ico_ok);
}
}
});
}
}
Screen of my program. Thanks.
The simplest method is to simply add this line inside your Button tag in xml
android:textAppearance="?android:attr/textAppearanceLarge"
Adding this line will show the text of your Button as you want whether capital or small depending upon the text in
android:text=" .... "
and change text size which fits perfectly
Button's widget in Android uses this style by default:
<item name="textAppearanceButton">#android:style/TextAppearance.Widget.Button</item>
Which enable by default the textAllCaps to true. As you can see in values/styles_base_text.xml of AppCompat theme, it could refer to the same style:
<style name="Base.TextAppearance.AppCompat.Button">
<item name="android:textSize">#dimen/abc_text_size_button_material</item>
<item name="textAllCaps">true</item>
<item name="android:textColor">?android:textColorPrimary</item>
</style>
Therefore, you need to override the current theme by your own. This question has been already resolved with this solution provided by #Galya. The steps are:
Create a new style for android:textAppearanceButton
Use the style's parent from the Base theme: #style/Base.TextAppearance.AppCompat.Button
Then, set caps to false with textAllCaps.
However, this will change all text caps buttons in the project. If you only need to handle these two buttons, I'd suggest you to create two TextViews and provide an onClickListener on them.
I want to display following output:
NAME : _
USN : _
ADDRESS : _
I want to display it in a single text view. When I tried using 2 text views it worked. But to use a single text view I used String builder, But when i click submit button, My application gets crashed. Why it's happening?
/*main.xml*/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="43dp"
android:text="#string/name" />
<EditText
android:id="#+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_name" />
<TextView
android:id="#+id/usn"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="30dp"
android:paddingLeft="52dp"
android:text="#string/usn" />
<EditText
android:id="#+id/edit_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_name"
android:layout_marginLeft="95dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_usn" />
<TextView
android:id="#+id/add"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="40dp"
android:paddingLeft="20.3dp"
android:text="#string/add" />
<EditText
android:id="#+id/edit_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_usn"
android:layout_marginLeft="95dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_add" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_centerHorizontal="true"
android:text="#string/submit"/>
</RelativeLayout>
/*layout file*/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Student Application Form</string>
<string name="name">NAME :</string>
<string name="edit_name">Enter your Name</string>
<string name="usn">USN :</string>
<string name="edit_usn">Enter the Usn</string>
<string name="add">ADDRESS :</string>
<string name="edit_add">Enter the Address</string>
<string name="submit">Submit</string>
<string name="number">1.</string>
<string name="action_settings">Settings</string>
<string name="title_activity_details">Details</string>
<string name="hello_world">Hello world!</string>
</resources>
/*Activity_details.xml*/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="#string/number"
android:textStyle="bold" />
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:paddingLeft="43dp"
android:text="#string/name"
android:textStyle="bold" />
<TextView
android:id="#+id/usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="15dp"
android:paddingLeft="52dp"
android:text="#string/usn"
android:textStyle="bold"/>
<TextView
android:id="#+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="20dp"
android:paddingLeft="20.3dp"
android:text="#string/add"
android:textStyle="bold" />
</RelativeLayout>
/*Main Activity.java*/
package com.example.assignment4;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button btn;
EditText etext1;
EditText etext2;
EditText etext3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext1 = (EditText) findViewById(R.id.edit_name);
etext2 = (EditText) findViewById(R.id.edit_usn);
etext3 = (EditText) findViewById(R.id.edit_add);
/** Called when the user clicks the Submit button */
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, DetailsActivity.class);
intent.putExtra("name", etext1.getText().toString());
intent.putExtra("usn", etext2.getText().toString());
intent.putExtra("address", etext3.getText().toString());
startActivity(intent);
}
}
I passed the intent from this class to Details class. But when I click submit,it gets crashed. But Before it was working properly. After I added string builder, it started giving problem.
/*Details.java*/
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class Details extends ActionBarActivity {
TextView text1;
TextView text2;
TextView text3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
//Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
//String Builder to append strings
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
//Receiving the intent
Intent intent=getIntent();
//Extracting the string
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("address");
//get the string from a textview
String stext1=getString(R.id.name);
sb1.append(stext1);
sb1.append(Name);
text1.setText(sb1);
String stext2=getString(R.id.usn);
sb2.append(stext2);
sb2.append(Usn);
text2.setText(sb2);
String stext3=getString(R.id.add);
sb3.append(stext3);
sb3.append(Address);
text3.setText(sb3);
}
}
I'm getting Run Time Exception and Null Pointer exception in Logcat
you forgot to initialize textviews.
change your code like below:
public class Details extends ActionBarActivity {
TextView text1;
TextView text2;
TextView text3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
text1 = (TextView)findViewById(R.id.name);
text2 = (TextView)findViewById(R.id.usn);
text3 = (TextView)findViewById(R.id.add);
//Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
//String Builder to append strings
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
//Receiving the intent
Intent intent=getIntent();
//Extracting the string
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("address");
//get the string from a textview
String stext1= getResources().getString(R.string.name);
text1.setText(Html.fromHtml("<b>" + stext1+ "</b>" +Name));
String stext2= getResources().getString(R.string.usn);
text2.setText(Html.fromHtml("<b>" + stext2+ "</b>" +Usn));
String stext3= getResources().getString(R.string.add);
text3.setText(Html.fromHtml("<b>" + stext3+ "</b>" +Address));
}
}
As Simple you have just declared your TextView s, not initialize it
TextView text1;
TextView text2;
TextView text3;
So initialize it on onCreate() method
text1 = (TextView)findViewById(R.id.name);
text2 = (TextView)findViewById(R.id.usn);
text3 = (TextView)findViewById(R.id.add);
try changing these:
String stext1=getString(R.id.name);
to
String stext1=(findViewById(R.id.name)).getText().toString();
String stext2=getString(R.id.usn);
to
String stext2=(findViewById(R.id.usn)).getText().toString();
String stext3=getString(R.id.add);
to
String stext3=(findViewById(R.id.add)).getText().toString();
I am new at Android and i have this error when trying to start the application
I will attach you some code and maybe you could help me
is there any problem my code ????
$Java code
package digicare.try2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity_try extends Activity {
int num1, num2 ;
Button sub,add,btn_total;
TextView txt_ans ;
int num_1 , num_2 , total;
EditText edtxt_num1,edtxt_num2,edtxt_total;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_try);
num1=1;
add=(Button) findViewById(R.id.btn_add);
sub =(Button) findViewById(R.id.btn_sub);
txt_ans=(TextView)findViewById(R.id.textView1);
edtxt_num1=(EditText) findViewById(R.id.editText_num1);
edtxt_num2=(EditText) findViewById(R.id.editText_num2);
edtxt_total=(EditText) findViewById(R.id.editText_total );
btn_total.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String num1_str = edtxt_num1.getText().toString();
String num2_str=edtxt_num2.getText().toString();
int num1_cal=Integer.parseInt(num1_str);
int num2_cal=Integer.parseInt(num2_str);
total=num1_cal + num2_cal;
edtxt_total.setText(total);
}
});
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
num1 ++;
txt_ans.setText("Your Answer is "+num1);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
num1--;
txt_ans.setText("Your total is "+num1);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_activity_try, menu);
return true;
}
}
$xml main activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity_try" >
<Button
android:id="#+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/btn_name" />
<Button
android:id="#+id/btn_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btn_add"
android:text="#string/btn_sub" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btn_sub"
android:text="#string/txt"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:ems="10"
android:inputType="number" />
<EditText
android:id="#+id/editText_num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginLeft="129dp"
android:layout_marginTop="117dp"
android:layout_toRightOf="#+id/editText_total"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView_num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText_num1"
android:layout_alignLeft="#+id/editText_num1"
android:layout_marginBottom="50dp"
android:text="#string/num1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText_num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginLeft="56dp"
android:layout_toRightOf="#+id/editText_num1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView_num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/editText_num1"
android:text="#string/num2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText_num1"
android:layout_alignBottom="#+id/editText_num1"
android:layout_alignLeft="#+id/editText_num2"
android:layout_marginLeft="43dp"
android:text="#string/btn_total" />
</RelativeLayout>
$xml strings
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Try</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="btn_name">add one </string>
<string name="btn_sub">minus one</string>
<string name="txt">your total is zero</string>
<string name="num1">1st Number</string>
<string name="num2">Second Number</string>
<string name="total">Total</string>
<string name="btn_total">Check Total</string>
</resources>
You have not initialized btn_total
btn_total =(Button) findViewById(R.id.button1);
You are probably gettting null pointer exception
Also
edtxt_total.setText(String.value(total));
I am new to android and making a simple application of a temperature converter. i had placed 2 text views and a button on the screen. i just wanted to saw that when i press the button an event comes or not when i run the application no event came after pressing the button so i put it in debug mode then it gives a message
Source Not Found:Source attachment does not contain the source for the file view.class
Here is my code:
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"
android:background="#color/red"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
style="#style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="136dp"
android:layout_marginLeft="94dp"
android:onClick="onclick"
android:text="#string/Button" />
<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_marginLeft="34dp"
android:layout_marginTop="38dp"
android:text="#string/Large_Text"
android:textStyle="italic"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="42dp"
android:text="#string/Large_Text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView2"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/button1"
android:ems="10"
android:inputType="numberSigned" />
</RelativeLayout>
Strings.Xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Converter</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Button">Convert</string>
<string name="Large_Text">Deg C</string>
<string name="Large_Text1">Deg F</string>
<color name="red">#D3D3D3</color>
</resources>
Main_activity.java
package com.example.converter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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;
}
}
I have created a classs1.java
package com.example.converter;
import android.app.Activity;
import android.widget.TextView;
public class Classs1 extends Activity{
public void onclick(){
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
}
U did not call event listener to ur view
public class Classs1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
btn = (Button) findViewById(R.id.ur_button_id);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
});
}
Edit:
Error : Source Not Found:Source attachment does not contain the source for the file view.class
Did u add classs1.java in your manifest file Plz check that too.