这是我的登录活动课,我想在登录后登录它显示地图但是当我运行应用程序时它显示不幸停止登录
loginActivity.java
package com.ivb.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity{
EditText edtemail,edtPassword;
Button btnLogin;
String strUsername,strPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
edtemail = (EditText)this.findViewById(R.id.edt_email);
edtPassword = (EditText)this.findViewById(R.id.edt_passwrd);
btnLogin = (Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
strUsername = edtemail.getText().toString().trim();
strPassword = edtPassword.getText().toString().trim();
if(strUsername.equals("needin@gmail.com") && strPassword.equals("needin123"))
startActivity(new Intent(LoginActivity.this,LoginSuccess.class).putExtra("usr",(CharSequence)strUsername));
else
Toast.makeText(LoginActivity.this,"Invalid UserName or Password", Toast.LENGTH_LONG).show();
}
});
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
loginsuccess.java
package com.ivb.login;
import javax.security.auth.PrivateCredentialPermission;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
public class LoginSuccess extends Activity {
private GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
Intent in = getIntent();
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText("Welcome \n "+in.getCharSequenceExtra("usr"));
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap =((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ivb.login"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.ivb.login.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission android:name="com.ivb.login.permission.MAPS_RECEIVE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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="com.ivb.login.LoginActivity"
android:label="@string/app_name"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.ivb.login.RegisterActivity"
android:label="Register New Account"></activity>
<activity android:name="com.ivb.login.LoginSuccess"></activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC-2jAJ7MEkho_gJv7KLeb-tHrU2zHDUQU" />
<uses-library android:name="com.google.android.maps"/>
</application>
</manifest>
second.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"
tools:context=".LoginSuccess" >
<LinearLayout android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip" >
<ImageView android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
</LinearLayout>
<TextView
android:id="@+id/showmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Welcome"
android:textSize="20dip" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TextView>
</RelativeLayout>
login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<LinearLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/header"
android:orientation="vertical"
android:padding="10dip" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="@drawable/logo1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textColor="#372c24" />
<EditText
android:id="@+id/edt_email"
android:layout_width="209dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:ems="10"
android:hint="Enter Email Address"
android:inputType="textEmailAddress"
android:singleLine="true"
android:typeface="normal" >
<requestFocus />
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password"
android:textColor="#372c24" />
<EditText
android:id="@+id/edt_passwrd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dip"
android:ems="10"
android:hint="Enter password"
android:inputType="textPassword"
android:password="true"
android:singleLine="true"
android:typeface="normal" />
<Button
android:id="@+id/btnLogin"
android:layout_width="133dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:background="#acd28a"
android:clickable="true"
android:gravity="center"
android:paddingLeft="15dip"
android:text="Login" />
<TextView
android:id="@+id/link_to_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip"
android:layout_marginTop="40dip"
android:gravity="center"
android:text="Need an account?sign up"
android:textColor="#0b84aa"
android:textSize="20dip" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:2)
从
改变<TextView
android:id="@+id/showmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Welcome"
android:textSize="20dip" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TextView>
到
<TextView
android:id="@+id/showmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Welcome"
android:textSize="20dip" >
</TextView>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这是因为你的&lt; / TextView的&GT;在片段类之后关闭,所以你得到了这个错误。
此处也改变
startActivity(new Intent(LoginActivity.this,LoginSuccess.class).putExtra("usr",strUsername));
以及 LoginSuccess 活动
Intent in = getIntent();
if (in.getStringExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText("Welcome \n "+in.getStringExtra("usr"));
}
这意味着您必须在清单文件中声明此部分......
<uses-feature android:glEsVersion="0x00020000" android:required="true/>
并且必须使用最新更新的Google Play服务。