2014-09-28 108 views
-1

我打算做的是,製作一個空的EditText,當玩家或用戶輸入我的答案字/數字時,它會自動說「正確」,它不會對其他任何事情做出反應, 它被命名爲「測試」,因爲一旦我得到它的工作,我會使用代碼爲我將創造的小猜謎遊戲:)EditText Android,[new to android]

這裏是java。

package com.example.test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    EditText who; 
    String name = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     name = who.getText().toString(); 
     if(name.matches("rumia")); 
     Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show(); 
    } 
} 

這裏的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@android:color/black"> 

    <EditText 
     android:id="@+id/who" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
     android:background="@android:color/white"/> 
</RelativeLayout> 

我嘗試過各種東西,它的工作(例如:Just純XML沒有設計(背景/圖片)),但不能得到它的工作..還是並稱不幸的是測試已經停止..

這裏的logcat的

09-28 00:18:42.106: E/AndroidRuntime(3250): Process: com.example.test, PID: 3250 
09-28 00:18:42.106: E/AndroidRuntime(3250): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:18:42.106: E/AndroidRuntime(3250):  at com.example.test.MainActivity.onCreate(MainActivity.java:20) 
09-28 00:18:50.806: E/WindowManager(372): Starting window AppWindowToken{b523b308 token=Token{b4f45670 ActivityRecord{b4f7af58 u0 com.example.test/.MainActivity t4}}} timed out 
09-28 00:20:10.586: E/AndroidRuntime(3311): Process: com.example.test, PID: 3311 
09-28 00:20:10.586: E/AndroidRuntime(3311): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:20:10.586: E/AndroidRuntime(3311):  at com.example.test.MainActivity.<init>(MainActivity.java:23) 
09-28 00:20:19.296: E/WindowManager(372): Starting window AppWindowToken{b525be10 token=Token{b5006af8 ActivityRecord{b5016630 u0 com.example.test/.MainActivity t5}}} timed out 
09-28 00:24:07.836: E/AndroidRuntime(3361): Process: com.example.test, PID: 3361 
09-28 00:24:07.836: E/AndroidRuntime(3361): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:24:07.836: E/AndroidRuntime(3361):  at com.example.test.MainActivity.<init>(MainActivity.java:21) 
09-28 00:24:16.826: E/WindowManager(372): Starting window AppWindowToken{b526fe78 token=Token{b4f9d330 ActivityRecord{b4d98c78 u0 com.example.test/.MainActivity t6}}} timed out 
09-28 00:28:19.376: E/AndroidRuntime(3407): Process: com.example.test, PID: 3407 
09-28 00:28:19.376: E/AndroidRuntime(3407): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:28:19.376: E/AndroidRuntime(3407):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:28:27.876: E/WindowManager(372): Starting window AppWindowToken{b5250f38 token=Token{b4e1ed28 ActivityRecord{b4ff97f8 u0 com.example.test/.MainActivity t7}}} timed out 
09-28 00:31:57.366: E/AndroidRuntime(3454): Process: com.example.test, PID: 3454 
09-28 00:31:57.366: E/AndroidRuntime(3454): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:31:57.366: E/AndroidRuntime(3454):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:32:06.026: E/WindowManager(372): Starting window AppWindowToken{b5276900 token=Token{b5225168 ActivityRecord{b4f8f4a8 u0 com.example.test/.MainActivity t8}}} timed out 
09-28 00:33:25.616: E/AndroidRuntime(3500): Process: com.example.test, PID: 3500 
09-28 00:33:25.616: E/AndroidRuntime(3500): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:33:25.616: E/AndroidRuntime(3500):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:33:34.496: E/WindowManager(372): Starting window AppWindowToken{b5257c88 token=Token{b52659a0 ActivityRecord{b4ff97f8 u0 com.example.test/.MainActivity t9}}} timed out 
09-28 00:36:09.836: E/AndroidRuntime(3547): Process: com.example.test, PID: 3547 
09-28 00:36:09.836: E/AndroidRuntime(3547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:36:09.836: E/AndroidRuntime(3547):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:36:18.316: E/WindowManager(372): Starting window AppWindowToken{b525c520 token=Token{b5244b60 ActivityRecord{b5016630 u0 com.example.test/.MainActivity t10}}} timed out 
09-28 00:42:10.286: E/AndroidRuntime(3583): Process: com.example.test, PID: 3583 
09-28 00:42:10.286: E/AndroidRuntime(3583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:42:10.286: E/AndroidRuntime(3583):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:44:05.186: E/AndroidRuntime(3629): Process: com.example.test, PID: 3629 
09-28 00:44:05.186: E/AndroidRuntime(3629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:44:05.186: E/AndroidRuntime(3629):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:44:13.996: E/WindowManager(372): Starting window AppWindowToken{b522efa0 token=Token{b51cc708 ActivityRecord{b5104bb8 u0 com.example.test/.MainActivity t12}}} timed out 
09-28 00:44:56.576: E/AndroidRuntime(3676): Process: com.example.test, PID: 3676 
09-28 00:44:56.576: E/AndroidRuntime(3676): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:44:56.576: E/AndroidRuntime(3676):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:45:05.136: E/WindowManager(372): Starting window AppWindowToken{b51c3570 token=Token{b4ff9098 ActivityRecord{b4ff88a0 u0 com.example.test/.MainActivity t13}}} timed out 
09-28 00:49:34.606: E/AndroidRuntime(3722): Process: com.example.test, PID: 3722 
09-28 00:49:34.606: E/AndroidRuntime(3722): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:49:34.606: E/AndroidRuntime(3722):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:49:43.276: E/WindowManager(372): Starting window AppWindowToken{b5257c88 token=Token{b51dda88 ActivityRecord{b502c6c0 u0 com.example.test/.MainActivity t14}}} timed out 
09-28 00:52:00.506: E/AndroidRuntime(3770): Process: com.example.test, PID: 3770 
09-28 00:52:00.506: E/AndroidRuntime(3770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:52:00.506: E/AndroidRuntime(3770):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:52:09.396: E/WindowManager(372): Starting window AppWindowToken{b5270b48 token=Token{b4e77b28 ActivityRecord{b4ff88a0 u0 com.example.test/.MainActivity t15}}} timed out 
09-28 00:53:46.216: E/AndroidRuntime(3817): Process: com.example.test, PID: 3817 
09-28 00:53:46.216: E/AndroidRuntime(3817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:53:46.216: E/AndroidRuntime(3817):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:53:55.206: E/WindowManager(372): Starting window AppWindowToken{b52681d8  token=Token{b4f93ed0 ActivityRecord{b5029f80 u0 com.example.test/.MainActivity t16}}} timed out 
09-28 00:55:21.036: E/AndroidRuntime(3865): Process: com.example.test, PID: 3865 
09-28 00:55:21.036: E/AndroidRuntime(3865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
09-28 00:55:21.036: E/AndroidRuntime(3865):  at com.example.test.MainActivity.onCreate(MainActivity.java:17) 
09-28 00:55:30.136: E/WindowManager(372): Starting window AppWindowToken{b5257c88 token=Token{b4f72f20 ActivityRecord{b4ff88a0 u0 com.example.test/.MainActivity t17}}} timed out 
+0

怎麼了'if(name.matches(「rumia」)); '多數民衆贊成在'沒有大括號'的if語句...只是由分號關閉..im驚訝編譯(?) – 2014-09-28 05:34:33

+0

是的,它確實..沒有錯誤和警告哦我試圖添加大括號但仍然相同 – Confused 2014-09-28 05:36:19

+0

看看在textwatcher在android .. – Lal 2014-09-28 05:42:25

回答

0
public class MainActivity extends Activity { 
    EditText who; 
    String name = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     who = (EditText)findViewById(R.id.who); 

     // Create some button for event then get value from Edit Text 
     name = who.getText().toString(); 
     if(name.matches("rumia")) 
     Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

哦..現在它的工作感謝 – Confused 2014-09-28 05:46:32

+0

你是否鑄造它,像 - 誰=(EditText)findViewById(R.id.who); – 2014-09-28 05:48:48

+0

只是想說謝謝那些幫助..所有工作現在..現在我只需要找出即使關閉或按下應用程序XD – Confused 2014-09-28 06:36:05

0

之前name = who.getText().toString();,你應該加上who = (EditText)findViewById(R.id.who);

+0

工作是的,謝謝。關閉到我的應用程序XD – Confused 2014-09-28 05:51:06

+0

只想說的感謝那些誰幫助..所有工作現在..現在我只需要找出如何分數變化將保持即使當你關閉或按下「返回」的應用程序XD – Confused 2014-09-28 06:34:14