Skip to content

Commit df4d226

Browse files
committed
子线程获取用户自定义类有异常
1 parent 6743a8d commit df4d226

6 files changed

Lines changed: 68 additions & 6 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.DS_Store
22
.gradle
33
.idea
4-
build
4+
build
5+
.cxx
6+
gradle

JniThread/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
/build
88
/captures
99
.externalNativeBuild
10+
./AppProgrammingSource/*

JniThread/app/src/main/cpp/WlListener.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,19 @@ void WlListener::onError(int type, int code, const char *msg) {
4848
LOGD("Could find class, ArrayList");
4949
}
5050

51-
52-
jclass person_cls = env->FindClass("com/ywl5320/jnithread/Person");
53-
if (person_cls == nullptr) {
54-
LOGE("Could not find class, Person");
55-
} else {
51+
jclass personClass = env->FindClass("com/ywl5320/jnithread/Person");
52+
if (personClass == NULL) {
53+
// 检查是否有异常发生
54+
if (env->ExceptionCheck()) {
55+
// 输出错误信息
56+
env->ExceptionDescribe();
57+
}
58+
}else {
5659
LOGD("Could find class, Person");
5760
}
5861

62+
63+
5964
jstring jmsg = env->NewStringUTF(msg);
6065
env->CallVoidMethod(jobj, jmid, code, jmsg);
6166
env->DeleteLocalRef(jmsg);

JniThread/app/src/main/java/com/ywl5320/jnithread/JniThread.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class JniThread {
2424
private OnErrorListener onErrorListener;
2525

2626
public void setOnErrorListener(OnErrorListener onErrorListener) {
27+
2728
this.onErrorListener = onErrorListener;
2829
}
2930

JniThread/app/src/main/java/com/ywl5320/jnithread/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ public class MainActivity extends AppCompatActivity {
1717
protected void onCreate(Bundle savedInstanceState) {
1818
super.onCreate(savedInstanceState);
1919
setContentView(R.layout.activity_main);
20+
21+
Person person = new Person("ywl", 18, "beijing");
22+
person.displayInfo();
23+
2024
jniThread = new JniThread();
2125
jniThread.setOnErrorListener(new JniThread.OnErrorListener() {
2226
@Override
2327
public void onError(int code, String msg) {
28+
2429
Log.d("ywl5320", "code: " + code + ", msg: " + msg);
2530
Message message = Message.obtain();
2631
message.what = code;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.ywl5320.jnithread;
2+
3+
import android.util.Log;
4+
5+
public class Person {
6+
private String name;
7+
private int age;
8+
private String address;
9+
10+
// Constructor
11+
public Person(String name, int age, String address) {
12+
this.name = name;
13+
this.age = age;
14+
this.address = address;
15+
}
16+
17+
// Getters and Setters
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public int getAge() {
27+
return age;
28+
}
29+
30+
public void setAge(int age) {
31+
this.age = age;
32+
}
33+
34+
public String getAddress() {
35+
return address;
36+
}
37+
38+
public void setAddress(String address) {
39+
this.address = address;
40+
}
41+
42+
// Other methods
43+
public void displayInfo() {
44+
Log.d("Person","Name: " + name);
45+
Log.d("Person","Age: " + age);
46+
Log.d("Person","Address: " + address);
47+
}
48+
}

0 commit comments

Comments
 (0)