1

Java で、オブジェクトに値があるか、または返されているかを確認する最良の方法は何nullですか? 私が見つけたほとんどの例はあまり良くありません。基本的に私はこのコードを持っています:

mDBApi.getSession().setAccessTokenPair(reAuthTokens);
System.out.println(reAuthTokens);
if(reAuthTokens.equals(null)) {
    mDBApi.getSession().startAuthentication(Main.this);
    Log.e(TAG, "Keys not set -- I'm starting authentication");
}

reAuthTokens値をチェックしようとしていますが、値がない場合は、先に進んで認証してください。ただし、NullPointerExceptionif ステートメントの行に a が表示されるだけです。もっとうまくできることはありますか?

__ _ __ _ __ _ _ _ rcookOnCreate _ __ _ __ _ _ __ _ __ _ _ __ _ __

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

    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);


    //AccessTokenPair tokens=null;      
    //AccessTokenPair tokens = getStoredKeys();   
    //System.out.println(access + "here I am"); 
    //clearKeys();
    //Log.e(TAG, "keys cleared");


    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    AccessTokenPair reAuthTokens = new AccessTokenPair(APP_KEY, APP_SECRET);
    mDBApi.getSession().setAccessTokenPair(reAuthTokens);

        System.out.println(reAuthTokens);

    if(reAuthTokens == null) {       
    mDBApi.getSession().startAuthentication(Main.this);
    Log.e(TAG, "Keys not set -- I'm starting authentication");



    }


//



    /*read settings*/
    mSettings = getSharedPreferences(PREFS_NAME, 0);
    boolean hide = mSettings.getBoolean(PREFS_HIDDEN, false);
    boolean thumb = mSettings.getBoolean(PREFS_THUMBNAIL, true);
    int space = mSettings.getInt(PREFS_STORAGE, View.VISIBLE);
    int color = mSettings.getInt(PREFS_COLOR, -1);
    int sort = mSettings.getInt(PREFS_SORT, 3);



    mFileMag = new FileManager();
    mFileMag.setShowHiddenFiles(true);
    mFileMag.setSortType(sort);

    if (savedInstanceState != null)
        mHandler = new EventHandler(Main.this, mFileMag, savedInstanceState.getString("location"));
    else
        mHandler = new EventHandler(Main.this, mFileMag);

    mHandler.setTextColor(color);
    mHandler.setShowThumbnails(thumb);
    mTable = mHandler.new TableRow();

    /*sets the ListAdapter for our ListActivity and
     *gives our EventHandler class the same adapter
     */
    mHandler.setListAdapter(mTable);
    setListAdapter(mTable);

    /* register context menu for our list view */
    registerForContextMenu(getListView());

    mStorageLabel = (TextView)findViewById(R.id.storage_label);
    mDetailLabel = (TextView)findViewById(R.id.detail_label);
    mPathLabel = (TextView)findViewById(R.id.path_label);
    mPathLabel.setText("path: /sdcard");

    updateStorageLabel();
    mStorageLabel.setVisibility(space);

    mHandler.setUpdateLabels(mPathLabel, mDetailLabel);

    /* setup buttons */
    int[] img_button_id = {R.id.help_button, R.id.home_button, 
                           R.id.back_button, R.id.info_button, 
                           R.id.manage_button, R.id.multiselect_button,
                           R.id.dropbox_button
                           };

    int[] button_id = {R.id.hidden_copy, R.id.hidden_attach,
                       R.id.hidden_delete, R.id.hidden_move};

    ImageButton[] bimg = new ImageButton[img_button_id.length];

    Button[] bt = new Button[button_id.length];

    for(int i = 0; i < img_button_id.length; i++) {
        bimg[i] = (ImageButton)findViewById(img_button_id[i]);
        bimg[i].setOnClickListener(mHandler);

        if(i < 4) {
            bt[i] = (Button)findViewById(button_id[i]);
            bt[i].setOnClickListener(mHandler);
        }
    }

    Intent intent = getIntent();

    if(intent.getAction().equals(Intent.ACTION_GET_CONTENT)) {
        bimg[5].setVisibility(View.GONE);
        mReturnIntent = true;

    } else if (intent.getAction().equals(ACTION_WIDGET)) {
        Log.e("MAIN", "Widget action, string = " + intent.getExtras().getString("folder"));
        mHandler.updateDirectory(mFileMag.getNextDir(intent.getExtras().getString("folder"), true));

    }
}
4

1 に答える 1

5

if (reAuthTokens == null))代わりに使用してください。オブジェクトの内容を比較しようとしているのではありません。参照を比較しようとしています。「は?reAuthTokensと同じアドレスを指していnullます」

OPからの次の更新を編集reAuthTokensします:タイプAccessTokenPairです(そして私は多くの読者が最初にこれをList...と思っていたに違いありません)。次の行でインスタンス化されます。

AccessTokenPair reAuthTokens = new AccessTokenPair(APP_KEY, APP_SECRET);

そのため、次の条件は常にfalseになりますreAuthTokens == null。そのため、コーディング時に「デッドコード」の警告が表示されます。コンパイラは、上記の数行if (reAuthTokens == null)をインスタンス化すると、この条件が真になることはないことを認識しています。reAuthTokens

したがって、あなたが求めている比較のタイプは、参照ではなく、コンテンツに関するものです。reAuthTokens「空」かどうかを確認したい。しかし、それはあなたが引用したコードからは意味がありません。オブジェクトをインスタンス化して、それが「空」であるかどうかを確認したいのはなぜですか。

あなたの論理は正しくないと思います。最初に、期待する場所(セッション?)からアクセストークンペアを取得し、その結果をと比較する必要がありnullます。このようなもの:

AccessTokenPair reAuthTokens = mDBApi.getSession().getAccessTokenPair();

if (reAuthTokens == null) {
    reAuthTokens = new AccessTokenPair(...);
    mDBApi.getSession().setAccessTokenPair(reAuthTokens);
}
于 2013-02-21T02:01:00.940 に答える