Krishna
Krishna

Reputation: 621

One of the two setContentView() not working

This is the code I have written. I have implemented LocationListener methods which I did not paste here. I felt it is unnecessary. Here I want the activity_main.xml to be displayed for the first launch. So I used SharedPreferences as explained here. Even when the app is launched for the first time, the activity_main.xml is not showing up. The Toast notification saying Registration started is displayed so I know the if statement is getting executed. I can't figure out why activity_map is always loading.

public class MainActivity extends Activity implements LocationListener{
EditText d_name,d_phone,d_email;
Button submit=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences pref = getSharedPreferences("hello", MODE_PRIVATE);

    if(pref.getBoolean("firststart", true)){
        //Checking weather this is executed or not
        Toast.makeText(getApplicationContext(), "Registration started", 
                   Toast.LENGTH_LONG).show();
        //setting layout on the screen
        setContentView(R.layout.activity_main);
        // update sharedpreference - another start wont be the first
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("firststart", false);
        editor.commit(); // apply changes
        // first start, show your dialog | first-run code goes here
        d_name = (EditText)findViewById(R.id.d_name);
        d_phone = (EditText)findViewById(R.id.d_phone);
        d_email = (EditText) findViewById(R.id.d_mail);
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                final String name = d_name.getText().toString();
                final long ph =Long.parseLong(d_phone.getText().toString());
                final String mail = d_email.getText().toString();   
                '
                '
                .(Save to db)
            }});
    }
    setContentView(R.layout.activity_map);
    initializeMap();
}

If needed, this is my xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
    android:layout_centerHorizontal="true"
    android:id="@+id/details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/details"
    android:textSize="20sp" />

<EditText
    android:id="@+id/d_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_name"
    android:layout_below="@id/details"
    android:inputType="text" />

<EditText
    android:id="@+id/d_phone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/d_name"
    android:layout_marginTop="26dp"
    android:ems="10"
    android:hint="@string/hint_phone"
    android:inputType="phone" />

<EditText
    android:id="@+id/d_mail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/d_phone"
    android:layout_marginTop="26dp"
    android:hint="@string/hint_email"
    android:inputType="textEmailAddress" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/d_mail"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:hint="@string/button" />  
</RelativeLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.prasad.currentlocator"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBLG6F6G8Yi9MYDPdyetgSVgXxhVNYIdyw" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MapActivity"
        android:label="@string/title_activity_map" >
    </activity>
</application>

</manifest>

Upvotes: 2

Views: 371

Answers (3)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You are putting wrong setContentView(R.layout.activity_map); . Little mistake in your statement .

  1. Where is your else {} Part

At first Set if-else block properly .

Upvotes: 1

Henry
Henry

Reputation: 17841

setContentView() is called twice in your code. Make sure that in your onCreate() the setContentView() is encountered only once in all flows. Here you need to have an else statement.

From my experience, I would suggest that you call setContentView() outside the If/Else, i.e, have just one layout. Then, based on the condition alter the stuff inside the layout. I suggest this, because this would later become a maintenance nightmare.

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

This

setContentView(R.layout.activity_map);

is always getting called because it runs after everythin g in if. You need an if/else

if(pref.getBoolean("firststart", true)){
    // do all your stuff
} else {
    setContentView(R.layout.activity_map);
    // do anything else that should run if not first start
}

Upvotes: 4

Related Questions