I am developing my first andoid app and want to post some data to my php webservice. I have the following signup.xml form
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/signup_layout"
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/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Please Register To Get Started"
android:textSize="20sp" />
.
.
.
<Button
android:id="#+id/signupbutton"
style="#android:style/Widget.Material.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/phone"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/rg_bg"
android:text="Signup"
android:textColor="#ffffff"
android:textSize="20sp"
android:onClick="send" />
</RelativeLayout>
This is the corresponding code for the signup.java file
package com.example.abhi.myapplication2;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class signup extends ActionBarActivity {
EditText username,pass,cpass,mail,phn;
String uname,password,confirmpass,email;
int phone=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
cpass = (EditText)findViewById(R.id.comfirmpass);
mail = (EditText)findViewById(R.id.email);
phn = (EditText)findViewById(R.id.phone);
Button signup = (Button)findViewById(R.id.signupbutton);
public void send(View v)
{
//Some code will go here
}
}
I am reading tutorials and trying to code now the tutorial says that for the signup button to work I need to create the public void send(View V) but this line throws an error - Cannot Resolve Symbol View
Why is this error coming up, and in the situation above where all I want to do is post some data to my php backend when user clicks on the signup button what should be the correct way of solving this situation?
Try import android.view.View;
IDE usually takes care of imports though, are you not using Android Studio or Eclipse?
Related
I'm currently following the Android Studio "Build Your First App" tutorial (https://developer.android.com/training/basics/firstapp/starting-activity) and I can't seem to get the DisplayMessageActivity working. The variable "R.id.textView" doesn't seem to exist and I can't see any differences between the tutorial and my own code. I know this is just me being stupid somewhere but I can't pinpoint it.
Here's my code for DisplayMessageActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView); <-- Cannot resolve symbol 'textView'
textView.setText(message);
}
}
And MainActivity.java, which is trying to run it:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
When I try to run the app on the virtual device it doesn't crash, but the button which calls the DisplayMessage activity does nothing.
EDIT: As requested, the xml code for both activities:
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
I read the tutorial and I think they missed this part of code:
android:onClick="sendMessage"
so, the code should be like this:
<Button
android:id="#+id/button"
android:onClick="sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
and I think that's it, let me know if it helps
After looking more closely at #Vishal Naikawadi's comment, I realised what the problem was. R.id.textView is not some sort of variable, placeholder, or constant as I had assumed, it is literally getting the object with id "textView" (or so I think). I had accidentally changed the id of the object I was trying to reference in the visual editor, so there was no object with that id. I changed the id of the object back to the same as is referenced in the code and everything seems to be fine now.
Apologies to anyone whose time I've wasted with this, I'm really new to this concept in Java.
I am new to android dev and started learning from udacity course but the java code is not running It is showing errors(picture below). I am new at stack overflow too sorry I don't know how to properly post a question.
the java program(image to know the errors)
https://drive.google.com/file/d/1D-OSZX2oxuRWZD_Yb8x4WWUBhMykWQKq/view?usp=sharing
package com.example.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
display(1);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}`
the XML code`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity:"
android:padding="15dp"
/>
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_below="#id/quantity"
android:paddingLeft="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_below="#id/quantity_text_view"
android:onClick="submitOrder"
android:text="Order"/>
</RelativeLayout>`
enter image description here
v7 is no longer supported. Instead of v7 use androidx packages.
import androidx.appcompat.app.AppCompatActivity;
My XML code is as follows:
<?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:padding="16dp"
tools:context="com.example.dell.myapplication.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUANTITY"
android:paddingBottom="8dp"/>
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:paddingTop="8dp"
android:paddingBottom="8dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDER"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:onClick="submitOrder"/>
</LinearLayout>
My JAVA code is as follows:
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.dell.myapplication.R;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
display(1);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}
I am not getting any error on Android studio IDE but on uploading the APK file on my phone, it says "Application Error Unfortunately, My Application has stopped."
How do I rectify this? Please help.
Thanks in advance
I have just started working on Android Studio and I am not able to solve this problem. I Have been getting the uploaded errors for a long time. Here are the files I am using. I have checked the package name as well, so that is not the issue.
MainAcitivity.java
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
display(1);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}
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:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Quantity"
android:textAllCaps="true" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Order"
android:textAllCaps="true"
android:onClick="submitOrder" />
</LinearLayout>
Error messages
You probably didn't add the appropriate support lib dependency. Add the following line to the dependencies block in your module's build.gradle file:
compile 'com.android.support:appcompat-v7:24.2.0'
You should also read the Support Library Setup before using the support libs.
I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. 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"
tools:context=".MainActivity" >
<ListView
android:layout_width="300dp"
android:layout_height="fill_parent"
android:id="#+id/event_list"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/datePicker" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_alignBottom="#+id/datePicker"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Event"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/datePicker"
android:layout_toEndOf="#+id/event_list"
android:background="#android:color/holo_blue_light"
android:onClick="newEvent"
android:textSize="20dp"
android:textColor="#android:color/white" />
</RelativeLayout>
new_plan.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_name"
android:hint="Event Name"
android:inputType="textCapWords"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_date"
android:hint="Event Date" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:textSize="20dp"
android:textColor="#android:color/white" />
</LinearLayout>
MakePlan.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MakePlan extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
}
}
MainActivity.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEvent(View v){
Intent i = new Intent(this, MakePlan.class);
startActivity(i);}}
Please help, thank you.
Take a look on this google developers project. It's for animations but i think it also have the solution to your question.
https://developer.android.com/training/animation/layout.html
You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. This illustration assumes that you have a List of String objects.
EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...
You must have done something like this to setup your listView.
listView.setAdapter(new YourAdapter(list, this));
Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for e.g.
void OnButtonClick(View view){
String strToAdd = editText.getText().toString();
list.add(strToAdd);
adapter.notifyDataSetChanged();
}