iT邦幫忙

0

(android)Java Reflect 出了問題~"~

匿名 2011-04-28 22:27:059355 瀏覽

這是我用ListView範例改的,並參考How to expand the Status Bar in Android(http://www.xequae.com/node/4539)所以應是任意按其中的ItemA,ItemB,ItemC就會
達到我要的效果(擴展狀態列)..但..

package com.test123;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;  

public class test123 extends Activity implements 
OnItemClickListener{
	Method expandStatusBar;
	Object mStatusBarManager= new Object();
	Object statusBarManager = new Object();
	 public void onItemClick(AdapterView <?> arg0, 
	View arg1, int arg2, long arg3) {  
	 this.setTitle("You Click Item:" + String.valueOf(arg2));   




       mStatusBarManager=this.getSystemService("statusbar");
		Method[] methods=statusBarManager.getClass().getDeclaredMethods();
     for (Method method : methods)
     {
		if (method.getName().compareTo("expand")==0)  //這裡
         {expandStatusBar = method;} //這裡
	}
     try {
    	  expandStatusBar.invoke(statusBarManager);
    	} catch (IllegalArgumentException e) {
    	  e.printStackTrace();
    	} catch (IllegalAccessException e) {
    	  e.printStackTrace();
    	} catch (InvocationTargetException e) {
    	  e.printStackTrace();
    	}	 

}  
	 private ListView mListView = null;   
    @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = new ListView(this);  
mListView.setOnItemClickListener((OnItemClickListener) this);  
(this,android.R.layout.simple_list_item_checked,  
new String[]{"ItemA", "ItemB", "ItemC"});  
mListView.setAdapter(adapter);  
this.setContentView(mListView);  
}  
};

程式碼中的if完全沒有跑進去,導致了下面的 expandStatusBar.invoke(statusBarManager);出了問題..(似乎是因為expandStatusBar=null..)

有沒有大大知道為什麼?謝謝

1 個回答

2
aliceyeh
iT邦新手 2 級 ‧ 2011-04-29 17:19:51

this.getSystemService("statusbar")

你是否沒有宣告使用權限, 所以取不到 statusbar :
<uses-permission android:name="android.permission.STATUS_BAR">
</uses-permission>

並不一定要用 reflection 去找 expand 這個方法, 這樣似乎多繞圈了:
final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
if (statusBar != null) {
statusBar.expand();
}
透過轉型, 就可以直接呼叫 expand() 這個方法,
不過要注意 statusBar 在轉型時會發生空值錯誤!

匿名 檢舉

等等..可是AndroidSDK裡面沒有StatusBarManager這個API耶..
<uses-permission android:name="android.permission.STATUS_BAR">
</uses-permission> 試了也一樣,if那段沒有跑進去,expandStatusBar=null..

匿名 檢舉

啊我搞錯了,我宣告了使用權限之後出現...

&lt;pre class="c" name="code"> ActivityManager: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.tteesstt/.tteesstt } from null (pid=-1, uid=-1) requires android.permission.STATUS_BAR

..Orz

aliceyeh iT邦新手 2 級 ‧ 2011-05-10 09:35:59 檢舉

歹勢, 玩票性質, 這裏有 "系統" 應用程式可宣告權限的說明:
Security Permissions in Android
http://www.krvarma.com/posts/android/security-permissions-in-android/

我要發表回答

立即登入回答