我正在尝试在myString
方法中设置onPostExecute(..)
的值。好吧,我确信那里有myString
。
接下来,我想设置一个ListView
,其中包含myString
方法中设置的onPostExecute(..)
。但是当我运行应用程序时它会崩溃,而且只是因为我在myString
中添加了ArrayList
而崩溃了。
在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)
答案 0 :(得分:1)
您的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);
}
试试这个。它应该工作。