0

我正在尝试使用添加子活动

TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                parentActivity.startChildActivity("AlbumDetailsLatest2", i);

但似乎我遇到了一个错误,我无法理解似乎是什么问题。

这是我的最新专辑活动课程:

public class LatestAlbums extends FragmentActivity {

    // widgets
    private ListView list;
    private ImageView back, clear;
    private EditText find;
    // JSON Node names
    private static final String TAG_ALBUMS = "albums";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_SINGER = "singer";
    private static final String TAG_GENRE = "genre";
    private static final String TAG_MIX = "mix";
    private static final String TAG_THUMB = "thumb";
    private static final String TAG_SONGS = "songs";
    private static final String TAG_SONG_TITLE = "song";
    private static final String TAG_SONG_ARTIST = "artist";
    private static final String TAG_SONG_MP3 = "mp3";
    private static final String TAG_SONG_MP4 = "mp4";
    private static final String TAG_SONG_THUMB = "thumb";
    private Parcelable state;
    private ProgressDialog pdialog;

    // albums JSONArray
    private JSONArray albums;
    private JSONArray sngs;
    // arrayLists for creating objects
    private ArrayList<Albums> albumsArray;
    private ArrayList<Songs> songsArray;
    private AlbumLazyAdapter adapter;
    private ViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.latest_albums);
        albumsArray = new ArrayList<Albums>();
        songsArray = new ArrayList<Songs>();

        try {
            loadJSON();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initWidgets() {
        list = (ListView) findViewById(R.id.listView1);
        back = (ImageView) findViewById(R.id.latest_back_img);
        clear = (ImageView) findViewById(R.id.latest_search_clear);
        pager = (ViewPager) findViewById(R.id.my_slider);
        clear.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                find.setText("");
                find.clearFocus();
                find.clearComposingText();
                clear.setVisibility(View.INVISIBLE);
            }
        });
        find = (EditText) findViewById(R.id.latest_search_et);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        adapter = new AlbumLazyAdapter(this, albumsArray);

        // set list view adapter
        list.setAdapter(adapter);

        // setting find edit text searching method
        find.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                clear.setVisibility(View.VISIBLE);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                final ArrayList<Albums> temp = new ArrayList<Albums>();
                for (int i = 0; i < albumsArray.size(); i++) {
                    if (albumsArray.get(i).getTAG_NAME().toLowerCase()
                            .contains(find.getText().toString().toLowerCase())
                            || albumsArray.get(i).getTAG_SINGER().toLowerCase()
                                    .contains(find.getText().toString())) {
                        temp.add(albumsArray.get(i));
                    }
                }

                AlbumLazyAdapter mAdapter = new AlbumLazyAdapter(
                        LatestAlbums.this, temp);
                list.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                list.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long arg3) {
                        Intent i = new Intent();
                        i.putExtra("albums", temp.get(position));
                        i.putExtra("songs", temp.get(position).getSongs());
                        i.putExtra("title", temp.get(position).getTAG_NAME());
                        i.putExtra("genre", temp.get(position).getTAG_GENRE());
                        i.putExtra("thumb", temp.get(position).getTAG_THUMB());
                        i.setClass(LatestAlbums.this, AlbumDetails.class);
                        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                        parentActivity.startChildActivity("AlbumDetailsLatest1", i);

                    }
                });

            }
        });
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long arg3) {
                Intent i = new Intent();
                i.putExtra("albums", albumsArray.get(position));
                i.putExtra("songs", albumsArray.get(position).getSongs());
                i.putExtra("title", albumsArray.get(position).getTAG_NAME());
                i.putExtra("genre", albumsArray.get(position).getTAG_GENRE());
                i.putExtra("thumb", albumsArray.get(position).getTAG_THUMB());
                i.setClass(LatestAlbums.this, AlbumDetails.class);
                TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                parentActivity.startChildActivity("AlbumDetailsLatest2", i);

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        initWidgets();
    }

    // load json from server via asyncTask
    public void loadJSON() throws JSONException {
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                initWidgets();
                pdialog = new ProgressDialog(LatestAlbums.this);
                pdialog.setMessage(getString(R.string.loading_));
                pdialog.show();
                pdialog.setCancelable(true);
                pdialog.setCanceledOnTouchOutside(false);

            }

            @Override
            protected Void doInBackground(Void... params) {

                if (isOnline()) {

                    JSONObject json = new JSONParser()
                            .getJSONFromUrl(getString(R.string.json_URL));

                    JSONObject theJSON;
                    try {
                        // Getting Array of albums
                        theJSON = json.getJSONObject("json");
                        albums = theJSON.getJSONArray(TAG_ALBUMS);
                        // looping through All albums
                        for (int i = 0; i < albums.length(); i++) {
                            JSONObject c = albums.getJSONObject(i);
                            String album_id = c.getString(TAG_ID);
                            String album_name = c.getString(TAG_NAME);
                            String album_singer = c.getString(TAG_SINGER);
                            String album_mix = c.getString(TAG_MIX);
                            String album_genre = c.getString(TAG_GENRE);
                            String album_thumb = getString(R.string.cover_URL)
                                    + c.getString(TAG_THUMB);
                            albumsArray.add(new Albums(album_id, album_name,
                                    album_singer, album_genre, album_mix,
                                    album_thumb, songsArray));
                            sngs = c.getJSONArray(TAG_SONGS);
                            // getting songs for the album
                            songsArray = new ArrayList<Songs>();
                            for (int j = 0; j < sngs.length(); j++) {
                                JSONObject songs = sngs.getJSONObject(j);
                                String song_artist = songs
                                        .getString(TAG_SONG_ARTIST);
                                String song_mp3 = songs.getString(TAG_SONG_MP3);
                                String song_mp4 = songs.getString(TAG_SONG_MP4);
                                String song_thumb = songs
                                        .getString(TAG_SONG_THUMB);
                                String song_title = songs
                                        .getString(TAG_SONG_TITLE);
                                songsArray.add(new Songs(song_title,
                                        song_artist,
                                        "http://someurl.com/musica/songs_with/"
                                                + song_mp3, song_mp4,
                                        getString(R.string.cover_URL)
                                                + song_thumb));

                            }
                            albumsArray.get(i).setSongs(songsArray);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.latest_relativeLayout);
                    TextView noCon = new TextView(LatestAlbums.this);
                    noCon.setText(R.string.not_connected_to_internet);
                    noCon.setPadding(100, 200, 100, 0);
                    noCon.setTextSize(22);
                    noCon.setTextColor(Color.WHITE);
                    relativeLayout.addView(noCon);
                    ImageView left = (ImageView) findViewById(R.id.latest_left);
                    left.setVisibility(View.GONE);
                    ImageView right = (ImageView) findViewById(R.id.latest_right);
                    right.setVisibility(View.GONE);

                    pager.setVisibility(View.GONE);
                    find.setVisibility(View.GONE);
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                pdialog.dismiss();
                FragmentManager fm = getSupportFragmentManager();
                MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(
                        fm, albumsArray);
                pager.setAdapter(pagerAdapter);

                adapter.notifyDataSetChanged();
            }
        };
        task.execute();
    }

    public void saveState() {
        state = list.onSaveInstanceState();
    }

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

    public void reload() {
        saveState();
        list.setAdapter(adapter);
        list.onRestoreInstanceState(state);
    }
}

