gpt4 book ai didi

java - onPostExecute 中来自 AsyncTask 的 setText

转载 作者:行者123 更新时间:2023-11-30 00:24:59 24 4
gpt4 key购买 nike

我有以下 AsyncTask 从 xml 文件中获取一些数据:

public class GetTripDetails extends AsyncTask<String, Integer, String[]> {

private Context context;
private String[] details = new String[4];

@Override
protected void onPreExecute() {
super.onPreExecute();
}

public GetTripDetails (Context parsedContext){
context = parsedContext;
}

@Override
protected String[] doInBackground(String... params) {
SharedPreferences sharedpreferences;
String activeTrip;

try {
sharedpreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
activeTrip = sharedpreferences.getString("activeTrip", null); //Liest den Namen des aktiven Trips

File file = new File(Environment.getExternalStorageDirectory() + "/" + "android/data/com.travip/" + activeTrip + ".trp");
FileInputStream fis = new FileInputStream(file);

XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setInput(fis, null);

int event = myparser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String tag_name = myparser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.END_TAG:
if(tag_name.equals("trip")){
details[0] = myparser.getAttributeValue(null, "name");
details[1] = myparser.getAttributeValue(null, "startdate");
details[2] = myparser.getAttributeValue(null, "enddate");
details[3] = myparser.getAttributeValue(null, "description");
}
break;
}
event = myparser.next();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}

return details;
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String[] result) {
Toast.makeText(context, result[0], Toast.LENGTH_LONG).show();

tv_title.setText(result[0]);
tv_subtitle.setText(result[1] + " - " + result[2]);

super.onPostExecute(result);
}

}

尽管 tv_title 和 tv_subtitle 是公共(public)的并且 AsyncTask 在同一个类中,但它会产生一个空指针异常。我试图在 onStart() 中执行它(-> 在 TextViews 应该已经初始化之后),但它也没有用。谢谢你的建议

更新:
日志:

08-08 12:09:01.446 22390-22390/com.travip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.travip, PID: 22390
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.travip/com.travip.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.access$900(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.travip.MainActivity$GetTripDetails.onPreExecute(MainActivity.java:192)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604)
at android.os.AsyncTask.execute(AsyncTask.java:551)
at com.travip.MainActivity.onCreate(MainActivity.java:88)
at android.app.Activity.performCreate(Activity.java:6279)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500) 
at android.app.ActivityThread.access$900(ActivityThread.java:163) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5585) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

完整代码:

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, Overview.OnFragmentInteractionListener, Activities.OnFragmentInteractionListener, Marked.OnFragmentInteractionListener, Map.OnFragmentInteractionListener, Nearby.OnFragmentInteractionListener {
NavigationView navigationView;
String activeMenu;

public ImageView expand_collapse;
public TextView tv_title;
public TextView tv_subtitle;
public View headerLayout;

DrawerLayout drawer;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Mapbox.getInstance(getApplicationContext(), getString(R.string.access_token));

setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_overview); //Setzt den aktiven Menüeintrag
activeMenu = "main";

headerLayout = navigationView.getHeaderView(0);
headerLayout.setOnClickListener(new View.OnClickListener() { //Ändert das Menü, wenn auf den Header gecklickt wird
@Override
public void onClick(View v) {
toggleMenu();
}
});

expand_collapse = (ImageView) headerLayout.findViewById(R.id.expand_collapse);
tv_title = (TextView) headerLayout.findViewById(R.id.tv_name);
tv_subtitle = (TextView) headerLayout.findViewById(R.id.subtitle);

new GetTripDetails(getApplicationContext()).execute();
}
//Here is the asynctask (see above)

更新 2:
logcat(初始化 TextView 后):

08-08 12:20:59.008 934-934/com.travip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.travip, PID: 934
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.travip.MainActivity$GetTripDetails.onPostExecute(MainActivity.java:255)
at com.travip.MainActivity$GetTripDetails.onPostExecute(MainActivity.java:183)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

更新 3:
nav_header_main.xml(抽屉导航的headerLayout(TextView所在的位置)):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/nav_header_main" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/nav_header_top"
android:background="?attr/selectableItemBackground">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:id="@+id/title"
android:text="Spanien"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:id="@+id/subtitle"
android:text="21.3.17 - 1.4.17"
android:textAppearance="@style/TextAppearance.AppCompat.Small" />

<ImageView
android:id="@+id/expand_collapse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="@drawable/ic_expand"
android:background="?attr/actionBarItemBackground"/>

</RelativeLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/fragment_overview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"/>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.v4.widget.DrawerLayout>

最佳答案

为避免上下文内存泄漏,我建议您将 GetTripDetails 类设为静态,并将构造函数中的上下文参数替换为将任务结果返回到 Activity 的适当回调接口(interface)。它也肯定会解决您的问题,因为在您的情况下,没有设置您的任务的引用。

static class GetTripDetails extends AsyncTask<String, Void, String[]> {

private WeakReference<Callback> callbackRef;

public GetTripDetails(final Callback callback){
callbackRef = new WeakReference<>(callback);
}

@Override
protected String[] doInBackground(String... params) {
// Do your stuff
return details;
}

@Override
protected void onPostExecute(String[] result) {
final Callback callback = callbackRef.get();
if (callback == null) {
return;
}
callback.onResult(result);
}

interface Callback {
void onResult(String[] result);
}

}

在你的 Activity 中:

private final Callback callback = new Callback() {
void onResult(String[] result) {
// set values in your textview..
}
}

private GetTripDetails task;

@Override
protected void onCreate(Bundle savedInstanceState) {
//...
task = new GetTripDetails(callback);
task.execute();
}

关于java - onPostExecute 中来自 AsyncTask 的 setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45565372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com