簡體   English   中英

當我嘗試從EditText獲取文本時應用崩潰

[英]App crashes when i try to getText from EditText

我不知道為什么會崩潰,當我嘗試將aaa和bbb字符串設置為與我評論的代碼完全相同的值時,它會出現問題,但是當我嘗試從edittexts中獲取文本時會崩潰...

 EditText text1;
 EditText text2;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    OpemM = MediaPlayer.create(this, R.raw.battlefield);
    OpemM.start();
    gm1=(Button)findViewById(R.id.gm1);

    gm3=(Button)findViewById(R.id.gm3);
    am1=(Button)findViewById(R.id.am1);
    am2=(Button)findViewById(R.id.am2);
    am3=(Button)findViewById(R.id.am3);
    o3=(Button)findViewById(R.id.o3);
    text1 = (EditText)findViewById(R.id.t1);
    text2 = (EditText)findViewById(R.id.t2);

}

public void OnASD(View sender)
{
    /*String aaa = "aaa";
    String bbb = "bbb";*/
    String aaa =text1.getText().toString().trim();
    String bbb =text2.getText().toString().trim();

    Intent myIntent1 = new Intent(Menu.this, OnASameDevice.class);
     myIntent1.putExtra("player1", aaa);
     myIntent1.putExtra("player2", bbb);
     startActivity(myIntent1);
     finish();

} 

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/pozadina" >

 <Button
    android:id="@+id/am1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_marginTop="68dp"
    android:background="@drawable/dugme1"
    android:onClick="next"
    android:text="@string/am1" />

 <Button
    android:id="@+id/am3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"


    android:layout_marginTop="38dp"
    android:background="@drawable/dugme1"
    android:onClick="quit"
    android:text="@string/am3" />

 </LinearLayout>

game_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@drawable/pozadina">


<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/player1"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/t1"
    android:text="  "
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/player2"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/t2"
    android:text="  "
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>



<Button
    android:id="@+id/gm1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:background="@drawable/dugme1"
    android:onClick="OnASD" 
    android:text="@string/gm1" />

<Button
    android:id="@+id/gm3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="29dp"
    android:background="@drawable/dugme1"
    android:onClick="back"
    android:text="@string/gm3" />

日志:

