开发者

simply use "setContentView" or "startActivity"?

I'm learning android and in a tutorial, to open a new screen they use

...

startActivity( new Intent("com.rob.minispy.sweet"));

( in main.java )

...

public class sweet extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    }

}

( in sweet.java )

...

and finally

<activity android:name=".sweet"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.SWEET" />
        <category android:name="android.intent.category.LAUNCH开发者_JAVA技巧ER" />
    </intent-filter>
</activity>

( in the manifest )

now not only that it doesn't really work and make the app crash, wouldn't it be easier to do it simply by

setContentView(R.layout.splash);

from main.java or are there some downsides to that?

Thanks!


You have 2 ways to start the sweet activity

  1. Change startActivity( new Intent("com.rob.minispy.sweet")); to startActivity( new Intent("android.intent.action.SWEET")); See more info here
  2. startActivity(new Intent(this,sweet.class)); . This uses the form Intent(Context packageContext, Class<?> cls). More info here


Yes, it's best to start a new activity because this way Android will automatically handle transitions from one activity to another. Your application crashes because you do it improperly. Intent should be created as new Intent(this, com.rob.minispy.sweet.class). You use a string in the constructor which is supposed to be an action and not a class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