-1

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);
  }
KishanCS
  • 1,357
  • 1
  • 19
  • 38
Programmer
  • 21
  • 10
  • 1
    Why do you want to load setContentView(R.layout.activity_main) onClick?are you trying to load another activity – tahsinRupam Mar 07 '17 at 10:18
  • Post your stack trace – Sony Mar 07 '17 at 10:19
  • if your app crashed => you must get an error log! – Atef Hares Mar 07 '17 at 10:20
  • no , i want to show a welcome message when the app starts (layout of activity_welcom)then when user click on the Button ,then load the main activity. – Programmer Mar 07 '17 at 10:22
  • if you want to display a welcome screen and then show another screen you can either create splash screen with your welcome screen and then after sometime you can show the activity using [Handler](https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)) or you can use a [ViewStub](https://developer.android.com/training/improving-layouts/loading-ondemand.html) – Sony Mar 07 '17 at 10:24
  • @Programmer, check my answer. – tahsinRupam Mar 07 '17 at 10:27
  • android.content.ActivityNotFoundException: Unable to find explicit activity class {com.blog.software.app/com.blog.software.app.WelcomeClass}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794) ............................................ so this error is what i get in the log – Programmer Mar 07 '17 at 11:25

8 Answers8

3

You should use Intent rather than using setContentView(R.layout.activity_main) on Click

 button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
             Intent k = new Intent(Welcomactivity.this, MainActivity.class);
             startActivity(k);

                }

            });
Liya
  • 568
  • 7
  • 28
1

I suggest you to use Fragments: Your activity starts, show the first fragment with the welcome message and the button, the user clicks the button and then you replace the first fragment(without adding it to the backstack so you won't go back to it when back is tapped) with the second one! have a look here

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
1

Use this to load MainActivity:

View.OnClickListener welcomeListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(YourCurrentActivity.this, MainActivity.class);
            startActivity(intent);
        }
    };
    btn.setOnClickListener(welcomeListener);
  }
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
1

Try this -

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent()!=null){
            if (getIntent().getBooleanExtra("REFRESH",false)){
                setContentView(R.layout.activity_main);
            } else{
                setContentView(R.layout.activity_welcom);
            }
        }else{
            setContentView(R.layout.activity_welcom);
        }

        Button btn = (Button) findViewById(R.id.button);
        View.OnClickListener welcomeListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(this,CurrentClass.class);
                mIntent.putExtra("REFRESH",true);
                startActivity(mIntent);
            }
        };
        btn.setOnClickListener(welcomeListener);
    }
Shoeb Siddique
  • 2,805
  • 1
  • 22
  • 42
1

Best option to add multiple layout in one activity is to use fragments in your activity which gives better performance. setContentView is not a preferred way and it should be used only inside your Activity once.

Read this docs you will get better knowledge about everything you need to do: enter link description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kamal Oli
  • 55
  • 1
  • 9
0

That's just not the way it works. You can either show a dialog with your welcome message or use a ViewSwitcher and switch the layout onClick. Another option would be start a new activity onClick with the main layout, whatever you do, don't setContentView more than one time in this case.

0

As mentioned before, it's better to use Intent for switching between screens. Start with your Welcome activity, and then run next one onClick:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

Also, you can try use Fragments for this purpose. But better practice – is just put your splash screen over your main activity layout in a FrameLayout and make it setVisibility(Visibility.GONE) onClick.

sashk0
  • 821
  • 10
  • 10
-1

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

Programmer
  • 21
  • 10