06-15 16:51:10.879: W/ApplicationPackageManager(17269): getCSCPackageItemText()
06-15 16:51:11.060: D/dalvikvm(17269): GC_FOR_ALLOC freed 93K, 35% free 17209K/26192K, paused 16ms, total 17ms
06-15 16:51:11.130: I/dalvikvm-heap(17269): Grow heap (frag case) to 57.318MB for 38455036-byte allocation
06-15 16:51:11.260: I/Adreno-EGL(17269): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build:  (CL3869936)
06-15 16:51:11.260: I/Adreno-EGL(17269): OpenGL ES Shader Compiler Version: 17.01.11.SPL
06-15 16:51:11.260: I/Adreno-EGL(17269): Build Date: 01/17/14 Fri
06-15 16:51:11.260: I/Adreno-EGL(17269): Local Branch: 
06-15 16:51:11.260: I/Adreno-EGL(17269): Remote Branch: 
06-15 16:51:11.260: I/Adreno-EGL(17269): Local Patches: 
06-15 16:51:11.260: I/Adreno-EGL(17269): Reconstruct Branch: 
06-15 16:51:11.300: D/OpenGLRenderer(17269): Enabling debug mode 0
06-15 16:51:16.275: W/ApplicationPackageManager(17269): getCSCPackageItemText()
06-15 16:51:16.335: D/dalvikvm(17269): GC_FOR_ALLOC freed 19K, 15% free 54800K/63748K, paused 12ms, total 12ms
06-15 16:51:16.375: V/MediaPlayer-JNI(17269): native_setup
06-15 16:51:16.375: V/MediaPlayer(17269): constructor
06-15 16:51:16.375: V/MediaPlayer(17269): setListener
06-15 16:51:16.385: V/MediaPlayer-JNI(17269): setDataSourceFD: fd 56
06-15 16:51:16.385: V/MediaPlayer(17269): setDataSource(56, 4098303, 7988106)
06-15 16:51:16.385: V/MediaPlayer(17269): message received msg=8, ext1=0, ext2=0
06-15 16:51:16.385: V/MediaPlayer(17269): notify(8, 0, 0) callback on disconnected mediaplayer
06-15 16:51:16.405: V/MediaPlayer(17269): setVideoSurfaceTexture
06-15 16:51:16.405: V/MediaPlayer(17269): prepare
06-15 16:51:16.405: V/MediaPlayer(17269): message received msg=200, ext1=973, ext2=0
06-15 16:51:16.405: W/MediaPlayer(17269): info/warning (973, 0)
06-15 16:51:16.405: V/MediaPlayer(17269): callback application
06-15 16:51:16.405: V/MediaPlayer(17269): back from callback
06-15 16:51:16.405: V/MediaPlayer(17269): message received msg=5, ext1=0, ext2=0
06-15 16:51:16.405: V/MediaPlayer(17269): New video size 0 x 0
06-15 16:51:16.405: V/MediaPlayer(17269): callback application
06-15 16:51:16.405: V/MediaPlayer(17269): back from callback
06-15 16:51:16.405: V/MediaPlayer(17269): message received msg=1, ext1=0, ext2=0
06-15 16:51:16.405: V/MediaPlayer(17269): prepared
06-15 16:51:16.405: V/MediaPlayer(17269): signal application thread
06-15 16:51:16.405: V/MediaPlayer(17269): callback application
06-15 16:51:16.405: V/MediaPlayer(17269): back from callback
06-15 16:51:16.405: V/MediaPlayer(17269): prepare complete - status=0
06-15 16:51:16.405: V/MediaPlayer-JNI(17269): start
06-15 16:51:16.405: V/MediaPlayer(17269): start
06-15 16:51:16.475: V/MediaPlayer(17269): message received msg=6, ext1=0, ext2=0
06-15 16:51:16.475: V/MediaPlayer(17269): Received MEDIA_STARTED
06-15 16:51:16.475: V/MediaPlayer(17269): callback application
06-15 16:51:16.475: V/MediaPlayer(17269): back from callback
06-15 16:51:16.485: I/MediaPlayer(17269): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
06-15 16:51:16.485: E/MediaPlayer(17269): Should have subtitle controller already set
06-15 16:51:16.525: V/MediaPlayer-JNI(17269): getCurrentPosition: 0 (msec)
06-15 16:51:16.525: V/MediaPlayer(17269): isPlaying: 1
06-15 16:51:16.525: V/MediaPlayer-JNI(17269): isPlaying: 1
06-15 16:51:26.855: D/AndroidRuntime(17269): Shutting down VM
06-15 16:51:26.855: W/dalvikvm(17269): threadid=1: thread exiting with uncaught exception (group=0x41879da0)
06-15 16:51:26.875: E/AndroidRuntime(17269): FATAL EXCEPTION: main
06-15 16:51:26.875: E/AndroidRuntime(17269): Process: com.JMS.sinktheship, PID: 17269
06-15 16:51:26.875: E/AndroidRuntime(17269): java.lang.IllegalStateException: Could not execute method of the activity
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.view.View$1.onClick(View.java:3969)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.view.View.performClick(View.java:4633)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.view.View$PerformClick.run(View.java:19330)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.os.Handler.handleCallback(Handler.java:733)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.os.Looper.loop(Looper.java:157)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.app.ActivityThread.main(ActivityThread.java:5356)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at java.lang.reflect.Method.invoke(Method.java:515)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at dalvik.system.NativeStart.main(Native Method)
06-15 16:51:26.875: E/AndroidRuntime(17269): Caused by: java.lang.reflect.InvocationTargetException
06-15 16:51:26.875: E/AndroidRuntime(17269):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at java.lang.reflect.Method.invoke(Method.java:515)
06-15 16:51:26.875: E/AndroidRuntime(17269):    at android.view.View$1.onClick(View.java:3964)
06-15 16:51:26.875: E/AndroidRuntime(17269):    ... 11 more
06-15 16:51:26.875: E/AndroidRuntime(17269): Caused by: java.lang.NullPointerException
06-15 16:51:26.875: E/AndroidRuntime(17269):    at com.JMS.sinktheship.Menu.OnASD(Menu.java:83)
06-15 16:51:26.875: E/AndroidRuntime(17269):    ... 14 more
06-15 16:51:29.087: I/Process(17269): Sending signal. PID: 17269 SIG: 9

嘿,尼古拉,我認為您的設計本身是錯誤的,一個活動只能引用一個xml文件。 如果要為同一活動更改xml,則需要在運行時將其充氣。 在您的情況下,請復制所有textview的內容,並編輯來自game_menu.xml的文本,然后將其粘貼到您的activity_main.xml中,我認為這會起作用。 檢查您正在使用setContentView(R.layout.activity_main)的活動; 其中不包含您要訪問的editText。 所以我認為這就是您出錯的原因。

你的

text1.getText() or text1

一片空白。 所以

text1.getText().toString() 

給出NullPointerException。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM