新手按《Android应用开发揭秘》中的代码,由于版本不同做了一些修改。
可以运行不了,直接报应用异常。请各位高手帮忙看看是什么原因,多谢!package leo.zheng.LayoutUsage;import android.app.Activity;
import android.os.Bundle;
import android.provider.Contacts;import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;import android.view.View;
import android.util.Log;public class LayoutUsage extends Activity {
LinearLayout linearLayout;
ListView lv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(android.graphics.Color.BLACK);
lv = new ListView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(lv,param);
setContentView(linearLayout);
Cursor cur = getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cur,
new String[] { Contacts.People.NAME, Contacts.People.NUMBER},
new int[] { android.R.id.text1, android.R.id.text2 });
lv.setAdapter(adapter);
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
Log.v("Adapter", "Item selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
}
);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
}
}
);
}
}
可以运行不了,直接报应用异常。请各位高手帮忙看看是什么原因,多谢!package leo.zheng.LayoutUsage;import android.app.Activity;
import android.os.Bundle;
import android.provider.Contacts;import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;import android.view.View;
import android.util.Log;public class LayoutUsage extends Activity {
LinearLayout linearLayout;
ListView lv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(android.graphics.Color.BLACK);
lv = new ListView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(lv,param);
setContentView(linearLayout);
Cursor cur = getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cur,
new String[] { Contacts.People.NAME, Contacts.People.NUMBER},
new int[] { android.R.id.text1, android.R.id.text2 });
lv.setAdapter(adapter);
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
Log.v("Adapter", "Item selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
}
);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
}
}
);
}
}
解决方案 »
- Android 怎么向打开的网页中自动填充用户名和密码?
- 寻求一个完整的NDK下使用SDL简单例子
- android mms 更新时间格式(在setting中设置完时间格式的显示:mm/dd/yyy),短信或者mms中:信息的显示日期格式的更新机制是怎样的?
- 如何解决地图加载图层较多时,滑动地图出现的不流畅现象
- android 中java层怎么在新建的分区上存储数据?
- Android绘制图表 Charts4j的问题!
- 2.3.3截图功能
- 在做菜单练习的时候总是不成功,点击菜单后出错,为什么呢?快崩溃了
- 循环生成View,每个onClick方法接收参数求解!!!
- 新人提问,关于.so文件调用另一个.so的问题
- 【pc模拟手机】,这种图片是怎么弄的?
- Android SDK 1.5 是不是不能使用 AlertDialog.Builder
<uses-permission android:name="android.permission.READ_CONTACTS" />
02-24 11:10:16.911: DEBUG/AndroidRuntime(749): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
02-24 11:10:16.921: DEBUG/AndroidRuntime(749): CheckJNI is ON
02-24 11:10:17.221: DEBUG/AndroidRuntime(749): --- registering native functions ---
02-24 11:10:18.170: INFO/ActivityManager(567): Starting activity: Intent { flags=0x10000000 comp={leo.zheng.LayoutUsage/leo.zheng.LayoutUsage.LayoutUsage} }
02-24 11:10:18.180: WARN/ActivityManager(567): Permission Denial: starting Intent { flags=0x10000000 comp={leo.zheng.LayoutUsage/leo.zheng.LayoutUsage.LayoutUsage} } from null (pid=-1, uid=-1) requires android.permission.READ_CONTACTS
// Permission Denial 是最明示的原因。
02-24 11:10:18.201: DEBUG/AndroidRuntime(749): Shutting down VM
02-24 11:10:18.211: WARN/dalvikvm(749): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
02-24 11:10:18.211: ERROR/AndroidRuntime(749): Uncaught handler: thread main exiting due to uncaught exception
02-24 11:10:18.221: ERROR/AndroidRuntime(749): *** EXCEPTION IN SYSTEM PROCESS. System will crash.
02-24 11:10:18.270: ERROR/AndroidRuntime(749): java.lang.SecurityException: Permission Denial: starting Intent { flags=0x10000000 comp={leo.zheng.LayoutUsage/leo.zheng.LayoutUsage.LayoutUsage} } from null (pid=-1, uid=-1) requires android.permission.READ_CONTACTS
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at android.os.Parcel.readException(Parcel.java:1234)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at android.os.Parcel.readException(Parcel.java:1222)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1046)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at com.android.commands.am.Am.runStart(Am.java:199)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at com.android.commands.am.Am.run(Am.java:73)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at com.android.commands.am.Am.main(Am.java:51)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at com.android.internal.os.RuntimeInit.finishInit(Native Method)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:186)
02-24 11:10:18.270: ERROR/AndroidRuntime(749): at dalvik.system.NativeStart.main(Native Method)
02-24 11:10:18.291: ERROR/JavaBinder(749): Unknown binder error code. 0xfffffff7
02-24 11:10:18.291: ERROR/AndroidRuntime(749): Crash logging skipped, no checkin service
02-24 11:10:18.291: INFO/Process(749): Sending signal. PID: 749 SIG: 9
偶是通过 AndroidManifest.xml 的可视化编辑界面,在 Application 界面的 Permission 编辑框中设定的。
设定后在 AndroidManifest.xml 中生成如下的东东:
android:permission="android.permission.READ_CONTACTS"
自动生成前是:
<application android:icon="@drawable/icon" android:label="@string/app_name">
自动生成后是:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:permission="android.permission.READ_CONTACTS">谁能告诉我这两种有什么区别?
或者说第一种(错误的哪种)是什么作用?