從「TabActivity」中,我啓動了一個「Fragment」,然後啓動「FragmentActivity」。但我遇到了崩潰。如果啓動具有簡單文本的活動,它將工作,但如果我啓動具有AsyncTask的活動,它將崩潰。下面是錯誤和代碼,從錯誤日誌任何建議:從TabActivity啓動片段時應用程序崩潰[Android]
07-28 12:43:26.540: E/AndroidRuntime(32237): FATAL EXCEPTION: main
07-28 12:43:26.540: E/AndroidRuntime(32237):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.itcuties.multicategoryrssreader/com.itcuties.multicategoryrssreader.SocialFeeds}:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.itcuties.multicategoryrssreader/com.itcuties.multicategoryrssreader.FacebookFeeds}:
android.view.WindowManager$BadTokenException: Unable to add window --
token [email protected] is
not valid; is your activity running? 07-28 12:43:26.540:
E/AndroidRuntime(32237): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-28 12:43:26.540: E/AndroidRuntime(32237): at
android.app.ActivityThread.startActivityNow(ActivityThread.java:1941)
07-28 12:43:26.540: E/AndroidRuntime(32237): at
android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
07-28 12:43:26.540: E/AndroidRuntime(32237): at
android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
07-28 12:43:26.540: E/AndroidRuntime(32237): at
android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:705)
07-28 12:43:26.540: E/AndroidRuntime(32237): at
android.widget.TabHost.setCurrentTab(TabHost.java:369) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:560)
07-28 12:43:26.540: E/AndroidRuntime(32237): at
android.view.View.performClick(View.java:4223) 07-28 12:43:26.540:
E/AndroidRuntime(32237): at
android.view.View$PerformClick.run(View.java:17275) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
android.os.Handler.handleCallback(Handler.java:615) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
android.os.Handler.dispatchMessage(Handler.java:92) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
android.os.Looper.loop(Looper.java:137) 07-28 12:43:26.540:
E/AndroidRuntime(32237): at
android.app.ActivityThread.main(ActivityThread.java:4898) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
java.lang.reflect.Method.invokeNative(Native Method) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
java.lang.reflect.Method.invoke(Method.java:511) 07-28 12:43:26.540:
E/AndroidRuntime(32237): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-28 12:43:26.540: E/AndroidRuntime(32237): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 07-28
12:43:26.540: E/AndroidRuntime(32237): at
dalvik.system.NativeStart.main(Native Method) 07-28 12:43:26.540:
E/AndroidRuntime(32237): Caused by: java.lang.RuntimeException: Unable
to start activity
> ComponentInfo{com.itcuties.multicategoryrssreader/com.itcuties.multicategoryrssreader.FacebookFeeds}:
> android.view.WindowManager$BadTokenException: Unable to add window --
> token [email protected] is
> not valid; is your activity running? 07-28 12:43:26.540:
> E/AndroidRuntime(32237): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.app.ActivityThread.startActivityNow(ActivityThread.java:1941)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> com.inazaruk.example.ActivityHostFragment.onCreateView(ActivityHostFragment.java:46)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.app.Activity.performStart(Activity.java:5216) 07-28
> 12:43:26.540: E/AndroidRuntime(32237): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): ... 18 more 07-28
> 12:43:26.540: E/AndroidRuntime(32237): Caused by:
> android.view.WindowManager$BadTokenException: Unable to add window --
> token [email protected] is
> not valid; is your activity running? 07-28 12:43:26.540:
> E/AndroidRuntime(32237): at
> android.view.ViewRootImpl.setView(ViewRootImpl.java:708) 07-28
> 12:43:26.540: E/AndroidRuntime(32237): at
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.view.Window$LocalWindowManager.addView(Window.java:554) 07-28
> 12:43:26.540: E/AndroidRuntime(32237): at
> android.app.Dialog.show(Dialog.java:277) 07-28 12:43:26.540:
> E/AndroidRuntime(32237): at
> com.itcuties.multicategoryrssreader.FacebookFeeds$RssFeedTask.onPreExecute(FacebookFeeds.java:132)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at
> android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 07-28
> 12:43:26.540: E/AndroidRuntime(32237): at
> android.os.AsyncTask.execute(AsyncTask.java:534) 07-28 12:43:26.540:
> E/AndroidRuntime(32237): at
> com.itcuties.multicategoryrssreader.FacebookFeeds.onCreate(FacebookFeeds.java:42)
> 07-28 12:43:26.540: E/AndroidRuntime(32237): at android.app.Activity.
>
讓我知道如果你需要尋找到代碼:
從RSSTabsActivity.java,我發動FragmentActivity「SocialFeeds.java」:
public class RssTabsActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// First, set the content view
setContentView(R.layout.activity_rss_top_tabs);
// Then get the TabHost
TabHost tabHost = getTabHost();
//For TabA
Intent artIntent = new Intent().setClass(this, TabA.class);
// The name of the art tab taken from the String resources
String artTabName = getResources().getString(R.string.tab_course);
TabSpec artTabSpec = tabHost.newTabSpec(artTabName).setIndicator(artTabName, getResources().getDrawable(R.drawable.rss_tab_art)).setContent(artIntent);
// Add art tab to the TabHost
tabHost.addTab(artTabSpec);
//For TabB
Intent techIntent = new Intent().setClass(this, TabB.class);
// Tech tab name taken from the string resources
String techTabName = getResources().getString(R.string.tab_tour);
TabSpec techTabSpec = tabHost.newTabSpec(techTabName).setIndicator(techTabName, getResources().getDrawable(R.drawable.rss_tab_tech)).setContent(techIntent);
tabHost.addTab(techTabSpec);
//For TabC
Intent sportsIntent = new Intent().setClass(this, TabC.class);
// Sports tab name - string resources
String sportsTabName = getResources().getString(R.string.tab_webcast);
TabSpec sportsTabSpec = tabHost.newTabSpec(sportsTabName).setIndicator(sportsTabName, getResources().getDrawable(R.drawable.rss_tab_sports)).setContent(sportsIntent);
tabHost.addTab(sportsTabSpec);
//For TabD
Intent tweetsIntent = new Intent().setClass(this, SocialFeeds.class);
String tweetTabName = getResources().getString(R.string.tab_tweets);
TabSpec tweetTabSpec = tabHost.newTabSpec(tweetTabName).setIndicator(tweetTabName, getResources().getDrawable(R.drawable.rss_tab_tweets)).setContent(tweetsIntent);
tabHost.addTab(tweetTabSpec);
// Set current tab to Technology
tabHost.setCurrentTab(0);
}
SocialFeeds.java:
public class SocialFeeds extends FragmentActivity {
private ActionsContentView viewActionsContentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentactivity);
viewActionsContentView = (ActionsContentView) findViewById(R.id.actionsContentView);
final ListView viewActionsList = (ListView) findViewById(R.id.actions);
final String[] values = new String[] { "Twitter", "Facebook", "Youtube" };
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
viewActionsList.setAdapter(adapter);
viewActionsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long flags) {
showFragment(position);
}
});
showFragment(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragmentmenu_main, menu);
return true;
}
private void showFragment(int position) {
final Fragment f;
switch (position) {
case 0:
f = new FacebookFragment();
break;
case 1:
f = new HomeFragment();
break;
case 2:
f = new HomeFragment();
break;
default:
return;
}
getSupportFragmentManager().beginTransaction().replace(R.id.content, f).commit();
viewActionsContentView.showContent();
}
從FragmentActivity Socia lFeeds.java,我發動ActivityHostFragment 「FacebookFragment.java」
public class FacebookFragment extends ActivityHostFragment {
@Override
protected Class<? extends Activity> getActivityClass() {
return FacebookFeeds.class;
}
}
從 「FacebookFragment.java」,我發動FacebookFeeds.java它具有的AsyncTask方法
public class FacebookFeeds extends Activity {
/** Called when the activity is first created. */
ListView _rssFeedListView;
List<JSONObject> jobs;
List<RssFeedStructure> rssStr;
private FacebookAdapter _adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
_rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview);
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
}
private class RssFeedTask extends AsyncTask<String, Void, String> {
// private String Content;
private ProgressDialog Dialog;
String response = "";
@Override
protected void onPreExecute() {
Dialog = new ProgressDialog(FacebookFeeds.this);
Dialog.setMessage("Rss Loading...");
Dialog.show();
}
@Override
protected String doInBackground(String... urls) {
try {
String feed = "https:someurl";
XmlHandler rh = new XmlHandler();
rssStr = rh.getLatestArticles(feed);
} catch (Exception e) {
}
return response;
}
@Override
protected void onPostExecute(String result) {
if (rssStr != null) {
_adapter = new FacebookAdapter(FacebookFeeds.this, rssStr);
_rssFeedListView.setAdapter(_adapter);
}
Dialog.dismiss();
}
}
此的AsyncTask inturn有內喊出了「FacebookAdapter.java」
public class FacebookAdapter extends ArrayAdapter<RssFeedStructure> {
List<RssFeedStructure> imageAndTexts1 = null;
public FacebookAdapter(Activity activity,
List<RssFeedStructure> imageAndTexts) {
super(activity, 0, imageAndTexts);
imageAndTexts1 = imageAndTexts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.facebookadapter, null);
TextView textView = (TextView) rowView.findViewById(R.id.feed_text);
TextView timeFeedText = (TextView) rowView
.findViewById(R.id.feed_updatetime);
ImageView imageView = (ImageView) rowView.findViewById(R.id.feed_image);
try {
Log.d("rssfeed", "imageAndTexts1.get(position).getImgLink() :: "
+ imageAndTexts1.get(position).getImgLink() + " :: "
+ imageAndTexts1.get(position).getTitle());
textView.setText(imageAndTexts1.get(position).getDescription());
SpannableString content = new SpannableString(imageAndTexts1.get(
position).getPubDate());
content.setSpan(new UnderlineSpan(), 0, 13, 0);
timeFeedText.setText(content);
if (imageAndTexts1.get(position).getImgLink() != null) {
URL feedImage = new URL(imageAndTexts1.get(position)
.getImgLink().toString());
if (!feedImage.toString().equalsIgnoreCase("null")) {
HttpURLConnection conn = (HttpURLConnection) feedImage
.openConnection();
InputStream is = conn.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(img);
} else {
imageView.setBackgroundResource(R.drawable.rss_tab_tweets);
}
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return rowView;
}
}
如果不是FacebookFeeds.java如果我叫在OnCreate中一些簡單的setText活動HomeFragment.java,它會工作。
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.home, container, false);
return v;
}
}
是的,我們需要的代碼。請張貼相同。 – Raghunandan
謝謝Raghunandan,我已經更新了代碼 – user45678
我正在等待您的回覆Raghunandan ...謝謝 – user45678