diff --git a/src/android/app.rs b/src/android/app.rs index e3c518b..287f6ba 100644 --- a/src/android/app.rs +++ b/src/android/app.rs @@ -265,8 +265,8 @@ pub fn test() { let act2 = act.clone(); let runnable = RunnableImpl::from_fn(move || { act2.set_content_view(&edit); - }) - .unwrap(); + Ok(()) + }).unwrap(); act.run_on_ui_thread(runnable.as_ref()); let wm: WindowManagerImpl = act.get_window_manager(); assert!(wm.to_string().starts_with("android.view.WindowManagerImpl")); diff --git a/utils/src/lib.rs b/utils/src/lib.rs index fef20ed..4b330e0 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -284,6 +284,35 @@ macro_rules! impl_array { const OBJECT_SIG: &'static str = ::OBJECT_SIG; const DIM: u8 = $dim; } + + // 为 String 类型实现必要的 trait + impl JType for String { + const CLASS: &'static str = "java/lang/String"; + const OBJECT_SIG: &'static str = "Ljava/lang/String;"; + } + + impl JObjRef for String { + fn java_ref(&self) -> Result { + let mut env = vm_attach()?; + let jstring = env.new_string(self)?; + Ok(env.new_global_ref(&jstring)?) + } + } + + impl JObjNew for String { + type Fields = (); + + fn _new(this: &GlobalRef, _fields: Self::Fields) -> Result { + if this.is_null() { + return Ok(String::new()); + } + + let mut env = vm_attach()?; + let jstring = this.as_obj(); + let java_string = env.get_string(jstring.into())?; + Ok(java_string.to_str()?.to_string()) + } + } }; (u8, $dim:expr) => {