我是android新手,下面的代碼被設計爲返回一個名爲「err」的字符串,其中包含輸入中的所有錯誤。遺憾的是,代碼不起作用,並返回一個空字符串。Android代碼不起作用
package com.example.app;
//import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
//import android.widget.DatePicker;
import android.widget.TextView;
public class Second extends Activity implements OnClickListener {
CheckBox Facebook_chk;
EditText Facebook_name;
EditText Name;
EditText Id;
EditText Txterr;
EditText Pass;
Button v;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.second);
v = (Button) findViewById(R.id.Btn1);
v.setOnClickListener(this);
Facebook_chk = (CheckBox)findViewById(R.id.Cfbook);//Represents the facebook checkbox.
Facebook_name = (EditText)findViewById(R.id.Face);//represents the facebook text.
Name = (EditText)findViewById(R.id.Name);//represents the Name text.
Id = (EditText)findViewById(R.id.Id);//represents the Id text.
Txterr = (EditText)findViewById(R.id.Txterr);//represents the Id text.
Pass = (EditText)findViewById(R.id.Pass);//represents the Pass text.
// final DatePicker Date = (DatePicker)findViewById(R.id.Datepick);//represents the Pass text.
Facebook_chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(Facebook_chk.isChecked())
Facebook_name.setEnabled(true);
else
Facebook_name.setEnabled(false);
;
}
});
}
public String GetErr(){
String error="a";
if(Facebook_name.toString().equals("")&& Facebook_chk.isChecked())//check with title if not available.
{
error+="facebook account not entered/n";//also check if not available
}
if((Name.getText().toString().equals("")))
{
error+="Name not entered\n";
}
if(Id.toString().matches("[a-zA-Z]+") || Id.getText().toString().equals(""))
error+="Id entered is invalid\n";
if(Pass.toString().length()<5 || Pass.getText().toString().equals(""))
error+="Passwords must contain 5 or more digits\n";
// int day= Date.getDayOfMonth();
// int month = Date.getMonth();
// int year=Date.getYear();
//Calendar enter = Calendar.getInstance();
// Calendar today = Calendar.getInstance();
// enter.set(year,month,day);
// today.set(Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH);
//if((enter.getTime().before(today.getTime())))
// error+="Date entered either passed or not available.";
return error;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
{//Returns strings,if not empty w print it and stop.
if(v.getId()==R.id.Btn1)
{
if(v.getId() == R.id.Btn1){
String err = GetErr();//THE STRING OF ERROR RETRIEVED.
if(err!="a")
Txterr.setText(err);
else
Txterr.setText("all is well");
}
}
}
}
}
// TODO Auto-generated method stub
我會比高興更多關於此事的可能的解決方案的進一步指示。
錯誤:
11-09 03:04:21.331: D/dalvikvm(825): GC_FOR_ALLOC freed 39K, 9% free 2885K/3140K, paused 145ms, total 156ms
11-09 03:04:23.240: D/gralloc_goldfish(825): Emulator without GPU emulation detected.
11-09 03:11:33.671: D/dalvikvm(825): GC_FOR_ALLOC freed 164K, 11% free 3222K/3596K, paused 75ms, total 94ms
11-09 03:11:33.873: D/AndroidRuntime(825): Shutting down VM
11-09 03:11:33.880: W/dalvikvm(825): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-09 03:11:33.950: E/AndroidRuntime(825): FATAL EXCEPTION: main
11-09 03:11:33.950: E/AndroidRuntime(825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Second}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.os.Looper.loop(Looper.java:137)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-09 03:11:33.950: E/AndroidRuntime(825): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 03:11:33.950: E/AndroidRuntime(825): at java.lang.reflect.Method.invoke(Method.java:525)
11-09 03:11:33.950: E/AndroidRuntime(825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-09 03:11:33.950: E/AndroidRuntime(825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 03:11:33.950: E/AndroidRuntime(825): at dalvik.system.NativeStart.main(Native Method)
11-09 03:11:33.950: E/AndroidRuntime(825): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:33.950: E/AndroidRuntime(825): at com.example.app.Second.onCreate(Second.java:41)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.Activity.performCreate(Activity.java:5133)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-09 03:11:33.950: E/AndroidRuntime(825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-09 03:11:33.950: E/AndroidRuntime(825): ... 11 more
11-09 03:11:37.812: I/Process(825): Sending signal. PID: 825 SIG: 9
11-09 03:11:51.013: D/dalvikvm(868): GC_FOR_ALLOC freed 42K, 9% free 2885K/3144K, paused 48ms, total 51ms
11-09 03:11:51.410: D/gralloc_goldfish(868): Emulator without GPU emulation detected.
11-09 03:11:55.360: D/dalvikvm(868): GC_FOR_ALLOC freed 164K, 11% free 3221K/3596K, paused 60ms, total 65ms
11-09 03:11:55.600: D/AndroidRuntime(868): Shutting down VM
11-09 03:11:55.600: W/dalvikvm(868): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-09 03:11:55.660: E/AndroidRuntime(868): FATAL EXCEPTION: main
11-09 03:11:55.660: E/AndroidRuntime(868): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Second}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.os.Looper.loop(Looper.java:137)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-09 03:11:55.660: E/AndroidRuntime(868): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 03:11:55.660: E/AndroidRuntime(868): at java.lang.reflect.Method.invoke(Method.java:525)
11-09 03:11:55.660: E/AndroidRuntime(868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-09 03:11:55.660: E/AndroidRuntime(868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 03:11:55.660: E/AndroidRuntime(868): at dalvik.system.NativeStart.main(Native Method)
11-09 03:11:55.660: E/AndroidRuntime(868): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:55.660: E/AndroidRuntime(868): at com.example.app.Second.onCreate(Second.java:41)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.Activity.performCreate(Activity.java:5133)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-09 03:11:55.660: E/AndroidRuntime(868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-09 03:11:55.660: E/AndroidRuntime(868): ... 11 more
11-09 03:11:58.330: I/Process(868): Sending signal. PID: 868 SIG: 9
11-09 03:17:20.080: D/dalvikvm(948): GC_FOR_ALLOC freed 46K, 9% free 2885K/3148K, paused 58ms, total 65ms
11-09 03:17:20.680: D/gralloc_goldfish(948): Emulator without GPU emulation detected.
11-09 03:24:25.141: D/dalvikvm(948): GC_FOR_ALLOC freed 164K, 11% free 3221K/3596K, paused 85ms, total 99ms
11-09 03:24:25.340: D/AndroidRuntime(948): Shutting down VM
11-09 03:24:25.340: W/dalvikvm(948): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-09 03:24:25.421: E/AndroidRuntime(948): FATAL EXCEPTION: main
11-09 03:24:25.421: E/AndroidRuntime(948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Second}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.os.Looper.loop(Looper.java:137)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-09 03:24:25.421: E/AndroidRuntime(948): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 03:24:25.421: E/AndroidRuntime(948): at java.lang.reflect.Method.invoke(Method.java:525)
11-09 03:24:25.421: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-09 03:24:25.421: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 03:24:25.421: E/AndroidRuntime(948): at dalvik.system.NativeStart.main(Native Method)
11-09 03:24:25.421: E/AndroidRuntime(948): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:24:25.421: E/AndroidRuntime(948): at com.example.app.Second.onCreate(Second.java:41)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.Activity.performCreate(Activity.java:5133)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-09 03:24:25.421: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-09 03:24:25.421: E/AndroidRuntime(948): ... 11 more
11-09 03:24:28.120: I/Process(948): Sending signal. PID: 948 SIG: 9
你能發佈整個錯誤信息嗎? –
看起來這些陳述都不是真的。在這種情況下,您需要將錯誤變量初始化爲空白以外的內容,以確保代碼正常工作。也許:錯誤=「錯誤:」 - 首先? – LokiSinclair
問題是有些是正確的:例如,我輸入了空id,但字符串爲空(現在打印用於調試).i的確將錯誤更改爲值。 – user44088