-1

我的项目有问题

这是我的登录活动

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {
    private final Context context = this;
    EditText un;
    EditText pw,ds;
    TextView error;
    String i;
    String x;   

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        un=(EditText)findViewById(R.id.un);
        pw=(EditText)findViewById(R.id.pwd);
        error=(TextView)findViewById(R.id.tv_error);
    }

    public void clickHandler(View view){
        Intent i = null;    
        switch (view.getId()){
            case R.id.btnLogin:
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
/*            String valid = "1";*/      
                String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("http://10.0.2.2/android/cek.php", postParameters);
                    String res = response.toString();
                    res = res.trim();
                    res = res.replaceAll("\\s+","");
                    error.setText(res);

                    if (res.equals("1")){
                        i = new Intent (this, Menu.class);
                        startActivity(i);
                    } else { 
                        error.setText("Sorry!! Username or Password salah");
                    }
                } catch (Exception e) {
                    un.setText(e.toString());
                }
                break;

            case R.id.registerBtn:
                i = new Intent(this, RegisterActivity.class);
                startActivity(i);
                break;

            case R.id.btnExit:
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Keluar dari aplikasi ini?");
                builder.setCancelable(false);
                builder.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    }
                });
                builder.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
                    @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                // Create and show the dialog
                builder.show();
                break;
        }
    }
    public  String uname(){
        x = un.getText().toString();
        return x;
    }
}

这是我的 KirimInfo.java

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class KirimInfo extends Activity {

    String time,date,type,jln,y;
    EditText usrn,ket,ds;
    Spinner nj,st1;
    TextView error;
    String[] jalan = {"A.Yani","Ambengan", "Anjasmoro","Baliwerti","Basuki Rahmat","Bintoro","Ciliwung","Darmo Raya","Darmokali","Diponegoro Raya","Dharmawangsa","Dinoyo","Embong Kemiri","Embong Malang"};
    String[] stat = {"Macet Total", "Padat", "Padat Merayap","Padat Lancar","Lancar","Sepi"};
    /** Called when the activity is first created. */
    LoginActivity l = new LoginActivity();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kirim_info);
        ket=(EditText)findViewById(R.id.keterangan);
        usrn=(EditText)findViewById(R.id.usrnm);
        error=(TextView)findViewById(R.id.tv_error3);
        nj=(Spinner)findViewById(R.id.namjal);
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, jalan);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        nj.setAdapter(aa);
        st1=(Spinner)findViewById(R.id.statusKepadatan);
        ArrayAdapter<String> bb = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, stat);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        st1.setAdapter(bb);
    }

    public void clickHandler(View view){
        Intent i = null;
        switch (view.getId()){
            case R.id.btnKirimInfo:
                jln = jalan[nj.getSelectedItemPosition()];
                type = stat[st1.getSelectedItemPosition()];
                String y = l.uname();
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("username", y));
                postParameters.add(new BasicNameValuePair("nama_jalan", jln));
                postParameters.add(new BasicNameValuePair("keterangan", ket.getText().toString()));
                postParameters.add(new BasicNameValuePair("status", type));
/*            String valid = "1";*/      
                String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("http://10.0.2.2/android/kepadatan.php", postParameters);
                    String res = response.toString();
                    res = res.trim();
                    res = res.replaceAll("\\s+","");
                    error.setText(res);
                    if (res.equals("1")) 
                        error.setText("Data Berhasil Masuk");
                    else
                        error.setText("Gagal");
                } catch (Exception e) {
                    error.setText(e.toString());
                }
                break;
            case R.id.kembali:
                i = new Intent(this, Menu.class);
                startActivity(i);
                break;
        }
    }
}

我想将用户名从 发送LoginActivity.javaKirimInfo.java。因为我使用“uname”方法来传递用户名。

但是当我运行程序时,即使我写了用户名,它也会传递空或NULL(而我已经从数据库中正确写下了用户名和密码)

我想问的是,我写的方法“uname”正确吗?你能告诉我如何编写正确的方法并启动对象,以便我可以传递用户名吗?

因为我不知道如何在android中正确地创建一个方法和启动对象

请告诉我如何....任何帮助将不胜感激

谢谢你

4

2 回答 2

1

LoginActivity l = new LoginActivity(); String y = l.uname();

这不是将价值从一个活动发送到另一个活动的正确程序。

在您的程序中添加以下内容。

LoginActivity.java添加:..

当你调用KirimInfo.javaActivity

添加

Intent intent = new Intent(LoginActivity.this,KirimInfo.class);
intent.putExtra("username",""+uname());

在 KirimInfo 活动中添加以下内容

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String userName = bundle.getString("username");
于 2012-12-10T11:10:30.627 回答
0

当您进入时,KirimInfo您正在访问的UI不是推荐的方式LoginActivityuname()

如果要在活动之间传输数据,请使用Bundle

例如

Intent intent = new Intent(LoginActivity.this, KirimInfo.class);
intent.putExtra("USER_NAME", "Safarudin");
startActivity(intent);

并在其他活动中获取数据,像这样使用OnCreate

String userName=getIntent().getStringExtra("USER_NAME");
于 2012-12-10T11:05:07.923 回答