如果我把标题栏删除下面的源代码导致adt下降(不幸的是,已经停止)

时间:2013-07-09 11:01:50

标签: android title logcat

**MainActivity**
package com.example.tabtest;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class TabFragTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//  requestWindowFeature(Window.FEATURE_NO_TITLE);
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    actionBar.addTab(actionBar
            .newTab()
            .setText("Tab1")
            .setTabListener(
                    new TabListener<FragmentTab1>(this, "tab1",
                            FragmentTab1.class)));
    actionBar.addTab(actionBar
            .newTab()
            .setText("Tab2")
            .setTabListener(
                    new TabListener<FragmentTab2>(this, "tab3",
                            FragmentTab2.class)));
    actionBar.addTab(actionBar
            .newTab()
            .setText("Tab3")
            .setTabListener(
                    new TabListener<FragmentTab3>(this, "tab3",
                            FragmentTab3.class)));
    if (savedInstanceState != null) {
        actionBar.setSelectedNavigationItem(savedInstanceState
                .getInt("selectedTab", 0));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("selectedTab", getActionBar()
            .getSelectedNavigationIndex());
}

}

**TabListener**
package com.example.tabtest;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.widget.Toast;

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private Fragment mFragment;

public TabListener(Activity activity, String tag, Class<T> clz) {
    mActivity = activity;
    mTag = tag;
    mClass = clz;
    mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
    if (mFragment != null && !mFragment.isDetached()) {
        FragmentTransaction fragmentTransaction = mActivity
                .getFragmentManager().beginTransaction();
        fragmentTransaction.detach(mFragment);
        fragmentTransaction.commit();
    }
}

public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
    if (mFragment == null) {
        mFragment = Fragment.instantiate(mActivity, mClass.getName(),
                null);
        fragmentTransaction
                .add(android.R.id.content, mFragment, mTag);
    } else {
        fragmentTransaction.attach(mFragment);
    }
}

public void onTabUnselected(Tab tab,
        FragmentTransaction fragmentTransaction) {
    if (mFragment != null) {
        fragmentTransaction.detach(mFragment);
    }
}

public void onTabReselected(Tab tab,
        FragmentTransaction fragmentTransaction) {
    Toast.makeText(mActivity, "onTabReselected!", Toast.LENGTH_SHORT)
            .show();
}

}

**tabview1**
package com.example.tabtest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentTab1 extends Fragment {
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.simple, container, false); 
    View tv = v.findViewById(R.id.text); 
    ((TextView) tv).setText("1");
    return v;
}

}

**tabview2**
package com.example.tabtest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentTab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.simple, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView) tv).setText("2.");
    return v;
}

}

**tabview3**
package com.example.tabtest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentTab3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.simple, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView) tv).setText("3");
    return v;
}

}

**xml**
<RelativeLayout 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: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=".TabFragTest" >

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

}

**manifest**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tabtest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
//  android:theme="@android:style/Theme.NoTitleBar"
    >
    <activity
        android:name="com.example.tabtest.TabFragTest"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

logcat的

07-09 09:32:29.953:D / gralloc_goldfish(770):未检测到GPU仿真的仿真器。 07-09 09:59:45.604:E / Trace(819):错误打开跟踪文件:没有这样的文件或目录(2) 07-09 09:59:46.0​​03:D / AndroidRuntime(819):关闭VM 07-09 09:59:46.0​​03:W / dalvikvm(819):threadid = 1:线程退出未捕获异常(组= 0x40a13300) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):致命异常:主要 07-09 09:59:46.0​​13:E / AndroidRuntime(819):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.tabtest / com.example.tabtest.TabFragTest}:java.lang.NullPointerException 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.ActivityThread.access $ 600(ActivityThread.java:130) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.os.Handler.dispatchMessage(Handler.java:99) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.os.Looper.loop(Looper.java:137) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.ActivityThread.main(ActivityThread.java:4745) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):at java.lang.reflect.Method.invokeNative(Native Method) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):at java.lang.reflect.Method.invoke(Method.java:511) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):at dalvik.system.NativeStart.main(Native Method) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):引起:java.lang.NullPointerException 07-09 09:59:46.0​​13:E / AndroidRuntime(819):at com.example.tabtest.TabFragTest.onCreate(TabFragTest.java:14) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.Activity.performCreate(Activity.java:5008) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 07-09 09:59:46.0​​13:E / AndroidRuntime(819):... 11更多

请帮帮我

1 个答案:

答案 0 :(得分:0)

  

引起:java.lang.NullPointerException .... at   com.example.tabtest.TabFragTest.onCreate(TabFragTest.java:14)

您在NullPointerException的第14行获得TabFragTest。 这是导致异常的行:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

由于actionBar为null,因此在尝试使用此对象时会抛出NPE。 它为空,因为getActionBar();没有返回操作栏。

Android 3.0中添加了

ActionBar。因此,这仅适用于Android 3.0及更高版本,在Android 2.3及更低版本中,您将获得该异常。

然而,您可以use the SherlockActionBar与2.x及以上的每个Android版本兼容。