Android…又摸出来玩了下。

上次摸[url=http://code.google.com/android/]Google Android[/url]的模拟器已经是去年这玩意刚发布没多久的时候了。嘛,本来我也没太着急在Android上做开发。这个学期开学之后,我们这边的毕业设计也正式开始了。
邵栋老师带了几个组是做Android应用程序开发的课题的。正好附近两个宿舍都有人是做这个课题的。语仔他们组想做的是一个*个人理财软件*,之前就在国内的一些Android开发论坛到处看啊找啊,在学习相关技术知识。不过前几天碰到了很奇怪的[url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NullPointerException.html]NullPointerException[/url],问我怎么办。这样我也就又摸出Android来玩了。

正好3月3号Google更新了[url=http://code.google.com/android/download.html]Android SDK[/url]到m5-rc15,而我之前用的SDK都不记得是m多少的了(m3?),干脆就连SDK和Eclipse+ADT都一并更新过来。
到[url=http://eclipse.org/]Eclipse官网[/url]去看,现在的版本已经到3.3.2了。正好连机上的别的Eclipse也一起更新。先是下了Eclipse Classic 3.3.2,结果到安装ADT的时候出现require "org.eclipse.wst.sse.ui"的错误。很明显是要装WTP了。以前装过的老版本的ADT明明不需要的……
装WTP就得连什么GEF啊之类的一堆东西都分别装上。嫌麻烦,就重新下了个Eclipse IDE for Java Developers。然后安装ADT就没问题了。

顺带记下官网对于安装ADT插件的说明:
[quote]To download and install the ADT plugin, follow the steps below.

[list][*]Start Eclipse, then select [b]Help > Software Updates > Find and Install....[/b]
[*]In the dialog that appears, select [b]Search for new features to install[/b] and press [b]Next[/b].
Press [b]New Remote Site[/b].
[*]In the resulting dialog box, enter a name for the remote site (e.g. Android Plugin) and enter this as its URL:
[code]https://dl-ssl.google.com/android/eclipse/[/code]
Press [b]OK[/b].
[*]You should now see the new site added to the search list (and checked). Press [b]Finish[/b].
[*]In the subsequent Search Results dialog box, select the checkbox for [b]Android Plugin[/b] > [b]Developer Tools[/b]. This will check both features: "Android Developer Tools", and "Android Editors". The Android Editors feature is optional, but recommended. If you choose to install it, you need the WST plugin mentioned earlier in this page.
Now press [b]Next[/b].

[*]Read the license agreement and then select [b]Accept terms of the license agreement[/b], if appropriate. Press [b]Next[/b].
Press [b]Finish[/b].
[*]The ADT plugin is not signed; you can accept the installation anyway by pressing [b]Install All[/b].
Restart Eclipse.
[*]After restart, [b]update your Eclipse preferences[/b] to point to the SDK directory:

1. Select [b]Window[/b] > [b]Preferences[/b]... to open the Preferences panel. (Mac OS X: [b]Eclipse[/b] > [b]Preferences[/b])
2. Select Android from the left panel.
3. For the SDK Location in the main panel, press [b]Browse[/b]... and locate the SDK directory.
4. Press [b]Apply[/b], then [b]OK[/b].[/list][/quote]

===========================================================================

[b]关于某NPE问题[/b]

OK,东西都装上了,开始解决问题。之前语仔遇到的NPE是基于这样的代码:
/src/com/my/MyActivity.java:
package com.my;

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

public class MyActivity extends Activity {
private EditText edAdmin = null;
private EditText edPassword = null;
private Button confirm = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

edAdmin = (EditText) findViewById(R.id.Admin);
edPassword = (EditText) findViewById(R.id.Password);
confirm = (Button) findViewById(R.id.confirm);

confirm.setOnClickListener(o);
}

private OnClickListener o = new OnClickListener() {
public void onClick(View v) {
//...
}
};
}

/res/layout/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="Hello World, MyProject"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Admin1"
/>
<EditText id="@+id/Admin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Admin2"
/>
<EditText id="@+id/Password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button id="@+id/confirm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="confirm"
/>
</LinearLayout>


这代码看起来似乎没什么问题。我之前又没怎么看过Android的教程或者文档之类的东西,觉得不知道该从何入手。总之第一能想到的就是看看stack trace;但当在Android里遇到异常时,它就这么在屏幕上显示个简单的提示:
[img]http://rednaxelafx.iteye.com/upload/picture/pic/8786/4da45919-1bdc-3f9c-9098-7fa0fe358ba6.jpg[/img]
(这图是我另外开的一个project,故意引发了一个NPE而已……)
感觉好可怜,啥也看不出来。

然后查了下如何向输出写自定义的log,发现android.util.Log类可以做这件事。用Log.v(/*String*/tag, /*String*/message, /*Throwable*/obj)就能把日志写出来。这个静态的Log.v()方法用于输出VERBOSE级日志,其中包括了stack trace。输出的目标是logcat。
那么把前面的Java代码中onCreate()方法里的内容整个用try-block包起来,然后catch Exception并将异常状况写到log里。然后运行……没看到NPE了,但那个按钮里的代码却没起作用。在Eclipse里的output窗口也没看到任何log,超郁闷。

没办法,又查了下如何查看日志。发现在android_sdk/tools目录有一个叫DDMS的程序。执行它,并且执行其中的logcat就能看到日志。于是终于看到了那个NPE的位置:在onCreate()方法中的confirm.setOnClickListener(o);这行。
接下来,在onCreate()方法里另外加了句log,当o为null的时候输出一句话;然后没看到输出。这才发现confirm是null……觉得超诡异,大家都是这么findViewById()来得到layout中指定的UI组件的引用,这里怎么会null了呢。

然后才发觉在M3到M5的变化的文档中有提到,原本在layout的XML文件里的[b]id[/b]的属性,要改为使用前缀的[b]android:id[/b]。于是把main.xml改过来,就好了
T T

更详细的API变化,看这里:[url]http://code.google.com/android/migrating/m3-to-m5/changes.html[/url]

===========================================================================

[b]关于模拟器总是disconnect的问题[/b]

语仔从Eclipse运行Android模拟器的时候总是会出现disconnected现象,非得多run几次才好。我也不知道是为什么。
不过后来发觉实在不行的话就干脆不要从Eclipse启动模拟器了……直接“热安装”就是
在Eclipse里把代码写好,保存了之后,这么做:
1. 任意启动一个Android模拟器进程,在“热安装”之前最好让它处于home状态
2. 在命令行,转到项目的/bin目录,然后用adb直接把程序装上去:
[code]adb install yourProject.apk[/code]
3. 程序已经“热安装”完成。可以从Android界面里的ALL那边运行新装的程序看看

装新的apk或者更新旧的apk都可以这样……至少,只要有一次能正常从Eclipse启动模拟器的话,就让它一直开着,然后不停把修改过的apk文件这么“热安装”上去就行了……
=_=|||

===========================================================================

[b]关于log的问题[/b]

前天给语仔找NPE的原因时,傻乎乎的另外开了一个DDMS来监视logcat的日志。我应该更加信赖ADT的……
实际上在Eclipse装了ADT之后,在[b]Windows[/b] > [b]Preferences[/b] > [b]Android[/b] > [b]DDMS[/b] > [b]Advanced[/b]里就可以设置监听log的等级。把等级设到Verbose就能看到全部的log了。我是用Log.v()来输出的,所以不调到这个等级就看不到之前写的那log。诶,果然还是应该多看看文档再说的。

===========================================================================

[b]关于Notepad Tutorial[/b]

Google Android官网上有个不错的教程,[url=http://code.google.com/android/intro/tutorial.html]Tutorial: A Notepad Application[/url]。跟着这个教程走就能了解到Android开发中的许多重要方面,像是Activity的使用(包括sub activity的使用),Intent的使用,SQLite的使用,使用XML的layout、resource、manifest,整个应用程序的流程/生命周期(life-cycle)等。我也跟着这个教程做了一遍,做到Notepad v3。下面这张是运行的截图:
[img]http://rednaxelafx.iteye.com/upload/picture/pic/8778/96d7fe19-c91f-3e70-b9fa-27951d688bbe.jpg[/img]

关于life-cycle,这几个方法很重要:
// Called when the activity is first created.
public void onCreate(Bundle icicle)

// called by Android if the Activity is being stopped and may be killed before it is resumed!
// complement of onCreate()
protected void onFreeze(Bundle outState)

// called when the Activity ends
// should release any resources that can be released
protected void onPause()

// complement of onPause()
protected void onResume()


当然这个地方说得更详细一些:[url=http://code.google.com/android/intro/lifecycle.html]Life Cycle of an Android Application[/url]

===========================================================================

[b]关于卸载Android模拟器中已经安装了的程序[/b]

在启动了一个Android模拟器进程之后,在命令行使用adb来卸载已经安装的程序:(一定要启动至少一个模拟器实例)
[code]D:\android\android-sdk_m5-rc15_windows\tools>adb shell
# cd /data/app
cd /data/app
# ls
ls
Notepadv3.apk
ApiDemos.apk
Notepadv1.apk
Notepadv2.apk
# rm Notepadv1.apk
rm Notepadv1.apk
# rm Notepadv2.apk
rm Notepadv2.apk
# exit
exit[/code]
这样就把前面装的Notepad v1和v2都卸载了。清爽。

===========================================================================

[b]其它[/b]

不知道实际的Dalvik VM在手机上跑速度如何呢,在模拟器上感觉还可以。只是模拟器的启动时间总是很长……
不过在我的机上跑模拟器明显比在语仔机上要快。不知道是不是因为我这边没怎么装游戏的缘故……
呵呵,开发机与游戏机总是无法并存的,除非开发的就是游戏……|||
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值