Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在后台线程中运行onNavigationItemSelected方法_Java_Android_Multithreading_Navigation Drawer - Fatal编程技术网

Java 如何在后台线程中运行onNavigationItemSelected方法

Java 如何在后台线程中运行onNavigationItemSelected方法,java,android,multithreading,navigation-drawer,Java,Android,Multithreading,Navigation Drawer,当我写入mNavigationView.setNavigationItemSelectedListener时,应用程序崩溃(此);在代码中 如果未添加,则会删除导航栏中项目的功能。我应该在后台线程中运行哪个方法来减少对主线程的影响 import android.app.Activity; import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; import an

当我写入mNavigationView.setNavigationItemSelectedListener时,应用程序崩溃(此);在代码中 如果未添加,则会删除导航栏中项目的功能。我应该在后台线程中运行哪个方法来减少对主线程的影响

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.content.Context;
import android.content.Intent;

import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.eggheadgames.aboutbox.AboutConfig;
import com.eggheadgames.aboutbox.IAnalytic;
import com.eggheadgames.aboutbox.IDialog;
import com.eggheadgames.aboutbox.activity.AboutActivity;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class EarthquakeActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener, SwipeRefreshLayout.OnRefreshListener, LoaderManager.LoaderCallbacks<List<Earthquake>>, NavigationView.OnNavigationItemSelectedListener {
    public static final String MyPrefs = "MyPrefs";
    private DrawerLayout mdrawerlayout;
    private ActionBarDrawerToggle mToogle;
    /** URL for earthquake data from the USGS dataset */
    private static final String USGS_REQUEST_URL =     "https://earthquake.usgs.gov/fdsnws/event/1/query";

    /**
     * Constant value for the earthquake loader ID. We can choose any integer.
     * This really only comes into play if you're using multiple loaders.
     */
    private static final int EARTHQUAKE_LOADER_ID = 1;

    /** Adapter for the list of earthquakes */
    private EarthquakeAdapter mAdapter;

    /** TextView that is displayed when the list is empty */
    private TextView mEmptyStateTextView;

    SwipeRefreshLayout swipe;

    private static final String LOG_TAG = EarthquakeActivity.class.getSimpleName();


    private ListView earthquakeListView;

    private static final String TWITTER_USER_NAME = "vaibhav_khulbe";
    private static final String WEB_HOME_PAGE = "https://about.me/vaibhav_khulbe";
    private static final String APP_PUBLISHER = "https://play.google.com/store/apps/developer?id=Vaibhav%20Khulbe&hl=en";
    private static final String EMAIL_ADDRESS = "khulbevaibhavdev@gmail.com";
    private static final String EMAIL_SUBJECT = "Quake Info app acknowledgements and/or issues";
    private static final String EMAIL_BODY = "Please explain your experience with this app here...This may include bugs" +
            " or issues you may be facing or what you liked about the app along with improvements. :) (MAKE SURE to clear out these lines before sending the mail to us)";

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.earthquake_activity);
        mdrawerlayout=(DrawerLayout)findViewById(R.id.drawer);
        mToogle=new ActionBarDrawerToggle(this,mdrawerlayout,R.string.open,R.string.close);
        mdrawerlayout.addDrawerListener(mToogle);
        mToogle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        NavigationView mNavigationView=(NavigationView)findViewById(R.id.navigation_view);
        mNavigationView.setNavigationItemSelectedListener(this);

        swipe = findViewById(R.id.swiperefresh);
        swipe.setOnRefreshListener(this);
        swipe.setColorSchemeColors(getResources().getColor(R.color.colorAccent));

        /* Start the intro only once */
        SharedPreferences sp = getSharedPreferences(MyPrefs, Context.MODE_PRIVATE);
        if (!sp.getBoolean("first", false)) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("first", true);
            editor.apply();
            Intent intent = new Intent(this, IntroActivity.class);
            startActivity(intent);
        }

        //Call and launch About activity
        initAboutActivity();

        // Find a reference to the {@link ListView} in the layout
        earthquakeListView = (ListView) findViewById(R.id.list);

        mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
        earthquakeListView.setEmptyView(mEmptyStateTextView);

        // Create a new adapter that takes an empty list of earthquakes as input
        mAdapter = new EarthquakeAdapter(this, new ArrayList<Earthquake>());

        // Set the adapter on the {@link ListView}
        // so the list can be populated in the user interface
        earthquakeListView.setAdapter(mAdapter);

        // Obtain a reference to the SharedPreferences file for this app
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        // And register to be notified of preference changes
        // So we know when the user has adjusted the query settings
        prefs.registerOnSharedPreferenceChangeListener(this);

        // Set an item click listener on the ListView, which sends an intent to a web browser
        // to open a website with more information about the selected earthquake.
        earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                // Find the current earthquake that was clicked on
                Earthquake currentEarthquake = mAdapter.getItem(position);

                // Convert the String URL into a URI object (to pass into the Intent constructor)
                Uri earthquakeUri = Uri.parse(currentEarthquake.getUrl());

                // Create a new intent to view the earthquake URI
                Intent websiteIntent = new Intent(Intent.ACTION_VIEW, earthquakeUri);

                // Send the intent to launch a new activity
                startActivity(websiteIntent);
            }
        });
        getSupportLoaderManager().initLoader(EARTHQUAKE_LOADER_ID, null, this);

    }



    /*Code to launch About activity */
    public void initAboutActivity()
    {
        /* Create About activity */
        AboutConfig aboutConfig = AboutConfig.getInstance();
        aboutConfig.appName = getString(R.string.app_name);
        aboutConfig.appIcon = R.mipmap.ic_launcher;
        aboutConfig.version = "1.0.0";
        aboutConfig.author = "Vaibhav Khulbe";
        aboutConfig.aboutLabelTitle = "About";
        aboutConfig.packageName = getApplicationContext().getPackageName();

        aboutConfig.appPublisher = APP_PUBLISHER;

        aboutConfig.twitterUserName = TWITTER_USER_NAME;
        aboutConfig.webHomePage = WEB_HOME_PAGE;


        aboutConfig.dialog = new IDialog() {
            @Override
            public void open(AppCompatActivity appCompatActivity, String url, String tag) {
                // handle custom implementations of WebView. It will be called when user click to web items. (Example: "Privacy", "Acknowledgments" and "About")
            }
        };

        aboutConfig.analytics = new IAnalytic() {
            @Override
            public void logUiEvent(String s, String s1) {
                // handle log events.
            }

            @Override
            public void logException(Exception e, boolean b) {
                // handle exception events.
            }
        };
        // set it only if aboutConfig.analytics is defined.
        aboutConfig.logUiEventName = "Log";

        // Contact Support email details
        aboutConfig.emailAddress = EMAIL_ADDRESS;
        aboutConfig.emailSubject = EMAIL_SUBJECT;
        aboutConfig.emailBody = EMAIL_BODY;


        aboutConfig.shareMessage = getString(R.string.share_message);
        aboutConfig.sharingTitle = getString(R.string.sharing_title);

    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equals(getString(R.string.settings_min_magnitude_key)) ||
                key.equals(getString(R.string.settings_order_by_key))){
            // Clear the ListView as a new query will be kicked off
            mAdapter.clear();

            // Hide the empty state text view as the loading indicator will be displayed
            mEmptyStateTextView.setVisibility(View.GONE);

            // Show the loading indicator while new data is being fetched
            View loadingIndicator = findViewById(R.id.loading_indicator);
            loadingIndicator.setVisibility(View.VISIBLE);

            // Restart the loader to requery the USGS as the query settings have been updated
            getSupportLoaderManager().restartLoader(EARTHQUAKE_LOADER_ID, null, this);
        }
    }

    @Override
    public Loader<List<Earthquake>> onCreateLoader(int i, Bundle bundle) {

         SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        String minMagnitude = sharedPrefs.getString(
                getString(R.string.settings_min_magnitude_key),
                getString(R.string.settings_min_magnitude_default));

        String orderBy = sharedPrefs.getString(
                getString(R.string.settings_order_by_key),
                getString(R.string.settings_order_by_default)
        );

        String region = sharedPrefs.getString(
                getString(R.string.settings_narrow_by_region_key),
                getString(R.string.settings_narrow_by_region_default)
        );

        String radius = sharedPrefs.getString(
                getString(R.string.settings_maximum_radius_key),
                getString(R.string.settings_maximum_radius_default)
        );

        List<Country> countries = new ArrayList<>();

        try {
            countries = Utils.generateCountryList(this);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Double latitude = 0.0;
        Double longitude = 0.0;
        for (Country country : countries) {
            if(country.getName().equalsIgnoreCase(region)){
                latitude = country.getLatitude();
                longitude = country.getLongitude();
            }
        }

        Uri baseUri = Uri.parse(USGS_REQUEST_URL);
        Uri.Builder uriBuilder = baseUri.buildUpon();

        uriBuilder.appendQueryParameter("format", "geojson");
        uriBuilder.appendQueryParameter("limit", "100");
        uriBuilder.appendQueryParameter("minmag", minMagnitude);
        uriBuilder.appendQueryParameter("orderby", orderBy);

        if(latitude != 0.0 && longitude != 0.0){
            uriBuilder.appendQueryParameter("latitude", String.valueOf(latitude.intValue()));
            uriBuilder.appendQueryParameter("longitude", String.valueOf(longitude.intValue()));
            uriBuilder.appendQueryParameter("maxradius", radius);
        }

        String url = uriBuilder.toString();
        return new EarthquakeLoader(this, url);
    }




    @Override
    public void onLoadFinished(Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
        swipe.setRefreshing(false);

        // Hide loading indicator because the data has been loaded
        View loadingIndicator = findViewById(R.id.loading_indicator);
        loadingIndicator.setVisibility(View.GONE);



        if (earthquakes != null && !earthquakes.isEmpty()) {
            this.showResults(earthquakes);
        } else {
            this.hideResults();
        }
    }

    @Override
    public void onLoaderReset(Loader<List<Earthquake>> loader) {
        // Loader reset, so we can clear out our existing data.
        mAdapter.clear();
    }

    /**
     * method to show results
     */
    private void showResults(List<Earthquake> earthquakeList) {
        mAdapter.clear();
        earthquakeListView.setVisibility(View.VISIBLE);
        mEmptyStateTextView.setVisibility(View.GONE);
        mAdapter.setNotifyOnChange(false);
        mAdapter.setNotifyOnChange(true);
        mAdapter.addAll(earthquakeList);
    }

    /**
     * method to hide results also checks internet connection
     */
    private void hideResults() {
        earthquakeListView.setVisibility(View.GONE);
        mEmptyStateTextView.setVisibility(View.VISIBLE);
        // Get a reference to the ConnectivityManager to check state of network connectivity
        ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);

        // Get details on the currently active default data network
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            mEmptyStateTextView.setText(R.string.no_earthquakes);
            Log.e(LOG_TAG, "no earthquakes data");

        } else {
            mEmptyStateTextView.setText(R.string.no_internet_connection);
            Log.e(LOG_TAG, "no internet");
        }
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToogle.onOptionsItemSelected(item)){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onRefresh() {
        getSupportLoaderManager().restartLoader(EARTHQUAKE_LOADER_ID, null, this);
        Toast.makeText(this, R.string.list_refreshed, Toast.LENGTH_SHORT).show();

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            Intent settingsIntent = new Intent(this, SettingsActivity.class);
            startActivity(settingsIntent);
            return true;
        }
        if (id == R.id.action_about) {
            Intent actionIntent = new Intent(this, AboutActivity.class);
            startActivity(actionIntent);
            return true;
        }
        if (id == R.id.action_did_you_feel_it){

            Intent feelItIntent = new Intent(this, DidYouFeel.class);
            startActivity(feelItIntent);
            return true;
        }
        if (id == R.id.action_more_apps){

            Uri uri = Uri.parse( "https://play.google.com/store/apps/developer?id=Vaibhav+Khulbe" );
            startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
        }
        if (id == R.id.fork_project){

            Uri uri = Uri.parse( "https://github.com/Kvaibhav01/Quake-Info" );
            startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
        }

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

            Intent notificationIntent = new Intent(this, EarthquakeNotification.class);
            startActivity(notificationIntent);
        }
        return true;
    }

}
导入android.app.Activity;
导入android.support.annotation.NonNull;
导入android.support.design.widget.NavigationView;
导入android.support.v4.widget.DrawerLayout;
导入android.support.v4.widget.swiperFreshLayout;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.net.Uri;
导入android.os.Bundle;
导入android.preference.PreferenceManager;
导入android.support.v7.app.ActionBarDrawerToggle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.support.v4.app.LoaderManager;
导入android.support.v4.content.Loader;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.eggheadcames.aboutbox.AboutConfig;
导入com.eggheadgames.aboutbox.ianAnalytic;
导入com.eggheadcames.aboutbox.IDialog;
导入com.eggheadgames.aboutbox.activity.AboutActivity;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类EarthquakeActivity扩展AppCompatActivity实现SharedReferences.OnSharedPreferenceChangeListener、SwiperFreshLayout.OnRefreshListener、LoaderManager.LoaderCallbacks、NavigationView.OnNavigationItemSelectedListener{
公共静态最终字符串MyPrefs=“MyPrefs”;
私人抽屉布局mdrawerlayout;
私人行动bardrawertoggle mToogle;
/**美国地质勘探局数据集地震数据的URL*/
私有静态最终字符串USGS\U请求\U URL=”https://earthquake.usgs.gov/fdsnws/event/1/query";
/**
*地震加载程序ID的常量值。我们可以选择任何整数。
*这只有在您使用多个加载程序时才起作用。
*/
专用静态最终int地震加载程序ID=1;
/**地震列表的适配器*/
私人地震适配器;
/**列表为空时显示的文本视图*/
私有文本视图mEmptyStateTextView;
轻扫布局轻扫;
私有静态最终字符串LOG_TAG=EarthquakeActivity.class.getSimpleName();
私有ListView earthquakeListView;
私有静态最终字符串TWITTER\u USER\u NAME=“vaibhav\u khulbe”;
私有静态最终字符串WEB\u主页=”https://about.me/vaibhav_khulbe";
私有静态最终字符串APP_PUBLISHER=”https://play.google.com/store/apps/developer?id=Vaibhav%20Khulbe&hl=en";
私有静态最终字符串电子邮件地址=”khulbevaibhavdev@gmail.com";
私有静态最终字符串电子邮件\u SUBJECT=“地震信息应用程序确认和/或问题”;
private static final String EMAIL_BODY=“请在此处解释您使用此应用程序的体验……这可能包括bug”+
“或者您可能面临的问题,或者您喜欢该应用程序的哪些方面以及改进::(在向我们发送邮件之前,请确保清除这些行)”;
工具栏;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、地震活动);
mdrawerlayout=(抽屉布局)findViewById(R.id.drawer);
mToogle=newactionBarDrawerToggle(this,mdrawerlayout,R.string.open,R.string.close);
mdrawerlayout.addDrawerListener(mToogle);
mToogle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView mNavigationView=(NavigationView)findViewById(R.id.navigation\u视图);
mNavigationView.setNavigationItemSelectedListener(此);
swipe=findviewbyd(R.id.swiperfresh);
swipe.setOnRefreshListener(此);
swipe.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
/*只开始一次介绍*/
SharedReferences sp=GetSharedReferences(MyPrefs,Context.MODE\u PRIVATE);
如果(!sp.getBoolean(“first”,false)){
SharedReferences.Editor=sp.edit();
编辑:putBoolean(“第一”,真);
editor.apply();
Intent Intent=新的Intent(这个,IntroActivity.class);
星触觉(意向);
}
//关于活动的电话和启动
初始触觉();
//在布局中查找对{@link ListView}的引用
earthquakeListView=(ListView)findViewById(R.id.list);
mEmptyStateTextView=(TextView)findViewById(R.id.empty\u视图);
earthquakeListView.setEmptyView(mEmptyStateTextView);
//创建一个新适配器,将地震的空列表作为输入
mAdapter=新的地震适配器(这是新的ArrayList());
//在{@link ListView}上设置适配器
//因此,可以在用户界面中填充列表
earthquakeListView.setAdapter(mAdapter);
//获取对此应用程序的SharedReferences文件的引用
SharedPreferences=PreferenceManager.getDefaultSharedPreferences(此);
//并注册以获得偏好变更的通知
//因此,我们知道用户何时调整了查询设置
prefs.RegisterOnSharedReferenceChangeListener(此);
//在ListView上设置一个项目单击侦听器,该侦听器将意向发送到web浏览器
//打开包含所选地震的更多信息的网站。
earthquakeListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共链接(AdapterView)
 ((MainActivity)context).runOnUiThread(new Runnable() {
    public void run() {
      //run another thread 
    }
});