Android:自API 24

时间:2016-09-26 04:57:45

标签: java android

我在Android Studio中从API 22更新到新版本24。 没有其他任何改变。但现在我遇到“setText”变量的问题。

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.something2156/com.app.something.webview.ModeratorPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

代码:

package com.app.something.webview;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.app.something.MainActivity;
import com.app.something.R;
import com.app.something.informations.WebradioConfig;

public class ModeratorPage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    WebView mWebView;
    String URL = WebradioConfig.RadioModeratorURL;
    ProgressBar loadingProgressBar, loadingTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_webview_moderator);

        TextView txtNavHeaderModeratorTitle = (TextView) findViewById(R.id.drawer_moderator_title);
        txtNavHeaderModeratorTitle.setText("Text 01");
        TextView txtNavHeaderModeratorDesc = (TextView) findViewById(R.id.drawer_moderator_desc);
        txtNavHeaderModeratorDesc.setText("Text 02");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        mWebView = (WebView) findViewById(R.id.content_webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setPluginState(PluginState.ON);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.clearCache(true);
        mWebView.loadUrl(URL);
        mWebView.setWebViewClient(new MyWebViewClient());
        loadingProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
        mWebView.setWebChromeClient(new WebChromeClient() {

            // this will be called on page loading progress
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                loadingProgressBar.setProgress(newProgress);
                //loadingTitle.setProgress(newProgress);
                // hide the progress bar if the loading is complete
                if (newProgress == 100) {
                    loadingProgressBar.setVisibility(View.VISIBLE);
                } else {
                    loadingProgressBar.setVisibility(View.VISIBLE);
                }
            }

        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (mWebView.canGoBack()) {
                        mWebView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_moderator, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_home) {
            Intent gonav_home = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(gonav_home);
            return true;
        }
        if (id == R.id.action_sharing) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, WebradioConfig.RadioSharingTextModeratorPage);
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, WebradioConfig.RadioShareWithTitle));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {

            Intent gonav_home = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(gonav_home);

        } else if (id == R.id.nav_moderators) {

            Intent gonav_moderators = new Intent(getApplicationContext(), ModeratorPage.class);
            startActivity(gonav_moderators);

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


}

以前没有问题。当我删除部分

TextView txtNavHeaderModeratorTitle = (TextView) findViewById(R.id.drawer_moderator_title);
txtNavHeaderModeratorTitle.setText("Text 01");
TextView txtNavHeaderModeratorDesc = (TextView) findViewById(R.id.drawer_moderator_desc);
txtNavHeaderModeratorDesc.setText("Text 02");

任何事情都有效,但这不能解决我的问题:(

activity_webview_moderator

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_moderator"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

nav_header_moderator

<?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="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="top"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin_drawer"
    android:paddingLeft="@dimen/activity_horizontal_margin_drawer"
    android:paddingRight="@dimen/activity_horizontal_margin_drawer"
    android:paddingTop="@dimen/activity_vertical_margin_drawer"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <TextView
        android:id="@+id/drawer_moderator_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:singleLine="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#ffffffff"
        android:textSize="22.0sp"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/drawer_moderator_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:enabled="false"
        android:gravity="center_vertical"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingLeft="2.0dip"
        android:paddingRight="2.0dip"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffffff"
        android:textSize="20.0sp" />

</LinearLayout>

希望有人可以帮助解决这个问题,谢谢。

0 个答案:

没有答案