单击按钮时应用程序崩溃(在两个类之间传递字符串。)

时间:2012-03-01 17:20:36

标签: java android string crash bundle

我已经开始了另一个从另一个类中获取两个字符串的类,到目前为止我只设置了一个,但我只是测试了它,但是当我填写我的两个编辑文本并点击提交时它会崩溃。

头等舱(发射器级别)

package com.gta5news.bananaphone;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

 public class LogIn extends Activity implements OnClickListener {
Button send;
EditText user;
EditText pass;
CheckBox staySignedIn;
FileOutputStream Fos;
String FILENAME = "userandpass";


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    send = (Button) findViewById(R.id.bLogIn);
    user = (EditText) findViewById(R.id.eTuser);
    pass = (EditText) findViewById(R.id.eTpassword);
    staySignedIn = (CheckBox) findViewById(R.id.Cbstay);
    send.setOnClickListener(this);
    try {
        Fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        Fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (staySignedIn.isChecked()) {

        String a = user.getText().toString();
        String b = pass.getText().toString();
        File f = new File(FILENAME);
        try {
            Fos = new FileOutputStream(f);
            //Write some Data
            Fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


    } 

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bLogIn:
        if (pass.length() == 0)
            Toast.makeText(this,
                    "Try to type in your username and password again!",
                    Toast.LENGTH_LONG).show();
        else if (user.length() == 0)
            Toast.makeText(this,
                    "Try to type in your username and password again!",
                    Toast.LENGTH_LONG).show();
        else {

            String u = user.getText().toString();
            String p = pass.getText().toString();
            Bundle send = new Bundle();
            send.putString("key", u);
            send.putString("key", p);
            Intent a = new Intent(LogIn.this, logincheck.class);
            startActivity(a);
            Toast.makeText(this, "Were signing you in!", Toast.LENGTH_LONG)
                    .show();
            break;
        }
    }

}
}

第二课:

package com.gta5news.bananaphone;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class logincheck extends Activity {
String GotBread;
TextView user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logincheck);
    user = (TextView) findViewById(R.layout.logincheck);
    SetUp();

}

public void SetUp() {
    // TODO Auto-generated method stub
    Bundle GotData = getIntent().getExtras();
    GotBread = GotData.getString("key");
    user.setText(GotBread); {


}

}
}

logcat的:

01-19 08:53:56.641: E/AndroidRuntime(3319):     ... 11 more
01-19 08:56:28.561: E/AndroidRuntime(3384): FATAL EXCEPTION: main
01-19 08:56:28.561: E/AndroidRuntime(3384): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.gta5news.bananaphone/com.gta5news.bananaphone.logincheck}: java.lang.NullPointerException
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.os.Looper.loop(Looper.java:123)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at java.lang.reflect.Method.invoke(Method.java:521)
 01-19 08:56:28.561: E/AndroidRuntime(3384):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at dalvik.system.NativeStart.main(Native Method)
01-19 08:56:28.561: E/AndroidRuntime(3384): Caused by: java.lang.NullPointerException
01-19 08:56:28.561: E/AndroidRuntime(3384):     at com.gta5news.bananaphone.logincheck.SetUp(logincheck.java:24)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at com.gta5news.bananaphone.logincheck.onCreate(logincheck.java:17)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 08:56:28.561: E/AndroidRuntime(3384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-19 08:56:28.561: E/AndroidRuntime(3384):     ... 11 more

3 个答案:

答案 0 :(得分:0)

在您的主要活动中,您正在创建Bundle并在其中插入内容,但您不会将其发送到意图。

您应该在开始第二个活动之前将信息放在Intent上。做这样的事情:

    Intent a = new Intent(LogIn.this, logincheck.class);
    a.putExtra("key", u);
    a.putExtra("key", p);
    startActivity(a);

答案 1 :(得分:0)

在Oncreate方法中获取捆绑

使用喜欢

Intent i = new Intent(Login.this,logincheck.class);
i.putExtra("key", user);
i.putExtra("key1", pass);
startActivity(i);

 user = (TextView) findViewById(R.layout.logincheck);

而不是

user = (TextView) findViewById(R.id.logincheck);

所以用户是空的

答案 2 :(得分:0)

我会试着给你一个如何解决它的线索..这可能会更加失败,但这应该对你有用..

Intent i = new Intent(LogIn.this, logincheck.class); 
i.putExtra("username", sUsername);
i.putExtra("password", SPassword);

要在新活动中获取已保存的数据,请尝试使用:

String newString;
if(extras == null) {
        sUsernameInNewActivity = null;
        sPasswordInNewActivity = null;
} else {
        sUsernameInNewActivity = extras.getString("username");
        sPasswordInNewActivity = extras.getString("password");
}

顺便说一句。在您的代码中有很多不好的做法.. :(我有麻烦阅读它很容易.. 例如:

  • 你有两个不同范围和类型的变量,但名称完全相同(尝试找到它)
  • 你想加入这个代码或???

    send.putString(“key”,u); send.putString(“key”,p);

你怎么想以后再来?一起/单独?

希望你能解决它。干杯