java.lang.RuntimeException:无法启动活动

时间:2016-03-06 10:58:02

标签: android

您正在练习使用Android Studio为应用添加徽标。我添加了如下简单代码。

package com.example.admin.actionbardemo;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionbar=  getActionBar();
        actionbar.setIcon(R.mipmap.ic_launcher);
        actionbar.setDisplayUseLogoEnabled(true);
        actionbar.setDisplayShowHomeEnabled(true);

    }
}

错误:

03-06 02:20:30.272 1381-1381/? E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.example.admin.actionbardemo, PID: 1381
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.actionbardemo/com.example.admin.actionbardemo.MainActivity}: java.lang.NullPointerException
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
         at android.app.ActivityThread.access$800(ActivityThread.java:135)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:136)
         at android.app.ActivityThread.main(ActivityThread.java:5001)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:515)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
         at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.NullPointerException
         at com.example.admin.actionbardemo.MainActivity.onCreate(MainActivity.java:21)
         at android.app.Activity.performCreate(Activity.java:5231)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
         at android.app.ActivityThread.access$800(ActivityThread.java:135) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:136) 
         at android.app.ActivityThread.main(ActivityThread.java:5001) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:515) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
         at dalvik.system.NativeStart.main(Native Method) 
03-06 02:20:30.276 1381-1384/? D/dalvikvm: GC_CONCURRENT freed 205K, 10% free 3023K/3340K, paused 5ms+0ms, total 6ms

3 个答案:

答案 0 :(得分:0)

您的getActionBar()返回null。我想你没有。

答案 1 :(得分:0)

这是androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.actionbardemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AddingLogo"
            android:label="@string/title_activity_adding_logo"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

我使用了android.support.v7.app.ActionBar actionBar = getSupportActionBar();

insted of ActionBar actionbar = getActionBar();

另一个查询-my徽标位于Drawable文件夹中。标志PNG或JPG的扩展需要什么?

答案 2 :(得分:0)

您正在使用的主题没有ActionBar。您必须将styles.xml中的AppTheme的父级更改为包含ActionBar的主题,或者在调用setSupportActionBar(...)之前将工具栏设置为getSupportActionBar()