1

从“TabActivity”,我启动了一个“Fragment”,它又启动了“FragmentActivity”。但是我遇到了崩溃。如果我启动具有简单文本的 Activity,它将起作用,但如果我启动具有 AsyncTask 的 Activity,它将崩溃。以下是错误和代码,来自错误日志的任何建议:

 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 android.app.LocalActivityManager$LocalActivityRecord@4250eaa8 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 android.app.LocalActivityManager$LocalActivityRecord@4250eaa8 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 android.app.LocalActivityManager$LocalActivityRecord@4250eaa8 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 SocialFeeds.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 反过来对“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;

}

 }

如果我在 OnCreate 中使用一些 setText 调用简单的 Activity HomeFragment.java 而不是 FacebookFeeds.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;
}
}
4

1 回答 1

0

请从活动中删除“对话框”,它将起作用。崩溃的原因是没有活动时显示的对话框。

于 2013-07-28T14:52:54.263 回答