当我运行我的项目时,我遇到了问题,因为不幸的是app停止了。我已经发布了我的代码。我想用sq-lite数据库连接我的应用程序。你能告诉我工作模式吗?我只是在执行期间存储和检索值。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/sign_in"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Sign In"
android:onClick="login_sigin" />
<Button
android:id="@+id/log_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:onClick="login_sigin"
android:text="Log In" />
</LinearLayout>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#CCCCCC"
android:text=" Welcome "
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#333333"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999" >
<RelativeLayout
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/phone2"
android:layout_alignRight="@+id/phone2"
android:layout_centerVertical="true"
android:addStatesFromChildren="true"
android:background="@android:drawable/edit_text"
android:baselineAligned="false"
android:gravity="center_vertical" >
<EditText
android:id="@+id/password2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/show_hide2"
android:background="@null"
android:ems="10"
android:maxLength="40"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
</RelativeLayout>
<EditText
android:id="@+id/phone2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rl"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Mobile No."
android:maxLength="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<Button
android:id="@+id/signin2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Sign in"
android:onClick="action"
android:textSize="20sp"/>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/rl"
android:layout_alignRight="@+id/rl"
android:layout_below="@+id/rl"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:onClick="action"
android:text="Continue.." />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999"
>
<EditText
android:id="@+id/email_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/phone"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Email id"
android:maxLength="40"
android:inputType="textEmailAddress" >
</EditText>
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/email_id"
android:layout_centerHorizontal="true"
android:ems="10"
android:maxLength="10"
android:hint="Mobile No."
android:inputType="number" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/phone"
android:layout_centerHorizontal="true"
android:ems="10"
android:maxLength="30"
android:hint="Name"
android:inputType="textPersonName">
<requestFocus />
</EditText>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/email_id"
android:layout_alignRight="@+id/email_id"
android:layout_below="@+id/email_id"
android:addStatesFromChildren="true"
android:background="@android:drawable/edit_text"
android:baselineAligned="false"
android:id="@+id/rl"
android:gravity="center_vertical" >
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/show_hide"
android:background="@null"
android:ems="10"
android:maxLength="40"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
</RelativeLayout>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Log in"
android:onClick="action"
android:textSize="20sp"/>
<Button
android:id="@+id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/rl"
android:layout_alignRight="@+id/rl"
android:layout_below="@+id/rl"
android:layout_marginTop="10dp"
android:text="Continue.."
android:onClick="action"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999">
<ImageButton
android:id="@+id/w_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/w_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/w_image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/w_image"
android:text="Welcome Mohsin"
android:gravity="center"
android:background="#444444"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
和我的Login.java
package com.example.datewithme;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Login extends Activity{
Intent i=null;
ImageView im=null;
EditText tv1,tv4;
boolean flag=false;
SQLiteDatabase db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
im=(ImageView)findViewById(R.id.show_hide2);
tv1=(EditText)findViewById(R.id.phone2);
tv4=(EditText)findViewById(R.id.password2);
db=openOrCreateDatabase("mydb", MODE_PRIVATE, null);
// db.execSQL("create table if not exists login(name varchar,mobile_no varchar,email_id varchar,password varchar,flag varchar)");
im.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(flag==false)
{
tv4.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
flag=true;
}
else
{
tv4.setInputType(129);
flag=false;
}
}
});
}
public void action(View v)
{
switch(v.getId())
{
case R.id.signin2:
i=new Intent(this,Signin.class);
startActivityForResult(i, 500);
overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
finish();
break;
case R.id.start:
String mobile_no=tv1.getText().toString();
String password=tv4.getText().toString();
if(mobile_no==null||mobile_no==""||mobile_no.length()<10)
{
show("Please Enter Correct mobile number.");
}
else if(password==null||password==""||password.length()<6)
{
show("Please Enter Correct Password.");
}
else
{
Cursor c=db.rawQuery("select * from login where mobile_no='"+mobile_no+"' and password='"+password+"'",null);
c.moveToFirst();
if(c.getCount()>0)
{
i=new Intent(this,Welcome.class);
startActivityForResult(i,500);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
db.close();
finish();
}
else
show("Wrong Password or Mobile number.");
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
public void show(String str)
{
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
}
和我的MainActivity
package com.example.datewithme;
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;
}
}
和我的登录.java
package com.example.datewithme;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Signin extends Activity{
Intent i=null;
ImageView im=null;
EditText tv1,tv2,tv3,tv4;
boolean flag=false;
SQLiteDatabase db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin);
im=(ImageView)findViewById(R.id.show_hide);
tv1=(EditText)findViewById(R.id.name);
tv2=(EditText)findViewById(R.id.email_id);
tv3=(EditText)findViewById(R.id.phone);
tv4=(EditText)findViewById(R.id.password);
db=openOrCreateDatabase("mydb", MODE_PRIVATE, null);
db.execSQL("create table if not exists login(name varchar,mobile_no varchar,email_id varchar,password varchar,flag varchar)");
im.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(flag==false)
{
tv4.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
flag=true;
}
else
{
tv4.setInputType(129);
flag=false;
}
}
});
}
public void action(View v)
{
switch(v.getId())
{
case R.id.login:
i=new Intent(this,Login.class);
startActivityForResult(i, 500);
overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
finish();
break;
case R.id.signin:
String name=tv1.getText().toString();
String email_id=tv2.getText().toString();
String mobile_no=tv3.getText().toString();
String password=tv4.getText().toString();
if(name==null||name==""||name.length()<3)
{
show("Please Enter Correct Name.");
}
else if(mobile_no==null||mobile_no==""||mobile_no.length()<10)
{
show("Please Enter Correct mobile number.");
}
else if(email_id==null||email_id==""||email_id.length()<10)
{
show("Please Enter Correct Email id.");
}
else if(password==null||password==""||password.length()<6)
{
show("Please Enter Strong Password.");
}
else
{
db.execSQL("insert into login values('"+name+"','"+mobile_no+"','"+email_id+"','"+password+"','nothing')");
i=new Intent(this,Welcome.class);
startActivityForResult(i, 500);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
db.close();
finish();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
public void show(String str)
{
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
}
和welcome.java
package com.example.datewithme;
import android.app.Activity;
import android.os.Bundle;
public class Welcome extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
}
}
// logcat错误
02-14 00:50:12.357: D/gralloc_goldfish(1045): Emulator without GPU emulation detected.
02-14 00:50:32.427: D/AndroidRuntime(1045): Shutting down VM
02-14 00:50:32.427: W/dalvikvm(1045): threadid=1: thread exiting with uncaught exception (group=0xb4af8b90)
02-14 00:50:32.457: E/AndroidRuntime(1045): FATAL EXCEPTION: main
02-14 00:50:32.457: E/AndroidRuntime(1045): Process: com.example.datewithme, PID: 1045
02-14 00:50:32.457: E/AndroidRuntime(1045): java.lang.IllegalStateException: Could not find a method login_sigin(View) in the activity class com.example.datewithme.MainActivity for onClick handler on view class android.widget.Button with id 'sign_in'
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.view.View$1.onClick(View.java:3801)
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.view.View.performClick(View.java:4424)
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.view.View$PerformClick.run(View.java:18383)
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.os.Handler.handleCallback(Handler.java:733)
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.os.Handler.dispatchMessage(Handler.java:95)
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.os.Looper.loop(Looper.java:137)
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-14 00:50:32.457: E/AndroidRuntime(1045): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 00:50:32.457: E/AndroidRuntime(1045): at java.lang.reflect.Method.invoke(Method.java:515)
02-14 00:50:32.457: E/AndroidRuntime(1045): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-14 00:50:32.457: E/AndroidRuntime(1045): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-14 00:50:32.457: E/AndroidRuntime(1045): at dalvik.system.NativeStart.main(Native Method)
02-14 00:50:32.457: E/AndroidRuntime(1045): Caused by: java.lang.NoSuchMethodException: login_sigin [class android.view.View]
02-14 00:50:32.457: E/AndroidRuntime(1045): at java.lang.Class.getConstructorOrMethod(Class.java:472)
02-14 00:50:32.457: E/AndroidRuntime(1045): at java.lang.Class.getMethod(Class.java:857)
02-14 00:50:32.457: E/AndroidRuntime(1045): at android.view.View$1.onClick(View.java:3794)
02-14 00:50:32.457: E/AndroidRuntime(1045): ... 11 more
02-14 00:50:37.817: I/Process(1045): Sending signal. PID: 1045 SIG: 9
02-14 00:50:41.567: D/gralloc_goldfish(1071): Emulator without GPU emulation detected.
02-14 00:50:46.827: D/AndroidRuntime(1071): Shutting down VM
02-14 00:50:46.827: W/dalvikvm(1071): threadid=1: thread exiting with uncaught exception (group=0xb4af8b90)
02-14 00:50:46.887: E/AndroidRuntime(1071): FATAL EXCEPTION: main
02-14 00:50:46.887: E/AndroidRuntime(1071): Process: com.example.datewithme, PID: 1071
02-14 00:50:46.887: E/AndroidRuntime(1071): java.lang.IllegalStateException: Could not find a method login_sigin(View) in the activity class com.example.datewithme.MainActivity for onClick handler on view class android.widget.Button with id 'log_in'
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.view.View$1.onClick(View.java:3801)
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.view.View.performClick(View.java:4424)
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.view.View$PerformClick.run(View.java:18383)
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.os.Handler.handleCallback(Handler.java:733)
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.os.Handler.dispatchMessage(Handler.java:95)
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.os.Looper.loop(Looper.java:137)
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-14 00:50:46.887: E/AndroidRuntime(1071): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 00:50:46.887: E/AndroidRuntime(1071): at java.lang.reflect.Method.invoke(Method.java:515)
02-14 00:50:46.887: E/AndroidRuntime(1071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-14 00:50:46.887: E/AndroidRuntime(1071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-14 00:50:46.887: E/AndroidRuntime(1071): at dalvik.system.NativeStart.main(Native Method)
02-14 00:50:46.887: E/AndroidRuntime(1071): Caused by: java.lang.NoSuchMethodException: login_sigin [class android.view.View]
02-14 00:50:46.887: E/AndroidRuntime(1071): at java.lang.Class.getConstructorOrMethod(Class.java:472)
02-14 00:50:46.887: E/AndroidRuntime(1071): at java.lang.Class.getMethod(Class.java:857)
02-14 00:50:46.887: E/AndroidRuntime(1071): at android.view.View$1.onClick(View.java:3794)
02-14 00:50:46.887: E/AndroidRuntime(1071): ... 11 more
02-14 00:55:13.447: D/gralloc_goldfish(1098): Emulator without GPU emulation detected.
02-14 00:55:17.007: D/AndroidRuntime(1098): Shutting down VM
02-14 00:55:17.007: W/dalvikvm(1098): threadid=1: thread exiting with uncaught exception (group=0xb4af8b90)
02-14 00:55:17.027: E/AndroidRuntime(1098): FATAL EXCEPTION: main
02-14 00:55:17.027: E/AndroidRuntime(1098): Process: com.example.datewithme, PID: 1098
02-14 00:55:17.027: E/AndroidRuntime(1098): java.lang.IllegalStateException: Could not find a method login_sigin(View) in the activity class com.example.datewithme.MainActivity for onClick handler on view class android.widget.Button with id 'sign_in'
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.view.View$1.onClick(View.java:3801)
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.view.View.performClick(View.java:4424)
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.view.View$PerformClick.run(View.java:18383)
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.os.Handler.handleCallback(Handler.java:733)
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.os.Handler.dispatchMessage(Handler.java:95)
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.os.Looper.loop(Looper.java:137)
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-14 00:55:17.027: E/AndroidRuntime(1098): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 00:55:17.027: E/AndroidRuntime(1098): at java.lang.reflect.Method.invoke(Method.java:515)
02-14 00:55:17.027: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-14 00:55:17.027: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-14 00:55:17.027: E/AndroidRuntime(1098): at dalvik.system.NativeStart.main(Native Method)
02-14 00:55:17.027: E/AndroidRuntime(1098): Caused by: java.lang.NoSuchMethodException: login_sigin [class android.view.View]
02-14 00:55:17.027: E/AndroidRuntime(1098): at java.lang.Class.getConstructorOrMethod(Class.java:472)
02-14 00:55:17.027: E/AndroidRuntime(1098): at java.lang.Class.getMethod(Class.java:857)
02-14 00:55:17.027: E/AndroidRuntime(1098): at android.view.View$1.onClick(View.java:3794)
02-14 00:55:17.027: E/AndroidRuntime(1098): ... 11 more
02-14 00:56:38.717: I/Process(1098): Sending signal. PID: 1098 SIG: 9
答案 0 :(得分:2)
从XML中的按钮中删除android:onClick="login_sigin"
和android:onClick="login_sigin"
<Button
android:id="@+id/sign_in"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="20sp"
android:onClick="login_sigin"
android:text="Sign In"/>
<Button
android:id="@+id/log_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:onClick="login_sigin"/>
因为你为此定义了onClick事件而你没有使用它。因此,XML中的按钮应该是这样的。
<Button
android:id="@+id/sign_in"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Sign In" />
<Button
android:id="@+id/log_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="Log In" />
错误地自我明确定义了
java.lang.IllegalStateException: Could not find a method login_sigin(View) in the activity class com.example.datewithme.MainActivity for onClick handler on view class android.widget.Button with id 'sign_in'
答案 1 :(得分:1)
LogCat输出:
java.lang.IllegalStateException: Could not find a method login_sigin(View) in the activity class com.example.datewithme.MainActivity for onClick handler on view class android.widget.Button with id 'sign_in'
02-14 00:50:32.457: E/AndroidRuntime(1045): Caused by: java.lang.NoSuchMethodException: login_sigin [class android.view.View]
Logcat本身为您提供了错误和解决方案的提示。尝试阅读和理解。
正如您在onClick
文件中为sign_in
和log_in
按钮声明了login.xml
方法
<Button
android:id="@+id/sign_in"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Sign In"/>
<Button
android:id="@+id/log_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:onClick="login_sigin"/>
<强> You must add method to your java file with a name given in onClick attribute.
强>
在您的代码中,您忘记在login_sigin()
文件中编写login.java
方法
将此方法添加到login.java
public void login_sigin(view v) {
// Actions
}