Maison > Questions et réponses > le corps du texte
Android新手,想用一下style定义控件的外观,我在 res/values/
下,建立了一个 style.xml
,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="base_text">
<item name="android:textSize">15sp</item>
<item name="android:textColor">#00FF00</item>
</style>
</resources>
结果,我还没有在layout或程序中引用 style,生成的app就会崩溃,我是不是遗漏了什么呢?
崩溃日志,来自(Logcat)
12-18 03:27:11.897: D/AndroidRuntime(879): Shutting down VM
12-18 03:27:11.897: W/dalvikvm(879): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
12-18 03:27:11.947: E/AndroidRuntime(879): FATAL EXCEPTION: main
12-18 03:27:11.947: E/AndroidRuntime(879): java.lang.RuntimeException: Unable to start activity ComponentInfo{jollywing.app/jollywing.app.TaskList}: java.lang.NullPointerException
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.os.Looper.loop(Looper.java:137)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-18 03:27:11.947: E/AndroidRuntime(879): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 03:27:11.947: E/AndroidRuntime(879): at java.lang.reflect.Method.invoke(Method.java:525)
12-18 03:27:11.947: E/AndroidRuntime(879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-18 03:27:11.947: E/AndroidRuntime(879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 03:27:11.947: E/AndroidRuntime(879): at dalvik.system.NativeStart.main(Native Method)
12-18 03:27:11.947: E/AndroidRuntime(879): Caused by: java.lang.NullPointerException
12-18 03:27:11.947: E/AndroidRuntime(879): at jollywing.app.TaskList.onCreate(TaskList.java:68)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.Activity.performCreate(Activity.java:5133)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-18 03:27:11.947: E/AndroidRuntime(879): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-18 03:27:11.947: E/AndroidRuntime(879): ... 11 more
12-18 03:27:12.017: D/dalvikvm(879): GC_FOR_ALLOC freed 463K, 19% free 2554K/3136K, paused 39ms, total 45ms
日志中报告第68行出现了空指针错误,贴上代码,TaskList.java中onCreate函数
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
taskList = (ListView)findViewById(R.id.task_list);
loadTaskData();
taskAdapter =new SimpleAdapter(this, taskListData, R.layout.task_item,
new String[] {"taskName", "taskTomatoNum"},
new int[]{R.id.task_name, R.id.task_tomato_num});
taskList.setAdapter(taskAdapter); //这就是第68行
taskList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id){
Intent intent = new Intent(TaskList.this, TaskClock.class);
Bundle data = new Bundle();
data.putInt("task_id", position);
HashMap<String, Object> map = taskListData.get(position);
data.putString("task_name", (String)map.get("taskName"));
intent.putExtras(data);
startActivityForResult(intent, TASK_TOMATO_REQUEST);
}
});
}
天蓬老师2017-04-17 13:03:47
taskList.setAdapter(taskAdapter); //这就是第68行
既然是空指针 说明 taskList 这个对象为null.
taskList = (ListView)findViewById(R.id.task_list);
findViewById失败了吧.
你检查一下R.layout.main对应的xml里是不是有R.id.task_list这个ListView吧..一般都是这里写错了..