从edittext获取gettext错误?

时间:2014-06-08 14:29:52

标签: android string gettext

我正在创建一个简单的应用程序,当在对话框中键入某个字符串时更改图像但是当我尝试从编辑文本中获取字符串时(在对话框中)应用程序停止并发出错误

public class MainActivity extends Activity {

 ImageView im;

 final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button b = (Button) findViewById(R.id.start); 
      im = (ImageView) findViewById(R.id.imageView1);
    final MediaPlayer mp  = MediaPlayer.create(MainActivity.this, R.raw.hey);
    final Button chat = (Button) findViewById(R.id.chat);

    chat.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             final Dialog dialog = new Dialog(context);
             dialog.setContentView(R.layout.dialog);
             dialog.setTitle("Friendly Chat");
             TextView col = (TextView) dialog.findViewById(R.id.color);
             col.setText("Which is your favorite color? ");



            Button subb = (Button) dialog.findViewById(R.id.sub);
            subb.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    EditText  edt = (EditText) findViewById(R.id.ans);
                    String S = edt.getText().toString();
                    if (S.equals("blue")){
                        im.setImageResource(R.drawable.a1b);
                        Toast.makeText(getApplicationContext(), "I also love blue", Toast.LENGTH_SHORT).show();
                    }else if(S.equals("red")){
                        im.setImageResource(R.drawable.a1r);
                        Toast.makeText(getApplicationContext(), "I also love red", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("yellow")){
                        im.setImageResource(R.drawable.a1y);
                        Toast.makeText(getApplicationContext(), "I also love yellow", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("green")){
                        im.setImageResource(R.drawable.a1g);
                        Toast.makeText(getApplicationContext(), "I also love green", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("black")){
                        im.setImageResource(R.drawable.a1bl);
                        Toast.makeText(getApplicationContext(), "I also love black", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("white")){
                        im.setImageResource(R.drawable.a1wh);
                        Toast.makeText(getApplicationContext(), "I also love white", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("orange")){
                        im.setImageResource(R.drawable.a1o);
                        Toast.makeText(getApplicationContext(), "I also love orange", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("brown")){
                        im.setImageResource(R.drawable.a1br);
                        Toast.makeText(getApplicationContext(), "I also love brown", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("silver")){
                        im.setImageResource(R.drawable.a1s);
                        Toast.makeText(getApplicationContext(), "I also love silver", Toast.LENGTH_SHORT).show();

                    }else if (S.equals("purple")){
                        im.setImageResource(R.drawable.a1p);
                        Toast.makeText(getApplicationContext(), "I also love purple", Toast.LENGTH_SHORT).show();

                    }else{
                        Toast.makeText(getApplicationContext(), "Color does not exist please clearify i.e black,blue or green etc", Toast.LENGTH_LONG).show();
                    }

                      dialog.dismiss();
                }
            });
            dialog.show();
        }
    });
    im.setBackgroundResource(R.drawable.anim);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            AnimationDrawable anim 
            = (AnimationDrawable) im.getBackground();

                anim.stop();
                anim.start();
                mp.start();


        }
    });

}}

我的布局是

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pocketfriend.foryou.MainActivity"
 android:background="@drawable/bg"
>

<Button
    android:id="@+id/start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="130dp"
    android:text="Start" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="300dp"
    android:layout_centerInParent="true" />

<Button
    android:id="@+id/chat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chat" />

dialog.xml是

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
    android:id="@+id/ans"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:capitalize="none"
    android:layout_below="@+id/color"
    android:ems="10" />

<TextView
    android:id="@+id/color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="23dp"
     />

<Button
    android:id="@+id/sub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ans"
    android:text="Submit" />
    </RelativeLayout>

我的日志猫正在显示

  06-08 19:41:40.792: E/AndroidRuntime(9945): FATAL EXCEPTION: main
  06-08 19:41:40.792: E/AndroidRuntime(9945): java.lang.NullPointerException
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at    pocketfriend.foryou.MainActivity$1$1.onClick(MainActivity.java:52)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at android.view.View.performClick(View.java:4091)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at android.view.View$PerformClick.run(View.java:17072)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at android.os.Handler.handleCallback(Handler.java:615)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at android.os.Handler.dispatchMessage(Handler.java:92)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at android.os.Looper.loop(Looper.java:153)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at android.app.ActivityThread.main(ActivityThread.java:5026)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at java.lang.reflect.Method.invoke(Method.java:511)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
    06-08 19:41:40.792: E/AndroidRuntime(9945):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试从错误的视图访问EditText。 EditText存在于对话框中,而不是主要活动的上下文。

替换:

EditText  edt = (EditText) findViewById(R.id.ans);

使用:

EditText  edt = (EditText) dialog.findViewById(R.id.ans);
相关问题