-3

我是 android 新手,以下代码旨在返回一个名为“err”的字符串,其中包含输入中的所有错误。不幸的是,该代码不起作用,并返回一个空字符串。

    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
4

1 回答 1

1

您需要将变量“错误”初始化为一个字符。尝试使用占位符文本,例如“错误 =”

如果这就是返回的所有内容,那么您就知道没有一个 if 语句是真的。

编辑:

您错误地将 EditText 声明为 TextView。编译器不会接受这一点,因为它被转换为 TextView,这是完全可能的。

您从 TextView 接收看似随机文本的原因是因为 TextView 方法“getText()”返回一个 CharSequence。

编辑2:

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)

第 41 行是Txterr = (EditText)findViewById(R.id.Txterr);//represents the Id text.

因此,现在您将 TextView 转换为 EditText。评估这意味着什么并做出正确的改变。

于 2013-10-30T16:02:38.967 回答