gpt4 book ai didi

java - 当我尝试更新 onPostExecute(...) 方法中的字段时,Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 02:50:06 26 4
gpt4 key购买 nike

我试图在 onPostExecute(..) 方法中设置 myString 的值。好吧,我确信 myString 设置在那里。接下来,我想设置一个 ListView,其中还包含 myString,它是在 onPostExecute(..) 方法中设置的。但是当我运行应用程序时它崩溃了,它崩溃只是因为我在 ArrayList 中添加了 myString

onPostExecute(..) 方法中设置值有什么问题?我该如何解决?提前致谢。

public class SearchTwitter extends ListActivity {
private ArrayList<String> events;
private ArrayAdapter<String> adapter;
private String myString;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchTweets();

events = new ArrayList<String>();
events.add("Item");
events.add(myString); // Application crashes because of that line

adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, events);

setListAdapter(adapter);
}
private void searchTweets()
{
new SearchTweets().execute("Hello World");
}

class SearchTweets extends AsyncTask<String, String, List<Status>> {
@Override
protected void onPostExecute(List<Status> result)
{
myString = result.get(0).getUser().getScreenName();
}
@Override
protected List<Status> doInBackground(String... params)
{
//Do the work here and return a List<Status>
}
}

编辑 1

06-22 10:39:51.073: E/AndroidRuntime(2696): FATAL EXCEPTION: main
06-22 10:39:51.073: E/AndroidRuntime(2696): Process: com.bledi.android.twittertest, PID: 2696
06-22 10:39:51.073: E/AndroidRuntime(2696): java.lang.NullPointerException
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.AbsListView.obtainView(AbsListView.java:2240)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.ListView.onMeasure(ListView.java:1175)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.View.measure(View.java:16497)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.View.measure(View.java:16497)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.View.measure(View.java:16497)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-22 10:39:51.073: E/AndroidRuntime(2696): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.View.measure(View.java:16497)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-22 10:39:51.073: E/AndroidRuntime(2696): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.View.measure(View.java:16497)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.Choreographer.doFrame(Choreographer.java:544)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.os.Handler.handleCallback(Handler.java:733)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.os.Looper.loop(Looper.java:136)
06-22 10:39:51.073: E/AndroidRuntime(2696): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-22 10:39:51.073: E/AndroidRuntime(2696): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 10:39:51.073: E/AndroidRuntime(2696): at java.lang.reflect.Method.invoke(Method.java:515)
06-22 10:39:51.073: E/AndroidRuntime(2696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-22 10:39:51.073: E/AndroidRuntime(2696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-22 10:39:51.073: E/AndroidRuntime(2696): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您的 onCreate() 应该如下所示:

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchTweets();

}

你的 onPostExecute() 应该是这样的:

@Override
protected void onPostExecute(List<Status> result) {

myString = result.get(0).getUser().getScreenName();

events = new ArrayList<String>();
events.add("Item");
events.add(myString);

adapter = new ArrayAdapter<String> (SearchTwitter.this, android.R.layout.simple_list_item_1, events);

setListAdapter(adapter);
}

试试这个。它应该有效。

关于java - 当我尝试更新 onPostExecute(...) 方法中的字段时,Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24352293/

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