为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和ImageView,来显示书的详细内容和图片。个人理解就好像web中的iframe样,一个HTML页面可以放多个iframe
调试欢乐多
修空指针异常的方法就是在报错的位置前后设几个断点,然后一步步走看哪里、哪个变量在运行的时候是空。
至于为啥是空,就得要具体问题具体分析了,比较常见的错误是写代码的人不太了解Android的生命周期,没搞清楚什么先被调用什么后被调用。有的时候你代码里写的感觉是给那个变量赋值了一个实例化对象,但那只是你自己的感觉而已,实际上系统用到这个变量的时机比你想的要更早,系统需要的时候发现这个变量的值是空的所以就报了空指针异常。
使用了findViewById方法后马上输出得到的是null,而且程序在虚拟机上也不能运行。这个控件是写在fragment_main.xml里的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (savedInstanceState == null) {
getFragmentManager().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);
return rootView;
}
}}
这就是全部的代码了,你能帮我看看是哪一个方法是怎么实现这个功能的吗?谢谢了。
.add(R.id.container, new PlaceholderFragment()).commit();
不习惯的可以这样处理:
1.删除fragment_main.xml整个文件
2.对activity_main.xml,删除里面的内容。然后切换到Graphy Layout,放入一个LinearLayout就可以
3.对MainActivity.java,可以删除部分的内容,再把MainActivity extends ActionBarActivity 改为MainActivity extends Activity :
改后如下:
package com.zmcorp.weightcalculator;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.os.Build;MainActivity extends Activity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}@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);
}
}}
逛了好多论坛和博客
}@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView textview = (TextView)rootView.findViewById(R.id.view);
System.out.println(textview);
testview.setText("Hello Android!");return rootView;
}
}
* 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);
Button btnFirstButton = (Button) rootView.findViewById(R.id.btn_firstButton);
btnFirstButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "rootView.findViewById|fragment_main - -#", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
public PlaceholderFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button)rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,MainsActivity.class);
startActivity(intent);
}
});
return rootView;
}
}这里的“Intent intent=new Intent(MainActivity.this,MainsActivity.class);“ 有错。
请问Intent里的第一个参数怎么设??
分类: Android Fragment详解 2012-10-27 21:44 6771人阅读 评论(4) 收藏 举报
目录(?)[+]
与activity通讯
尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。
Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:
ViewlistView =getActivity().findViewById(R.id.list);同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。例如:
[java] view plaincopy
ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment
Intent intent=new Intent(getActivity(), NextActivity.class);
这样设就可以了
楼主啊。不知道我的问题跟你的是否一样。
我是设置一个edittext,跟一个button 。调用findviewbyid,都没有问题 。可是一旦给button加上 监听 就出现空指针错误。烦了好几天了!!!
楼主啊。不知道我的问题跟你的是否一样。
我是设置一个edittext,跟一个button 。调用findviewbyid,都没有问题 。可是一旦给button加上 监听 就出现空指针错误。烦了好几天了!!!你要是吧Button写在Fragment_main.xml里,又在Activity的里面使用findViewById来获取Button对象,不用想了,就是这样。
findViewById 改成 rootView.fidViewById.
You cannot combine swipe dismissal and the action bar.
好困扰啊
学习