这是 logCat 输出:

04-11 11:48:39.057: E/AndroidRuntime(3573): FATAL EXCEPTION: main
04-11 11:48:39.057: E/AndroidRuntime(3573): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.activities.TabLayouts}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.activities.LatestGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.activities.LatestAlbums}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4198bfe8 is not valid; is your activity running?
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1972)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.access$600(ActivityThread.java:124)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1148)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.os.Looper.loop(Looper.java:137)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.main(ActivityThread.java:4440)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at java.lang.reflect.Method.invoke(Method.java:511)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at dalvik.system.NativeStart.main(Native Method)
04-11 11:48:39.057: E/AndroidRuntime(3573): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.activities.LatestGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.activities.LatestAlbums}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4198bfe8 is not valid; is your activity running?
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1972)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1813)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.widget.TabHost.setCurrentTab(TabHost.java:346)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.widget.TabHost.addTab(TabHost.java:236)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at com.myapp.activities.TabLayouts.addTab(TabLayouts.java:52)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at com.myapp.activities.TabLayouts.setTabs(TabLayouts.java:32)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at com.myapp.activities.TabLayouts.onCreate(TabLayouts.java:25)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.Activity.performCreate(Activity.java:4465)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1936)
04-11 11:48:39.057: E/AndroidRuntime(3573):     ... 11 more
04-11 11:48:39.057: E/AndroidRuntime(3573): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/myapp.activities.LatestAlbums}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4198bfe8 is not valid; is your activity running?
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1972)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1813)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at com.myapp.activities.TabGroupActivity.startChildActivity(TabGroupActivity.java:56)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at com.myapp.activities.LatestGroup.onCreate(LatestGroup.java:10)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.Activity.performCreate(Activity.java:4465)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-11 11:48:39.057: E/AndroidRuntime(3573):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1936)
04-11 11:48:39.057: E/AndroidRuntime(3573):     ... 23 more
04-11 11:48:39.057: E/AndroidRuntime(3573): Caused by: android.view.WindowManager$BadTokenExcep

很抱歉有很多代码,但真的不知道是什么导致了这个问题

4

2 回答 2

1

采用:

意图 i = new Intent(getParent(),AlbumDetails.class);

反而:

意图 i = new Intent();

并在下面的行评论:

i.setClass(LatestAlbums.this, AlbumDetails.class);

采用:

TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("AlbumDetailsLatest2", i);
于 2013-04-11T10:10:38.373 回答
0

据我所知,问题在于您尝试在 onCreate 方法中显示的对话框。您必须等到 Activity 的生命周期方法完成。像这样使用 post 方法pdialog.post(new Runnable() { pdialog.show(); });仅在活动启动并运行时显示对话框。

编辑:我无法删除该帖子,因为您已经接受了它,但是我的方法仅适用于视图。所以你必须创建一个处理程序并 postDelay 对话框。我希望这会奏效。

 new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
     pdialog = new ProgressDialog(LatestAlbums.this);
            pdialog.setMessage(getString(R.string.loading_));
            pdialog.setCancelable(true);
            pdialog.setCanceledOnTouchOutside(false);
            pdialog.show();
    }
}, 1000);
于 2013-04-11T10:12:57.880 回答