Search code examples
javaandroidandroid-layoutandroid-studioandroid-activity

why my app crash when i change its layout?


i want to use set content view on android studio to show a welcome message on screen before show the real main activity.

I just copy the part of code that i think it responsible for the crash of my app because if i remove the part of welcome activity ,i will get no error.

Remember android studio dose not show error just when you run it, program will crash. is there a way to make it not crash?

(any suggestion for make it better for bigger change of layouts because i will going to add more layout later with their activitys )

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcom);
    Button btn = (Button) findViewById(R.id.button);
    View.OnClickListener welcomeListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.activity_main);
        }
    };
    btn.setOnClickListener(welcomeListener);
  }

Solution

  • We did it.if any one else have the same problem use this steps: i edited my code to some thing like this:

    `

     View.OnClickListener welcomeListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
      Intent intent = newIntent(YourCurrentActivity.this,MainActivity.class);
            startActivity(intent);
        }
    };
    btn.setOnClickListener(welcomeListener);
    }
    

    ` then i go to android manifest and add my Welcome class there. why? because in android studio log there was an error that you didn't add the class to android manifest.xml

    `

    <activity android:name=".WelcomeClass">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="com.blog.software.app.WelcomeClass"
            android:resource="@layout/activity_welcom"/>
    </activity>
    

    ` all of your answers was useful and with them i solved it .so thanks to every one