Chinaunix首页 | 论坛 | 博客
  • 博客访问: 404570
  • 博文数量: 120
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 18:15
文章分类

全部博文(120)

文章存档

2016年(13)

2015年(41)

2014年(66)

我的朋友

分类: Android平台

2014-10-13 15:25:46

获取fragment_main.xml布局控件出现异常的解决办法:
    Android3.0以后新建的Android应用包含了两个xml布局文件,一个为activity_main.xml,另外一个fragment_main.xml,这样就可以实现UI的分片处理,我们以后就可以在fragment_main.xml上面添加控件等等。对于新手来说,可能在fragment_main.xml上面添加控件了,但是获取资源的操作却放在了应用的MainActivity的onCreate方法里面,而该方法里面却只加载了activity_main.xml文件,所以此时在这里面获取fragment_main.xml里面的资源,肯定是获取不到的,使用findByViewId获取到的值为NULL,其实对于fragment_main.xml, 是通过public static class PlaceholderFragment extends Fragment 里面的onCreateView方法来加载的,所以获取资源的方法应该放在onCreateView里面,给大家贴一个例子:

点击(此处)折叠或打开

  1. package com.example.activitydemo;

  2. import android.support.v7.app.ActionBarActivity;
  3. import android.support.v7.app.ActionBar;
  4. import android.support.v4.app.Fragment;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.view.ViewGroup;
  13. import android.widget.Button;
  14. import android.os.Build;
  15. import android.util.Log;

  16. public class MainActivity extends ActionBarActivity {

  17.     private static final String TAG = "ActivityDemo";
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);

  22.         if (savedInstanceState == null) {
  23.             getSupportFragmentManager().beginTransaction()
  24.                     .add(R.id.container, new PlaceholderFragment())
  25.                     .commit();
  26.         }
  27.         
  28.         Log.e(TAG, "start onCreate~~~");
  29.     }

  30.     @Override
  31.     protected void onStart() {
  32.         super.onStart();
  33.         Log.e(TAG, "start onStart~~~");
  34.     }
  35.      
  36.     @Override
  37.     protected void onRestart() {
  38.         super.onRestart();
  39.         Log.e(TAG, "start onRestart~~~");
  40.     }
  41.      
  42.     @Override
  43.     protected void onResume() {
  44.         super.onResume();
  45.         Log.e(TAG, "start onResume~~~");
  46.     }
  47.      
  48.     @Override
  49.     protected void onPause() {
  50.         super.onPause();
  51.         Log.e(TAG, "start onPause~~~");
  52.     }
  53.      
  54.     @Override
  55.     protected void onStop() {
  56.         super.onStop();
  57.         Log.e(TAG, "start onStop~~~");
  58.     }
  59.      
  60.     @Override
  61.     protected void onDestroy() {
  62.         super.onDestroy();
  63.         Log.e(TAG, "start onDestroy~~~");
  64.     }
  65.     
  66.     @Override
  67.     public boolean onCreateOptionsMenu(Menu menu) {
  68.         
  69.         // Inflate the menu; this adds items to the action bar if it is present.
  70.         getMenuInflater().inflate(R.menu.main, menu);
  71.         return true;
  72.     }

  73.     @Override
  74.     public boolean onOptionsItemSelected(MenuItem item) {
  75.         // Handle action bar item clicks here. The action bar will
  76.         // automatically handle clicks on the Home/Up button, so long
  77.         // as you specify a parent activity in AndroidManifest.xml.
  78.         int id = item.getItemId();
  79.         if (id == R.id.action_settings) {
  80.             return true;
  81.         }
  82.         return super.onOptionsItemSelected(item);
  83.     }

  84.     /**
  85.      * A placeholder fragment containing a simple view.
  86.      */
  87.     public static class PlaceholderFragment extends Fragment {

  88.         public PlaceholderFragment() {
  89.         }

  90.         @Override
  91.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  92.                 Bundle savedInstanceState) {
  93.             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  94.             
  95.             Button button = (Button)rootView.findViewById(R.id.button_normal);
  96.             
  97.             button.setOnClickListener(new OnClickListener() {

  98.                 @Override
  99.                 public void onClick(View arg0) {
  100.                     // TODO Auto-generated method stub
  101.                     Intent intent = new Intent(getActivity(), SecondActivity.class);
  102.                     startActivity(intent);
  103.                 }     
  104.             });
  105.             return rootView;
  106.         }
  107.     }

  108. }

fragment_main.xml

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context="com.example.activitydemo.MainActivity$PlaceholderFragment" >

  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="@string/hello_world" />

  14.     <EditText
  15.         android:id="@+id/editText"
  16.         android:layout_width="fill_parent"
  17.         android:layout_height="wrap_content" />

  18.     <Button
  19.         android:id="@+id/button_normal"
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:layout_alignLeft="@+id/editText"
  23.         android:layout_below="@+id/editText"
  24.         android:layout_marginLeft="25dp"
  25.         android:layout_marginTop="90dp"
  26.         android:text="页面普通跳转" />

  27. </RelativeLayout>



阅读(937) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~