更新到Android 4.4,写了个小程序。发现运行不起来了。抛空指针异常。debug模式下,发现在onCreate方法中获取Button是null。

Android 4.4把layout进行了重组,全部放到了Fragment下,在onCreate方法执行时,Fragment还没有被调用,所以在这个方法中获取Fragment中的Button是空值。新类中多了一个Fragment的内部类,当xml的Fragment被装载时,会调用内部类的onCreateView方法,所以要将所有之前在layout中而现在移到fragment中的操作全部移到此方法中进行。

package com.bignerdranch.android.geoquiz;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast; public class TestActivity extends ActionBarActivity { private static Button mFirstButton;
private static Button mSecondButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test); if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} /**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_test, container,
false); mFirstButton = (Button) rootView.findViewById(R.id.first_button);
mFirstButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(rootView.getContext(), R.string.first_toast,
Toast.LENGTH_SHORT).show();
} }); mSecondButton = (Button) rootView.findViewById(R.id.second_button);
mSecondButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(rootView.getContext(), R.string.second_toast,
Toast.LENGTH_SHORT).show();
} }); return rootView;
}
} }

  

变更到Android4.4的问题的更多相关文章

  1. android4.4.2 短信广播变更

    近期三星陆续放出android4.4.2ROM更新包,android4.4.2对短信虽说是放开了权限,但其实是加强了限制,一台手机智能通过一个设置为默认应用的短信应用软件才能发送短信,否则就无法对短信 ...

  2. Android短信监听实现,及Android4.4之后短信机制变更

    前阵子公司有一个项目,简单的监听短信应用,功能只有如下两个: 1.监听短信并获取短信内容上传服务器: 2.从服务器获取短信内容,发送出去    按照传统的思路,监听短信我们有两种方式:第一种是使用广播 ...

  3. Android4.0设置接口变更摘要(四)

    为了与你之前,你已经设置了共享Tab风格和Item实现圆角.希望能给有须要的朋友一点点帮助,今天再和大家分享一下用ViewPager实现设置分页,小米和OPPO就是这种设置,先来看看效果图: wate ...

  4. Ubuntu16.04下iTop4412环境搭建+Android4.0.3编译

    系统:Ubuntu16.04.02(优麒麟) 安装库文件和JDK 使用cd 命令,进入解压出来的“Android_JDK” -->“jdk6”文件夹,运行脚本文件 “install-sun-ja ...

  5. 如何利用pt-online-schema-change进行MySQL表的主键变更

    业务运行一段时间,发现原来的主键设置并不合理,这个时候,想变更主键.这种需求在实际生产中还是蛮多的. 下面,看看pt-online-schema-change解决这类问题的处理方式. 首先,创建一张测 ...

  6. Map工具系列-07-TFS变更集提取工具

    所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...

  7. win8 app GridView点击子项布局变更

    要触发点击必须设置IsItemClickEnabled="True" 要变更布局代码如下: private void gridView_ItemClick_1(object sen ...

  8. SQL Server 数据变更时间戳(timestamp)在复制中的运用

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 方案(Solution) 方案一(Solution One) 方案二(Solution Two ...

  9. SQL Server 变更数据捕获(CDC)监控表数据

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现过程(Realization) 补充说明(Addon) 参考文献(References) ...

随机推荐

  1. 【ThinkingInC++】65、使用delete void*可能会出错

    /** * 书本:[ThinkingInC++] * 功能:使用delete void*可能会出错 * 时间:2014年10月5日14:31:43 * 作者:cutter_point */ #incl ...

  2. json xmpp

    https://github.com/lamfire/jspp http://blog.csdn.net/nomousewch/article/category/823687 http://my.os ...

  3. Android_设备隐私获取,忽略6.0权限管理

    1.前言 (1).由于MIUI等部分国产定制系统也有权限管理,没有相关api,故无法判断用户是否允许获取联系人等隐私.在Android 6.0之后,新增权限管理可以通过官方api判断用户的运行状态: ...

  4. 启动hadoop,报错Error JAVA_HOME is not set and could not be found

    报如错误:JAVA_HOME is not set and could not be found,可能是因为JAVA_HOME环境没配置正确,还有一种情况是即使各结点都正确地配置了JAVA_HOME, ...

  5. HDU2088JAVA2

    Box of Bricks Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. day-8

    /* 倒数8天了 今天高考报名 两个班主任都来了23333 然后填班主任姓名就尴尬了23333 上午考试 挺不错的题目 都不是很水 但需要思考 而我是一脸的懵逼~~ 下午整理各种板子 主要是图论的 最 ...

  7. 调试php的soapServer

    用.NET的webservice做调试很轻松. 用soapserver的try和cacth获取不了多少信息

  8. bootstrap学习和使用的经验总结

    第一肯定是下载 然后就是目录介绍,因为bootstrap是个轻量级的框架,目录不是很多,所以很容易理解,主要有用的就是三个文件,bootstrap.js,bootstrap.css,bootstrap ...

  9. mysql开启函数功能

    输入 show variables like '%func%'; 命令 会看到 log_bin_trust_function_creators 的状态,如果是OFF表示自定义函数功能是关闭的 输入命令 ...

  10. C# DateTime显示时间格式的使用

    代码DateTime.ToString() Patterns All the patterns: 0 MM/dd/yyyy 08/22/2006 1 dddd, dd MMMM yyyy Tuesda ...