【问题标题】:App Crashes when Button is clicked (Passing a string between two classes.)单击按钮时应用程序崩溃(在两个类之间传递字符串。)
【发布时间】:2012-03-01 17:20:36
【问题描述】:

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

头等舱(发射器类)

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

【问题讨论】:

  • 抛出了什么异常?
  • 你能从你的 logcat 中发布异常吗?
  • 由于某种原因,您的 extras Bundle 为空。进行我们建议的所有这些更改,并使用您的新代码和新异常更新线程。
  • 发生了什么,正在更改您的代码和 logcat...?

标签: java android string crash bundle


【解决方案1】:

在 Oncreate 方法中获取 Bundle

和

使用喜欢

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】:

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

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

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

    【讨论】:

    • 你遇到了同样的异常?
    【解决方案3】:

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

    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);

    你以后怎么想得到它们?一起/分开?

    希望你能解决它。干杯

    【讨论】:

    • 你能指出我的语法错误吗..? sPassword 中有错字(第一个字母不应该是大写字母)但是你还有什么其他问题?
    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多