I am creating a simple CRUD using Android Studio with Sqlserver. When I add data into the database I got the error ConnectionRefusedError.
I have attached the code below what I tried so far. I have been trying to connect the database for 2 two days but it hasn't let me.
I add the jar file successfully
jtds-1.2.7.jar
and I get the IP address of the SQL server and its port
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View"
/>
<EditText
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
/>
<EditText
android:id="#+id/course"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="#+id/v1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View" />
</LinearLayout>
java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2;
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.title);
ed2 = findViewById(R.id.course);
b1 = findViewById(R.id.btn1);
b2 = findViewById(R.id.v1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
insert();
}
});
}
public void insert() {
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String connectionUrl = "jdbc:jtds:sqlserver://192.168.84.2; databaseName=skill; port =0; user=sa; password=admin123";
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement pst;
String title = ed1.getText().toString();
String description = ed2.getText().toString();
pst = con.prepareStatement("insert into course(coursename,fee)values(?,?)");
pst.setString(1, title);
pst.setString(2, description);
pst.executeUpdate();
Toast.makeText(this, "Success", Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed1.requestFocus();
} catch (Exception ex) {
Toast.makeText(this, "Record Fail", Toast.LENGTH_LONG).show();
}
}
The ConnectionRefused error simply means that the host computer is refusing a connection on that port. Perhaps the port is blocked on the host's firewall? If you have access to the host, you can simply unblock the port. Please report back with what you find.
Related
I am trying to do a POST Request with volley to interact with a MySQL Database through a local API. I am following a little tutorial (https://www.youtube.com/watch?v=M2KKIqrp8Y0) Everything seems to be good but the app send me a timeout error with "E/LB: fail to open file: No such file or directory" written in red in the Run of Android Studio.
Here some code:
demovolley/post.php
<?php
include "dbConnect.php";
$ten = $_POST['TEN'];
$email = $_POST['EMAIL'];
try{
//print ("Erreur%");
$conn = dbConnect();
$query = $conn->prepare("INSERT INTO sinhvien VALUES (null, '$ten','$email')");
$query->execute();
/*if($ligne = $query->fetch(PDO::FETCH_ASSOC)){
print (json_encode($ligne));
}*/
}catch(PDOException $e){
print "Erreur: " .$e->getMessage();
die();
}
?>
This query works I've tested it with Insomnia and the datas were inserted into my table
MainActivity.java
package com.mds.demovolley;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
EditText edtTen,edtMail;
Button btnGoi;
String URL_POST = "https://*my-ip*/demovolley/post.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtTen = (EditText) findViewById(R.id.editText);
edtMail = (EditText) findViewById(R.id.editText2);
btnGoi = (Button) findViewById(R.id.button);
btnGoi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InsertSV();
}
});
}
private void InsertSV(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,URL_POST, new Response.Listener<String>(){
#Override
public void onResponse(String response) {
Toast.makeText(getApplication(), response,Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error+"",Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String Ten = edtTen.getText().toString();
String MAIL = edtMail.getText().toString();
params.put("TEN",Ten);
params.put("EMAIL",MAIL);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I've read some things on forums but I'haven't found the solution
Could everyone help me?
(PS: Sorry if my english is not pretty good)
I also face this error that time I closed the project and later on after 2 days when I was started Android studio agian then it won't come again in short it automatically solve by system.
I'm connecting to external database from my android application using PHP & JSON.
it's a simple login system , it vérify if the username and password exist on the DB so it go to the Main2activity but if diffrent user and pass it show a error message
I am successfully connected to database and getting results in browser,but while coming to android i'm not getting any results it is showing :
Error Parsing Data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray.
Below is my code:
MainActivity.java :
package com.example.hussienalrubaye.mysqlsystemlogin;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void bulogin(View view) {
//get user name and password
EditText txtusername=(EditText)findViewById(R.id.etusername);
EditText txtpassword=(EditText)findViewById(R.id.etpassword);
new MyAsyncTaskresources().execute("http://l3pfe.byethost33.com/db.php?username="+txtusername.getText().toString()+"&password=" +txtpassword.getText().toString());
}
String result = "";
public class MyAsyncTaskresources extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... params) {
InputStream isr = null;
try{
String URL=params[0];
URL url = new URL( URL);
URLConnection urlConnection = url.openConnection();
isr = new BufferedInputStream(urlConnection.getInputStream());
}
catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
//parse json data
return null;
}
protected void onPostExecute(String result2){
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
s = s + "login info : " + json.getString("id") + " " + json.getString("username") + " " + json.getString("password");
break;}
if(s.length()>0){
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
Intent in=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(in);}
else
Toast.makeText(getApplicationContext(),"user name or password isnot correct",Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}
}
Main2Activity.java :
package com.example.hussienalrubaye.mysqlsystemlogin;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<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_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<EditText
android:hint="Enter user name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/etusername" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
<EditText
android:hint="Enter password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/etpassword" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/button"
android:onClick="bulogin" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
activity_main2.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="com.example.hussienalrubaye.mysqlsystemlogin.Main2Activity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main2" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
***content_main2.xml :***
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main2"
tools:context="com.example.hussienalrubaye.mysqlsystemlogin.Main2Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my control pannel"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
db.php :
<?php
mysql_connect("sql306.byethost33.com","b33_19513012","loool");//change server name //pass username according your settings
mysql_select_db("b33_19513012_pfe");// also chang the Mysql database name
$sql1=mysql_query("select * from admins WHERE username ='" . $_GET['username'] . "' and password='" . $_GET['password'] ."'"); //querstirng
if (!$sql1) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
while($row=mysql_fetch_assoc($sql1))
$output[]=$row;
print(json_encode($output));// this will print the output in json
mysql_close();
?>
Firt of all I'd like to warn you that mysql_connect is already deprecated, there is no mysql_connect anymore with PHP 7. Use mysqli instead.
As the mysql_connect doc says, you should specify the connection link. Check the $link variable.
$link = mysql_connect("sql306.byethost33.com", "b33_19513012", "loool");//change server name //pass username according your settings
mysql_select_db("b33_19513012_pfe", $link);// also chang the Mysql database name
$sql1 = mysql_query("select * from admins WHERE username ='" . $_GET['username'] . "' and password='" . $_GET['password'] . "'", $link); //querstirng
if (!$sql1) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error($link);
exit;
}
while ($row = mysql_fetch_assoc($sql1)) {
$output[] = $row;
}
print(json_encode($output));// this will print the output in json
mysql_close($link);
Good luck!
Source are :
MainActivity.java-
package com.amostrone.akash.safebrowser;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.amostrone.akash.safebrowser.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void search(View view) {
Intent intent = new Intent(this, Browser.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
if(message == null || message.trim().equals("")) {
Toast.makeText(this, "No Input", Toast.LENGTH_SHORT).show();
message = "http://www.android.com"; // TODO Add your own WEBSITE
}
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
MainActivity.xml`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
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.amostrone.akash.safebrowser.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textWebEmailAddress"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Search here." />
<Button
android:text="#string/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/button"
android:onClick="search" />
</RelativeLayout>
Browser.java
package com.amostrone.akash.safebrowser;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Browser extends AppCompatActivity {
static String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
Intent intent = getIntent();
message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if (!((message.substring(0, 11)).equals("http://www.") || (message.substring(0, 12)).equals("https://www.")))
check();
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl(message);
}
void check() {
// WWW
if(message.substring(0,4).equals("www."))
{
message = "http://" + message;
return;
}
// Google Search
message = "https://www.google.co.in/search?q=" + message + "&rct=j" ;
}
}
Browser.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_browser"
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.amostrone.akash.safebrowser.Browser">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
So my problem is that when i enter any text like "http://www.google.con.in" or just "www.google.co.in", everything works fine but when i enter just simple text like "stackoverflow" app crashes.It crashes even before system call the button "search" function "search(View view)".
Please help--
The problem may be that you are executing a substring on your message String, but it's too short.
As example:
message.substring(0, 11)
This will do a substring from index zero to 11, but what happens if the string have only a length of 4? There will be a exception.
You should extract the checks into an new method and do the length checks on the string before calling the substring method.
if(message.length() > 10){
message.substring(0,11);
}
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?
From looking online, I know there is some problem with my XML file but not sure what it is. I've tried remaking the entire button from scratch but the error persists. The following is the code in the activity.
package com.example.linked1n;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.linked1n.R;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
Button login;
int counter;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 3;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login = (Button)findViewbyId(R.Id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, ScreenAftLog.class );
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.Id.login:
loginClick();
break;
}
}
This is the XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.linked1n.MainActivity" >
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="loginClick"
android:text="#string/yayaya" />
<EditText
android:id="#+id/getEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:ems="10"
android:inputType="textWebEmailAddress" />
<EditText
android:id="#+id/getPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/getEmail"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</RelativeLayout>
Any help will be appreciated!
Edit: adding doesn't fix it
It is not R.Id.login, it should be R.id.login
You wrote ID in caps in your switch statement. id is a reserved word and needs to be in small letter, change the R.Id.login to R.id.login, then run :)
End tag is missing for the parent layout ...
Add </RelativeLayout> tag at the end. If the problem still persists, let us know.
Remove the last line - </EditText>
Below is your XML file which i build successfully. Try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.linked1n.MainActivity" >
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="loginClick"
android:text="Sample" />
<EditText
android:id="#+id/getEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:ems="10"
android:inputType="textWebEmailAddress" />
<EditText
android:id="#+id/getPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/getEmail"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPassword" />
<requestFocus />
</RelativeLayout>
java file
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
Button login;
int counter;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 3;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, ScreenAftLog.class );
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.id.login:
loginClick();
break;
}
}
}