操作栏产生NullPointerException

时间:2016-08-19 11:24:55

标签: android nullpointerexception

解决方案: 我在互联网上尝试了所有可用的东西,没有什么对我有用。然后我找到了解决方案。我的项目中没有values-v23文件夹。如果你没有,那就做吧!并在Values-v23文件夹的style.xml中添加它

    <resources>

   <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
       <item name="windowActionBar">true</item>
        <item name="windowNoTitle">false</item>
    </style>
</resources>

我有飞溅屏幕和滑块,它覆盖了操作栏和android studi不知怎的混淆,我的动作栏返回null。经过2周的研究,我发现了一个似乎没有人指出的小问题。我编辑了这行

        android:theme="@style/AppTheme"> 

我正在使用的实际主题即

                           <android:theme="@style/Theme.AppCompat.Light.DarkActionBar"> 

它对我有用..

问题 我有这个应用程序完全正常,直到我做了一些更改 我恢复了所有的更改,但现在我在ActionBar上得到了NUllPointerException

 ActionBar ab = getSupportActionBar();

    if (ab != null) {
        ab.setLogo(R.mipmap.icon);
        ab.setDisplayUseLogoEnabled(true); //shows logo
        ab.setDisplayShowHomeEnabled(true); //shows home page
    }
    else {
        Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();

    }

我把if else语句用来检查问题所在。它仅运行else部分。主屏幕代码就是这个

package com.example.dell.optasia;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.media.MediaPlayer;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v4.view.GestureDetectorCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
import static android.view.GestureDetector.OnDoubleTapListener;
import static android.view.GestureDetector.OnGestureListener;
import static com.example.dell.optasia.R.id.editText;

public class HomeScreen extends AppCompatActivity implements
        OnGestureListener, OnDoubleTapListener {
    //used for text to speech
    TextToSpeech ttsobject;
    int result;
    TextView et;
    String text;
    String texttoplay;
    //uptil here
    private GestureDetectorCompat GestureDetect;
    private static TextView Viewtext;
    //used for speech to text
    public static final int REQUEST_OK = 1;
    public static TextView finalRes;
    public static ImageView imgBtn;
    //uptill here
    public MediaPlayer mp1;

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

    mp1 = MediaPlayer.create(HomeScreen.this, R.raw.startup);
    mp1.start();
    ActionBar ab = getSupportActionBar();

    if (ab != null) {
        ab.setLogo(R.mipmap.icon);
        ab.setDisplayUseLogoEnabled(true); //shows logo
        ab.setDisplayShowHomeEnabled(true); //shows home page
    }
    else {
        Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();

    }

    //for tap and hold and all other gestures
    Viewtext = (TextView) findViewById(R.id.textView);
    //onlongpress code needs this
    GestureDetect = new GestureDetectorCompat(this, this);
    GestureDetect.setOnDoubleTapListener(this);
    //text to speech code here
    et = (TextView) findViewById(editText);

    ttsobject = new TextToSpeech(HomeScreen.this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                result = ttsobject.setLanguage(Locale.UK);
            } else {
                Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();
            }
        }
    });

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

package="com.example.dell.optasia">
<uses-sdk android:minSdkVersion="11" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Splashscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".HomeScreen">
        <intent-filter>
            <action android:name="com.example.dell.optasia.HomeScreen" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".AboutUs">
        <intent-filter>
            <action android:name="com.example.dell.optasia.AboutUs" />

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

Style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">true</item>

<item name="windowNoTitle">true</item>

</style>


<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />


<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

1 个答案:

答案 0 :(得分:0)

@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout...);

// experiment with the ActionBar 
ActionBar actionBar = getSupportActionBar();

}