我想在操作欄應用程序標題中使用自定義字體,但每次遇到同樣的錯誤(空指針異常),我的應用程序都會對我說「Unfortunately **** has stopped
」。我搜查了很多並試圖使用它們。但沒有工作。同樣的錯誤不斷髮生。我創建了一個簡單的佈局,一個TextView,並試圖通過這條線如何在android中的操作欄中設置自定義視圖?
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.custom_actionbar);
無論我做什麼,在setCustomView
送我的空指針錯誤插入的動作條。我在想,如果我可以在操作欄中設置佈局,將很容易改變TextView字體。這是我的完整代碼。
public class MainActivity extends ActionBarActivity {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.custom_actionbar);
}
}
和佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<TextView
android:id="@+id/App_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textSize="40dp"
android:maxLines="2"
android:ellipsize="end"
android:text="Prime" />
</RelativeLayout>
和錯誤:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tushar.primeplay2/com.example.tushar.primeplay2.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tushar.primeplay2.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
我使用Android的工作室。
您正在使用支持庫導入? – Rohit5k2 2014-12-27 19:20:51