2017-09-18 6 views
0

この質問はまだ尋ねられていません。フラグメント内のアクティビティからブール値を取得

/*fragment*/ 
((MainActivity) getActivity()).yourMethod(); 

どのように実行します。

は私が

/*MainActivity*/ 
boolean hasLowStorage(){ 
    IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); 
    return registerReceiver(null, lowStorageFilter) != null; 
} 

活動からvoidメソッドを呼び出して、我々はこれを行うに私の活動で携帯電話のストレージをチェックして、私のフラグメントに返された値を取得する必要がありますブール値の大文字小文字の値をフラグメントから求めますか?

答えて

1

スコープの変更、再試行してください。

// MainActivity

public boolean hasLowStorage(){ 
     IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); 
     return registerReceiver(null, lowStorageFilter) != null; 
    } 

//フラグメント

boolean bolValue = ((MainActivity) getActivity()).hasLowStorage(); 
1

これを試してみてください:公衆へのメソッドの

boolean yourValue = ((MainActivity) getActivity()).hasLowStorage(); 
+0

すでに試しました。 "シンボルを解決できません" – Lucem

0

それは公共の静的なこの

/*MainActivity*/ 
public static boolean hasLowStorage(){ 
IntentFilter lowStorageFilter = new 
IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); 
return registerReceiver(null, lowStorageFilter) != null; 
} 

ようにし、

boolean yourValue = 
MainActivity.hasLowStorage(); 
のようにそれを呼び出します

ハッピーコーディング:)

関連する問題