更新到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. CreateEvent的使用方法

     CreateEvent的使用方法收藏 新一篇: PreCreateWindow的作用和用法 | 旧一篇: VC中_T()的作用 function StorePage(){d=document;t=d ...

  2. Python调用C可执行程序(subprocess) 分类: python 服务器搭建 C/C++ shell 2015-04-13 21:03 87人阅读 评论(0) 收藏

    从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn.os.popen.popen2.commands. ...

  3. [React] React Fundamentals: Precompile JSX

    The JSX Transformer library is not recommended for production use. Instead, you'll probably want to ...

  4. [D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. Publisher/Subscriber(发布/订阅者)消息模式开发流程

    该模式的作用是发布者和订阅者 可以相互发送消息 发布者和订阅者都充当 生产者和消费者 发布者 package publisher.to.subscriber; import java.awt.font ...

  6. 动态jsp页面转PDF输出到页面

    最近工作中遇到不少问题.总结一下.这段代码主要功能是将一个生成JSP页面转发成PDF输出到页面 需要利用ITEXT String html = ServletUtils.forward(request ...

  7. 用标准版的Eclipse搭建PHP环境

    用标准版的Eclipse搭建PHP环境 ——@梁WP 摘要:用标准版的Eclipse搭建PHP环境. 一.下载Eclipse的PHP插件 百度搜索phpeclipse,看到某条结果是带有SourceF ...

  8. google map 定位

    在map初始化的过程中,得到当前经纬度,完成初始化地图,通过HTML5中的Geolocation实现,具体参考:http://www.jb51.net/html5/71556.html 1.获取当前地 ...

  9. JavaScript Invalid Date Verify

    if ( Object.prototype.toString.call(d) === "[object Date]" ) { // it is a date if ( isNaN( ...

  10. uniq和sort的用法

    uniq和sort都是按行操作的linux命令. sort按文本行排序,如下所示的log文件:直接sort log即可将其排序. 容易忽略的是sort -n命令,在如下例子中将看到 如果直接sort则 ...