简体   繁体   English

当我尝试从EditText获取文本时应用崩溃

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

I don't know why does it crash, when i try to set the aaa and bbb strings to an exact value like the code i commented, it worsk perectly but when i try to get text from my edittexts it chrashes... 我不知道为什么会崩溃,当我尝试将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: 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: 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" />

Logs: 日志:

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

hey Nikola i think your design it self is wrong one Activity can refer only one xml file. 嘿,尼古拉,我认为您的设计本身是错误的,一个活动只能引用一个xml文件。 if you want to change the xml for same activity you need to inflate it during runtime. 如果要为同一活动更改xml,则需要在运行时将其充气。 at your case copy your all textview's and edit text from game_menu.xml and paste it in your activity_main.xml i think this will work. 在您的情况下,请复制所有textview的内容,并编辑来自game_menu.xml的文本,然后将其粘贴到您的activity_main.xml中,我认为这会起作用。 Check out your activity you are using setContentView(R.layout.activity_main); 检查您正在使用setContentView(R.layout.activity_main)的活动; which does not contain the editText which you are acessing. 其中不包含您要访问的editText。 So i think that's the reason you are getting error. 所以我认为这就是您出错的原因。

Your 你的

text1.getText() or text1

is null. 一片空白。 So 所以

text1.getText().toString() 

gives NullPointerException. 给出NullPointerException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM