-3

私はアンドロイドが初めてで、次のコードは、入力にすべてのエラーを含む「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

変数「エラー」を文字に初期化する必要があります。"Error =" などのプレースホルダー テキストを使用してみてください

それが返されたすべての場合、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 に答える