Android帮助开始程序员

时间:2011-05-14 08:33:11

标签: android

来源:

package dreamcode.tutorials.part_two;

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

public class dic_tut2 extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                Button b = (Button)this.findViewById(R.id.btn_confirm);
                b.setOnClickListener(this);
    }
    @Override        
    public void onClick(View v) {
                TextView tv = (TextView)this.findViewById(R.id.tv_welcome);
                EditText et = (EditText)this.findViewById(R.id.txt_name);

                String text = "Hello, " + et.getText().toString() + ".\n\n";
                text += "Welcome to android development. :)";

                tv.setText(text);
    }
}

调试信息:

Android [Android Application]   
    DalvikVM[localhost:8610]    
        Thread [<3> main] (Suspended (exception RuntimeException))  
            ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417  
            ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2512   
            ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
            ActivityThread$H.handleMessage(Message) line: 1863  
            ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 4363    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 521  
            ZygoteInit$MethodAndArgsCaller.run() line: 860  
            ZygoteInit.main(String[]) line: 618 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<13> Binder Thread #2] (Running)    
        Thread [<11> Binder Thread #1] (Running)
问题: 据我所知,我的功能或Android版本的功能不合适。源代码取自1.5版。并用于版本2.1。所以任何方式都可以解释或显示正确的方向,我可以找到正确的功能创建按钮。或者请修复此代码以开始工作。

我的目标: 创造做某事的功能。版本合规性2.1。

编译说:

[2011-05-14 12:10:33 - dic_tut2] ------------------------------
[2011-05-14 12:10:33 - dic_tut2] Android Launch!
[2011-05-14 12:10:33 - dic_tut2] adb is running normally.
[2011-05-14 12:10:33 - dic_tut2] Performing dreamcode.tutorials.part_two.dic_tut2 activity launch
[2011-05-14 12:10:33 - dic_tut2] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'my_avd'
[2011-05-14 12:10:33 - dic_tut2] Uploading dic_tut2.apk onto device 'emulator-5554'
[2011-05-14 12:10:33 - dic_tut2] Installing dic_tut2.apk...
[2011-05-14 12:10:39 - dic_tut2] Success!
[2011-05-14 12:10:40 - dic_tut2] Starting activity dreamcode.tutorials.part_two.dic_tut2 on device emulator-5554
[2011-05-14 12:10:45 - dic_tut2] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=dreamcode.tutorials.part_two/.dic_tut2 }

有用的东西:

我将把我的2美分投入。首先,当使用eclipse时,有一个错误,它正在工作,在eclipse和sdk之间。如果修改xml文件,保存并尝试构建通常会出现有关解析的错误。如果从问题窗口中删除错误,则修改java文件并保存。现在它应该编译。当我说修改你可以添加一行并删除它然后保存它。如果在xml文件之后保存java文件,则应该编译。 (#6 codeHammer)

我做到了,错误消失=]]]。

2 个答案:

答案 0 :(得分:0)

首先,为什么要在按钮的点击上初始化你的观点?您必须在onCreate方法中执行此操作,并在onClick上更改其文本值。无论如何,很难根据你的调试信息说些什么。

答案 1 :(得分:0)

在您的活动中实施onclicklistener接口。然后在oncreate方法中编写用于单击按钮的代码..