- Tab1Fragment.java & Tab2Fragment is the same 公共类Tab1Fragment扩展了ListFragment{ String list[] = new String[]{ "apple“、"bluecherry”、"cherry“、"coconut”};(非Javadoc)*@见android.app.ListFragment#onCreateView(android.view.LayoutInflater,android.view.ViewGroup,android.os.Bundle) */ @Nullable @重写公共视图onCreateView(LayoutInflater inflater,@Nullable ViewGroup容器,Bundle savedInstanceState) { // TODO自动生成的方法存根视图视图= inflater.inflate(R.layout.tablist,容器,false);ListAdapter适配器=新ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,list);setListAdapter(适配器);返回视图;}@覆盖公共onListItemClick(ListView l,View v,int位置,long id) { Toast.makeText(getActivity(),"Vịtrívừa chọn:“+Toast.makeText+,”+ "vớiội dung“+ Toast.LENGTH_SHORT).show();}
- Main Activity:
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>
我的应用程序的目标是API14:Android4.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
发布于 2017-11-26 07:36:27
您必须扩展ActionBarActivity而不是FragmentActivity,才能使Actionbar具有片段。
仍然试图在下面的代码
ActionBar actionBar = (ActionBarActivity)getActivity().getSupportActionBar();更多细节请参见此http://developer.android.com/training/basics/fragments/creating.html
https://stackoverflow.com/questions/47493886
复制相似问题