开发者

startactivity() not working from inner class that extends ListActivity

开发者 https://www.devze.com 2023-03-31 00:41 出处:网络
Title basically says it all.The below code doesn\'t throw any errors, it just doesn\'t start the new activity.Code below.

Title basically says it all. The below code doesn't throw any errors, it just doesn't start the new activity. Code below.

I have tried modifying startActivity(new Intent(mCtx, NewActivity.class)); to read startActivity(new Intent(MyListActivity.this, NewActivity.class));

I hav开发者_Python百科e been testing this in Eclipse with an AVD.

Any thoughts on this would be appreciated - thanks!

public class MyListActivity extends ListActivity {

private Context mCtx;
MyContentObserver mContentObserver;

@Override
public void onCreate(Bundle onSavedInstance) {
    super.onCreate(onSavedInstance);
    setContentView(R.layout.calls_list);

    mContentObserver = new MyContentObserver(this);

    this.getApplicationContext().getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, mContentObserver);

}

private class MyContentObserver extends ContentObserver {
    private Context mCtx;
    public MyContentObserver(Context ctx ) {
        super(null);
        mCtx = ctx;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        startActivity(new Intent(mCtx, NewActivity.class));
    }
}   
}


Possible cause 1

The activity has not been declared.

You have to add the activity to your AndroidManifest.xml

You should add this to your <application> tag in your AndroidManifest.xml:

<activity android:name="package.name.NewActivity">
    <!-- Add an intent filter here if you wish -->
</activity>

Possible cause 2

The onChange method doesn't actually run.

Please use the code below to verify that the onChange method actually gets called:

public class MyListActivity extends ListActivity {
    MyContentObserver mContentObserver;

    @Override
    public void onCreate(Bundle onSavedInstance) {
        super.onCreate(onSavedInstance);
        setContentView(R.layout.calls_list);

        mContentObserver = new MyContentObserver();

        this.getApplicationContext().getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, mContentObserver);
    }

    private class MyContentObserver extends ContentObserver {
        public MyContentObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.d("MyListActivity.MyContentObserver", "onChange");
            startActivity(new Intent(MyListActivity.this, NewActivity.class));
        }
    }   
}

You could also try launching the activity from onCreate to make sure it can be launched.

0

精彩评论

暂无评论...
验证码 换一张
取 消