我正在尝试使用 Platfrom 通道集成第三方原生 SDK(用 java 编写)来颤振。现在第三方 sdk 可以完美地在原生编码中工作(没有颤动)。
但是,当我尝试使用平台通道启动意图时,应用程序只会在按钮单击时停止并显示此错误日志。
Process: com.example.platform_view_java, PID: 27183
E/AndroidRuntime(27183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.platform_view_java/com.xxxx.xxxx.xxxx}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(27183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3616)
E/AndroidRuntime(27183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3780)
E/AndroidRuntime(27183): at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
E/AndroidRuntime(27183): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(27183): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(27183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2251)
E/AndroidRuntime(27183): at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(27183): at android.os.Looper.loop(Looper.java:233)
E/AndroidRuntime(27183): at android.app.ActivityThread.main(ActivityThread.java:8063)
E/AndroidRuntime(27183): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(27183): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
E/AndroidRuntime(27183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
E/AndroidRuntime(27183): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(27183): at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:846)
E/AndroidRuntime(27183): at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
E/AndroidRuntime(27183): at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
E/AndroidRuntime(27183): at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
E/AndroidRuntime(27183): at com.karza.aadhaarsdk.AadharActivity.onCreate(AadharActivity.java:115)
E/AndroidRuntime(27183): at android.app.Activity.performCreate(Activity.java:8006)
E/AndroidRuntime(27183): at android.app.Activity.performCreate(Activity.java:7990)
E/AndroidRuntime(27183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
E/AndroidRuntime(27183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3589)
E/AndroidRuntime(27183): ... 11 more
I/Process (27183): Sending signal. PID: 27183 SIG: 9
Lost connection to device.
这是我的本机 UI 代码:
class NativeView implements PlatformView{
private Button button;
NativeView(Context context, int id, Map<String, Object> creationParams) {
button = new Button(context);
button.setText("Update");
button.setTextSize(20);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start_service(v);
}
});
}
public Boolean start_service(View view){
// Log.d("NATIVE","Method called");
// return true;
Intent intent = new Intent(view.getContext(),com.xxx.xxx.xxx.class);
intent.putExtra("MY-TOKEN","mytoken");
intent.putExtra("CLIENT","xxx-xxxx");
intent.putExtra("ENV","test");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
return true;
}
@Override
public View getView() {
return button;
}
@Override
public void dispose() {}
}
这是我的 main.dart 代码
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp( home: Native() );
}
class Native extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded( child: AndroidView(viewType: "view1",) ),
],
),
);
}
}
这是 mainactivity.java 文件
package com.example.platform_view_java;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
public class MainActivity extends FlutterActivity {
@Override
public void configureFlutterEngine(@NonNull @org.jetbrains.annotations.NotNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
flutterEngine
.getPlatformViewsController()
.getRegistry()
.registerViewFactory("view1", new NativeViewFactory());
}
}
最后是我的 nativeviewfactory.java
package com.example.platform_view_java;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Map;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
class NativeViewFactory extends PlatformViewFactory {
NativeViewFactory() {
super(StandardMessageCodec.INSTANCE);
}
@Override
public PlatformView create(@NonNull Context context, int id, @Nullable Object args) {
final Map<String, Object> creationParams = (Map<String, Object>) args;
return new NativeView(context, id, creationParams);
}
}
我究竟做错了什么?提前致谢。