0

我无法在我的操作栏中添加一个微调器来代替应用程序名称。我确定这个问题之前已经被问过,但是所有的答案几乎都是一样的,要么是简单的一行更正,要么是在谷歌开发网站上引用他们的一个可爱的例子:http: //developer.android.com/guide /topics/ui/actionbar.html#Dropdown android 文档,但对于初学者来说,我发现它太多了,并且遗漏了一些关键信息。

public class MainProgram extends Activity implements OnNavigationListener {

    /**
     * Mobile Service Client reference
     */
    private MobileServiceClient mClient;    
    private ConnectWithService service;

    /**
     * Progress spinner to use for table operations
     */
    private ProgressBar mProgressBar;

    /**
     * Sensor stuff
     */
    SensorManager mSensor;
    Detection orientation;

    //Spinner Listener
    mOnNavigationListener = new OnNavigationListener() {
          // Get the same strings provided for the drop-down's ArrayAdapter
          String[] strings = getResources().getStringArray(R.array.action_list);

          @Override
          public boolean onNavigationItemSelected(int position, long itemId) {
           //toast
              Toast toast = Toast.makeText(this, "whoop whoop!", toast.LENGTH_SHORT);
              toast.show();
          }
        };

    /**
     * Initializes the activity
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_program);

        //Load progress bar view
        mProgressBar = (ProgressBar) findViewById(R.id.loadingProgressBar);     
        // Initialize the progress bar
        mProgressBar.setVisibility(ProgressBar.GONE);

        //Spinner Adapter
        setNavigationMode(NAVIGATION_MODE_LIST);
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list,
                  android.R.layout.simple_spinner_dropdown_item);


        //innitialize Sensor Manager
        mSensor = (SensorManager)getSystemService(SENSOR_SERVICE);


        //signin
        try {
            // Create the Mobile Service Client instance, using the provided
            // Mobile Service URL and key
            mClient = new MobileServiceClient(
                    "secret",
                    "secret",
                    this).withFilter(new ProgressFilter());         

        } catch (MalformedURLException e) {
            createAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
        }
            authenticate();     
    }

    /**
     * Initializes the activity menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /**
     * Select an option from the menu
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_refresh) {

        }

        return true;
    }

    /**
     * Creates a dialog and shows it
     * 
     * @param exception
     *            The exception to show in the dialog
     * @param title
     *            The dialog title
     */
    private void createAndShowDialog(Exception exception, String title) {
        createAndShowDialog(exception.toString(), title);
    }

    /**
     * Creates a dialog and shows it
     * 
     * @param message
     *            The dialog message
     * @param title
     *            The dialog title
     */
    private void createAndShowDialog(String message, String title) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(message);
        builder.setTitle(title);
        builder.create().show();
    }

    private class ProgressFilter implements ServiceFilter {

        @Override
        public void handleRequest(ServiceFilterRequest request, NextServiceFilterCallback nextServiceFilterCallback,
                final ServiceFilterResponseCallback responseCallback) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (mProgressBar != null) mProgressBar.setVisibility(ProgressBar.VISIBLE);
                }
            });

            nextServiceFilterCallback.onNext(request, new ServiceFilterResponseCallback() {

                @Override
                public void onResponse(ServiceFilterResponse response, Exception exception) {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if (mProgressBar != null) mProgressBar.setVisibility(ProgressBar.GONE);
                        }
                    });

                    if (responseCallback != null)  responseCallback.onResponse(response, exception);
                }
            });
        }
    }

    private void authenticate() {

            // Login using the Google provider.
            mClient.login(MobileServiceAuthenticationProvider.MicrosoftAccount,
                 new UserAuthenticationCallback() {

                    @Override
                    public void onCompleted(MobileServiceUser user,
                            Exception exception, ServiceFilterResponse response) {

                        if (exception == null) {
                            createAndShowDialog(String.format(
                                            "You are now logged in - %1$2s",
                                            user.getUserId()), "Success");
                            service = new ConnectWithService(mClient,user.getUserId());
                            orientation = new Detection(mSensor);
                        } else {
                            createAndShowDialog("You must log in. Login Required", "Error");
                        }
                    }
                });
        }

     //start sensors camera etc if needed
      protected void onResume() {
            super.onResume();

            if(orientation!=null)
                orientation.startSensorsListening();
        }

      // stop sensors etc
        protected void onPause() {
            super.onPause();
            orientation.stopSensorListening();
        }

}

至少有两个错误我认为 mOnNavigationListener 应该在其他地方 setnavigationMode() 有错误

4

1 回答 1

0

不要将 OnNavigationListener 与 (ActionBar.NAVIGATION_MODE_LIST) 一起使用:

 final ActionBar bar = getActionBar();
     bar.setDisplayShowCustomEnabled(true);
     bar.setCustomView(R.layout.action_bar_custom);
then all as normal.

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?>....
于 2015-08-02T05:51:46.093 回答