这是我写的代码。我已经实现了LocationListener
方法,我没有粘贴在这里。我觉得没必要。
在这里,我希望第一次启动时显示activity_main.xml
。所以我使用SharedPreferences
解释here。即使首次启动应用,activity_main.xml
也未显示。显示注册已启动的Toast
通知已显示,因此我知道if
语句正在执行。我无法弄清楚为什么activity_map
总是加载。
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();
}
如果需要,这是我的xml文件
<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>
清单文件
<?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>
答案 0 :(得分:4)
这个
setContentView(R.layout.activity_map);
总是被调用,因为它在if
中的每一个之后运行。您需要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
}
答案 1 :(得分:1)
你错了setContentView(R.layout.activity_map);
。你的陈述中的错误。
else {}
部分 首先正确设置if-else
阻止。
答案 2 :(得分:1)
setContentView()
两次。确保在onCreate()中,setContentView()
仅在所有流中遇到一次。在这里,你需要一个else
声明。
根据我的经验,我建议您在If / Else之外调用setContentView(),即只有一个布局。然后,根据条件改变布局内的东西。我建议这样做,因为这会成为维护的噩梦。