2012-08-24 158 views
0

這是我的代碼,有什麼事嗎?Android:一個按鈕點擊事件,「意外停止」

運行結果「意外停止」。 它可以通過刪除OnClickListener時,也就是說,該按鈕點擊綁定是錯誤的?

gao.java

package cn.gao; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.*; 

public class gao<hellotwo> extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     Button btn=(Button)findViewById(R.id.go); 

     btn.setOnClickListener(new View.OnClickListener() 
     { 
       public void onClick(View v) 
       { 
         EditText edt=(EditText)gao.this.findViewById(R.id.edt); 
         TextView txt=(TextView)gao.this.findViewById(R.id.txt); 
         txt.setText(getString(R.string.msg_dialog)+edt.getText()); 
       } 
     }); 
    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    androidrientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 

<EditText id="@+id/edt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
/> 
<EditText id="@+id/txt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
/> 
<Button id="@+id/go" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/go"> 
    <requestFocus /> 
</Button> 
</LinearLayout> 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">hello android</string> 
    <string name="app_name">hellotwo</string> 
    <string name="tit_dialog">提示</string> 
    <string name="msg_dialog">你好,中國</string> 
    <string name="ok_dialog">確定</string> 
    <string name="go">瀏覽</string> 
</resources> 
+0

你能提供logcat輸出嗎? – sandrstar

+0

除了提到的@sandrstar的LogCat之外,你能解釋它何時崩潰嗎?點擊按鈕?負載? – Eric

回答

1

測試下面的代碼,

activity_main.xml中

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

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 

<EditText 
    android:id="@+id/edt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit" 
    android:text="" /> 

<EditText 
    android:id="@+id/txt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit" 
    android:text="" /> 

<Button 
    android:id="@+id/go" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/go" > 

    <requestFocus /> 
</Button> 
</LinearLayout> 

MainActivity.java

package com.example.stackoverflow; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button btn=(Button)findViewById(R.id.go); 

     btn.setOnClickListener(new View.OnClickListener() 
     { 
       public void onClick(View v) 
       { 
         EditText edt=(EditText)findViewById(R.id.edt); 
         TextView txt=(TextView)findViewById(R.id.txt); 
         txt.setText(getString(R.string.msg_dialog)+edt.getText()); 
       } 
     }); 
    } 
} 

string.xml

<resources>  
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title_activity_main">MainActivity</string> 
    <string name="hello">hello android</string> 
    <string name="app_name">hellotwo</string> 
    <string name="tit_dialog">提示</string> 
    <string name="msg_dialog">你好,中國</string> 
    <string name="ok_dialog">確定</string> 
    <string name="go">瀏覽</string> 
    <string name="edit">Enter text</string> 
</resources> 

輸出enter image description here

希望它能幫助你。 :-)