众所周知,我们既可以在 activity_main.xml文件中控制activity中的view,也可以使用java代码的set..()方法控制它。在学习过程中,发现在ADT新版本中,和以前版本有区别:

新建Andriod工程后,MainActivity 不再继承Activity,而继承的是ActionBarActivity;在layout文件夹下会自动生成两个.xml文件,activity_main.xml文件和fragement_main.xml文件,和以前的版本只有一个activity_main.xml文件是不一样的。

在fragment_main.xml中写一个TextView,内容为Hello World:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.textview.MainActivity$PlaceholderFragment" > <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="@string/hello_world" /> </LinearLayout>

我们当然可以在.xml文件中控制文本内容的显示,若是MainActivity中控制文本内容的显示,可以使用代表控件的对象;在以前的只有Activity_main.xml文件的版本中,可以这样写代码:

public class MainActivity extends Activity {

	private  TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView);//向下转型为TextView类型,根据xml文件中ID找到该TextView,
textView.setBackgroundColor(Color.BLUE);//设置背景颜色 if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
}

但是在新的版本中,继承的不再是activity,而是ActionBarActivity,而且在layout中有两个xml文件,若是按照上述的方式写,程序无法运行,解决这个问题的方法如下:

1.在内部静态类PlaceholderFragment 中的onCreateView()方法中获取textView对象,而不是在onCreate()方法中获取这个对象。

2.因为是内部静态类,故应该声明为private static TextView textView;

运行 程序,OK

package com.example.textview;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build; public class MainActivity extends ActionBarActivity { private static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//空指针异常
// textView = (TextView)findViewById(R.id.textView);//此处无法获得该对象
// textView.setBackgroundColor(Color.BLUE); 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.main, 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) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
textView = (TextView) rootView.findViewById(R.id.textView);//在这里获得该对象 OK!!!
textView.setText("Hello");
return rootView;
}
} }

Andriod ADT v22.6.2版本中在Mainactivity.java中使用fragment_main.xml中TextView控件对象的问题的更多相关文章

  1. android中的TextView控件

    我以前是搞ssh开发的,现在在搞android开发,所以简单学习了一下,对于自己所了解的做一个记录,也算是一个笔记吧,如果有什么不对的,希望大家给予一定的指导.  一.TextView的基本使用 Te ...

  2. 嵌套在ScrollView中的TextView控件可以自由滚动

    //设置TextView控件可以自由滚动,由于这个TextView嵌套在ScrollView中,所以在OnTouch事件中通知父控件ScrollView不要干扰. mContractDesc.setO ...

  3. Winform中怎样跨窗体获取另一窗体的控件对象

    场景 Winform中实现跨窗体获取ZedGraph的ZedGraphControl控件对象: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/de ...

  4. Winform中实现跨窗体获取ZedGraph的ZedGraphControl控件对象

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  5. wpf中在style的template寻找ControlTemplate和DataTemplate的控件

    一.WPF中的两棵树 WPF中每个控件的Template都是由ControlTemplate构成,ControlTemplate包含了构成该控件的各种子控件,这些子控件就构成了VisualTree:而 ...

  6. iOS开发中的错误整理,关于用绑定Tag取控件的注意事项,有时候不绑定也是个错!

    如图:红色框中是个自定义的导航工具条titlesView(没有绑定Tag),工具条中有五个按钮(按钮绑定了Tag)以及一个红色的指示器indicatorView(没有绑定Tag),下面的蓝色是可以滚动 ...

  7. DTCMS中部分IE8不支持webupload上传附件的控件,更改为ajaxfileupload

    dialog\dialog_attach.aspx <!DOCTYPE html> <html> <head> <meta http-equiv=" ...

  8. Ext中窗体第二次点击报错或者其内控件不显示的问题,弄了2天才解决,记录下

    registerPanel.js: registerPanel = new Ext.form.FormPanel({ id:'registerPanel', layout:'form', autoHe ...

  9. android开发中一个activity如何调用另一个xml中的控件

    有时候,我们需要在一个activity中使用另一个activity中的控件,这时候就不能直接findViewById,不然会报错指向空对象,这时就需要像下面这样做. LayoutInflater fa ...

随机推荐

  1. 【转】iOS开发者申请发布证书及真机调试图文详解

    原文网址:http://www.tqcto.com/article/mobile/57822.html 打开iOS Dev Center,选择Sign in,登陆(至少99美元账号),登陆之后在网页右 ...

  2. linux .o,.a,.so文件解析

    linux下文件的类型是不依赖于其后缀名的,但一般来讲:.o,是目标文件,相当于windows中的.obj文件.so 为共享库,是shared object,用于动态连接的,和dll差不多.a为静态库 ...

  3. Firebug控制台详解

    转自:http://www.ruanyifeng.com/blog/2011/03/firebug_console_tutorial.html 作者: 阮一峰 日期: 2011年3月26日 Fireb ...

  4. Climbing Worm

    Climbing Worm Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. Linux文件虚拟机系统只读Read-only file system的快速解决方法

    问题描述:上周公司的私有云(底层架构是Openstack+KVM,目前稳定性还不够好,开发团队在改进中)一个计算节点挂掉,之后恢复后发现这个计算节点的所有Linux系统都变成只读了,复制文件提示:Re ...

  6. html页面显示div源代码:用<xmp></xmp>标签

    html页面显示div源代码:用<xmp></xmp>标签效果还可以.

  7. 在 slua 中使用更新的面向对象方案

    上一篇记录了我使用 Slua.Class 来实现面向对象扩展 C# 中得类,但实际使用中,更多地情况是直接在 lua 中定义基类然后扩展,于是触发了我重新思考下是否两种形式应该统一用一种,目前的方案中 ...

  8. Naive and Silly Muggles

    Problem Description Three wizards are doing a experiment. To avoid from bothering, a special magic i ...

  9. Oracle translate 函数

    项目里要求对军人身份证特殊处理,只要数字位.本想用正则表达式,但是oracle9i不支持正则. 后来发现translate 可以实现功能. translate(string,from_str,to_s ...

  10. Uber能知道你是不是在开车的时候玩手机

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...