findViewById()返回null

Posted by dohkoos on August 18th, 2010 (83 views)

通过findViewById()获取一个Button时一直返回null值,百思不得其解。最后发现是因为在layout文件中使用了id的旧风格,如下所示:

<Button id="@+id/btn_ok" />

改成android:id就成功了。

<Button android:id="@+id/btn_ok" />

Related Posts

Android下显示本地Contact Number信息(续)

Posted by dohkoos on February 13th, 2010 (430 views)

前文的代码只能显示联系人的第一个号码,很不方便。如果要列出联系人的所有号码,需要将People.CONTENT_URI改为Phones.CONTENT_URI。具体代码如下:

package name.dohkoos;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ContactKit extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list);

        ListView contactList = (ListView)findViewById(R.id.contactList);

        Cursor contactsCursor = managedQuery(Phones.CONTENT_URI,
                null, null, null, null);
        startManagingCursor(contactsCursor);

        String[] columnsToMap = new String[] {Phones.NAME, Phones.NUMBER};
        int[] mapTo = new int[] {R.id.contactName, R.id.contactNumber};

        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                R.layout.contact_entry, contactsCursor,
                columnsToMap, mapTo);
        contactList.setAdapter(mAdapter);
    }
}

Related Posts

如何获取Android手机的型号

Posted by dohkoos on February 5th, 2010 (1,197 views)

怎么样才能获的Android手机的型号呢?例如HTC Hero或HTC Magic等名字。android.os.Build类有很多静态属性可以获得机器的信息,其中的MODEL属性就指代手机型号。

package name.dohkoos.lesson;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

public class QueryProductModel extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("Product Model: " + Build.MODEL);
        setContentView(tv);
    }
}

在我的英雄机上显示的是
Product Model: HTC Hero

Related Posts

Android下显示本地Contact Number信息

Posted by dohkoos on January 23rd, 2010 (793 views)

首先要在AndroidManifest.xml中配置允许读取联系人的权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />

contact_list.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">
    <ListView android:id="@+id/contactList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

contact_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <TextView android:id="@+id/contactName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView android:id="@+id/contactNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

ContactKit.java

package name.dohkoos;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ContactKit extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list);

        ListView contactList = (ListView)findViewById(R.id.contactList);

        Cursor contactsCursor = managedQuery(People.CONTENT_URI,
                null, null, null, null);
        startManagingCursor(contactsCursor);

        String[] columnsToMap = new String[] {People.NAME, People.NUMBER};
        int[] mapTo = new int[] {R.id.contactName, R.id.contactNumber};

        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                R.layout.contact_entry, contactsCursor,
                columnsToMap, mapTo);
        contactList.setAdapter(mAdapter);
    }
}

Related Posts

如何联机调试Android程序

Posted by dohkoos on December 26th, 2009 (864 views)

1. 首先你需要一台开发手机,如果不是的话可以刷ROM

2. 在Settings -> Applications -> Development下激活USB debugging选项

3. 下载并安装USB驱动程序
http://handheld.softpedia.com/progDownload/HTC-Hero-Drivers-Download-81097.html

4. 用USB数据线连接手机与电脑

5. 在AndroidManifest.xml中的<application>里添加android:debuggable="true"

6. 然后你就可以像平常一样调试你的Android代码了

Related Posts

在Windows Server 2008下安装Android SDK 2.0

Posted by dohkoos on November 4th, 2009 (683 views)

SDK Setup.exe在Windows Server 2008上是无法执行的,看Readme文档知道可以用命令行的方式更新。不过不带参数执行时抛出了下面的错误:

Starting Android SDK Updater
SWT folder 'lib\x86_64' does not exist.
Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.

查看tools/android.bat代码后发现有个bug,copy代码时没有考虑x86_64平台的问题,可以找到

xcopy lib\x86 %tmpdir%\lib\x86 /I /E /C /G /R /Y /Q > nul

把它改成

xcopy %swt_path% %tmpdir%\%swt_path% /I /E /C /G /R /Y /Q > nul

或者也可以在环境变量中添加ANDROID_SWT,使它指向lib\x86_64\swt.jar文件的位置。

Related Posts