开发者

Force close on trying to use default constructor

If I'm attempting to define a default constructor for the class which is extending an Activity I'm getting force close on application execution. Why is it so?

public class App extends Activity {


    App() {
              // FORCE CLOSE
    }
    /** Called when the activity is first created. */
    @Ove开发者_Python百科rride
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
}}


You are creating a package access constructor which is not allowed by the Android Framework,

You can create a public constructor i.e. public App(){} but this is really inappropriate way to initialize an activity class in Android to say the least.

Initialize all the requirements in onCreate() which is recommended an sensible approach.

also make sure you are defining each activity in you AndroidManifest.xml file.

with <activity> tag and defin android:name attribute at minimum.

<activity android:name=".SampleAppication"
              android:label="@string/app_name">

            <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

You can find more information on Acitvity Lifecycle here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