我正在使用片段在操作栏中使用Tab创建我的应用程序。我的代码如下:
Tab1Fragment.java& Tab2Fragment是一样的
public class Tab1Fragment extends ListFragment{
String list[] = new String[]{
"apple", "bluecherry", "cherry", "coconut cream"
};
/* (non-Javadoc)
* @see android.app.ListFragment#onCreateView(android.view.LayoutInflater,android.view.ViewGroup,android.os.Bundle) */
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tablist,container,false);
ListAdapter adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list);
setListAdapter(adapter);
return view;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "Vị trí vừa chọn: " + position + "," + "với nội dung" + getListView().getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
}
主要活动:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar ab = getActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ab.addTab(ab.newTab().setText("List").setTabListener(this));
ab.addTab(ab.newTab().setText("Custom").setTabListener(this));
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
if(tab.getPosition()==0){
Tab1Fragment tab1 = new Tab1Fragment();
getFragmentManager().beginTransaction().replace(R.id.container, tab1).commit();
} else if(tab.getPosition()==1){
Tab2Fragment tab2 = new Tab2Fragment();
getFragmentManager().beginTransaction().replace(R.id.container,tab2).commit();
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
我的应用针对的是API 14:Android 4.0(IceCreamSandwich)。我发现3天,并且无法理解为什么这个应用程序在启动时停止。
11-26 14:23:01.544 3417-3417/? E/libprocessgroup: failed to make and chown /acct/uid_10068: Read-only file system
11-26 14:23:01.544 3417-3417/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
11-26 14:23:01.545 3417-3417/? I/art: Late-enabling -Xcheck:jni
11-26 14:23:01.692 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.053 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.089 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.111 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.132 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.149 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.166 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.182 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.199 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.215 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.232 3417-3417/com.example.hoang.b13 W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.hoang.b13-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.hoang.b13-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
11-26 14:23:02.233 3417-3417/com.example.hoang.b13 W/System: ClassLoader referenced unknown path: /data/app/com.example.hoang.b13-1/lib/x86
11-26 14:23:02.235 3417-3417/com.example.hoang.b13 I/InstantRun: starting instant run server: is main process
11-26 14:23:02.271 3417-3417/com.example.hoang.b13 D/AndroidRuntime: Shutting down VM
11-26 14:23:02.272 3417-3417/com.example.hoang.b13 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hoang.b13, PID: 3417
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hoang.b13/com.example.hoang.b13.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
at com.example.hoang.b13.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-26 14:23:03.996 3417-3417/? I/Process: Sending signal. PID: 3417 SIG: 9
答案 0 :(得分:0)
您必须扩展ActionBarActivity而不是FragmentActivity以使Actionbar包含片段。
仍然尝试下面的代码
ActionBar actionBar = (ActionBarActivity)getActivity().getSupportActionBar();
更多细节见此 http://developer.android.com/training/basics/fragments/creating.html