做了一个android的登陆程序运行到ActivityManager:就结束了

manifest:
<?xml version="1.0" encoding="utf-8"?>
package="com.example.manage"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
            <uses-library android:name="android.test.runner" />
    <activity
        android:name="my"
        android:label="mine" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

mainactivity:

package com.example.manage;

import java.util.Map;

import com.example.manage.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
private EditText etNumber;
private EditText etPassword;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
MapuserInfo=Utils.getUserInfo(this);
if(userInfo!=null){
etNumber.setText(userInfo.get("number"));
etPassword.setText(userInfo.get("password"));
}
}
private void initView(){
etNumber=(EditText) findViewById(R.id.et_number);
etPassword=(EditText) findViewById(R.id.et_password);
findViewById(R.id.btn_login).setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String number=etNumber.getText().toString().trim();
String password=etPassword.getText().toString();
if(TextUtils.isEmpty(number)){
Toast.makeText(this,"请输入QQ号码",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
Log.i("MainActivity","记住密码:"+number+","+password);
boolean isSaveSuccess=Utils.saveUserInfo(this,number,password);
if(isSaveSuccess){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub

}

}

utils:

package com.example.manage;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Utils {
public static boolean saveUserInfo(Context context,String number,String password){
SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
Editor edit=sp.edit();
edit.putString("username", number);
edit.putString("pwd",password);
edit.commit();
return true;
}
public static MapgetUserInfo(Context context) {
SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
String number=sp.getString("userName", null);
String password=sp.getString("pwd",null);
MapuserMap=new HashMap();
userMap.put("number",number);
userMap.put("password", password);
return userMap;
}
}
这个是书上sharedpreferences的例子,不知道哪里错了,书上说utils类要创建在新的一个包里面,不知道到放在哪就跟mainactivity放在一个里面了。

5个回答

MainActivity.java

 package com.example.manage;

import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends  Activity implements OnClickListener{
    private EditText etNumber;
    private EditText etPassword;
    private Button etButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Map<String,String> userInfo=Utils.getUserInfo(this);
        if(userInfo!=null){
        etNumber.setText((String)userInfo.get("number"));
        etPassword.setText((String)userInfo.get("password"));
        }
    }

        private void initView(){
            etNumber=(EditText) findViewById(R.id.et_number);
            etPassword=(EditText) findViewById(R.id.et_password);
            etButton=(Button)findViewById(R.id.btn_login);
            etButton.setOnClickListener((android.view.View.OnClickListener) this);
            }

            public void onClick(View v) {
            // TODO Auto-generated method stub
            String number=etNumber.getText().toString().trim();
            String password=etPassword.getText().toString();
            if(TextUtils.isEmpty(number)){
            Toast.makeText(this,"请输入QQ号码",Toast.LENGTH_SHORT).show();
            return;
            }
            if(TextUtils.isEmpty(password)){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
            }
            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
            Log.i("MainActivity","记住密码:"+number+","+password);
            boolean isSaveSuccess=Utils.saveUserInfo(this,number,password);
            if(isSaveSuccess){
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
            }
            else{
            Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
            }
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Utils.java

 package com.example.manage;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Utils {

    public static boolean saveUserInfo(Context context,String number,String password){
        SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
        Editor edit=sp.edit();
        edit.putString("username", number);
        edit.putString("pwd",password);
        edit.commit();
        return true;
        }

        public static Map<String,String> getUserInfo(Context context) {
        SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String number=sp.getString("userName", null);
        String password=sp.getString("pwd",null);
        Map<String,String> userMap=new HashMap<String,String>();
        userMap.put("number",number);
        userMap.put("password", password);
        return userMap;
        }

}

改为这样试试

你这程序启动起来了吗,menifest文件里的配置的activity的名字与你写的类名都不相同,就能运行??

qq_34773922
qq_34773922 Manifest改过了还是不行
接近 3 年之前 回复

你可以Log一下userInfo這個值,這個值userInfo是不爲空的,然後etNumber.setText(userInfo.get("number"));
etPassword.setText(userInfo.get("password"));這兩句,應該要有一個强轉,不然會報錯空值的錯。

等一下,我上机看看。。。。。。。

activity_main.xml

 <LinearLayout 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="com.example.manage.MainActivity" 
    android:orientation="vertical">



    <EditText
        android:id="@+id/et_number"
        android:inputType="number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:inputType="textPassword"
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button 
        android:id="@+id/btn_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textSize="20sp"/>


</LinearLayout>

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!