Javascript Android应用程序在收到意向包时崩溃?
首先,我要说我是一个完全的新手,这是我的第一个项目,我已经尝试到处搜索来回答我的问题 我在android上设置了一个登录屏幕。一旦用户点击登录按钮并且登录名/密码正确(在PHP/SQL中验证),它将向下一个活动发送一个意图/捆绑包 以下是发送意向/捆绑包的代码:Javascript Android应用程序在收到意向包时崩溃?,javascript,android,string,android-intent,crash,Javascript,Android,String,Android Intent,Crash,首先,我要说我是一个完全的新手,这是我的第一个项目,我已经尝试到处搜索来回答我的问题 我在android上设置了一个登录屏幕。一旦用户点击登录按钮并且登录名/密码正确(在PHP/SQL中验证),它将向下一个活动发送一个意图/捆绑包 以下是发送意向/捆绑包的代码: Intent intent = new Intent(this, Homepage.class); EditText uname2 = (EditText)findViewById(R.id.txt_username); String
Intent intent = new Intent(this, Homepage.class);
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
Intent intent2 = new Intent(this, Homepage.class);
intent2.putExtra("username2", username2);
startActivity(intent);
以下是在下一个活动中接收意向包的代码:
Intent intent2 = getIntent();
String usernamefromlogin = intent2.getExtras().getString("username2");
String url5 = url.concat(usernamefromlogin);
TextView text = (TextView) findViewById(R.id.errorchecking);
text.setText(url5);
Intent intent = new Intent(this, Homepage.class);
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
intent.putExtra("username2", username2);
startActivity(intent);
我使用concat()将URL放在意图包之前,因为该URL将用作PHP文件的_GET命令(但我离题了)。目前,我已将其设置为将文本放置到文本视图中,以便查看最终结果,但在我到达该点之前,应用程序崩溃
当我点击登录按钮时,我的应用程序崩溃。这是我得到的日志:
11-30 20:06:39.449: E/AndroidRuntime(4578): FATAL EXCEPTION: main
11-30 20:06:39.449: E/AndroidRuntime(4578): Process: com.sencide, PID: 4578
11-30 20:06:39.449: E/AndroidRuntime(4578): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sencide/com.sencide.Homepage}: java.lang.NullPointerException
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.os.Handler.dispatchMessage(Handler.java:102)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.os.Looper.loop(Looper.java:137)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-30 20:06:39.449: E/AndroidRuntime(4578): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 20:06:39.449: E/AndroidRuntime(4578): at java.lang.reflect.Method.invoke(Method.java:515)
11-30 20:06:39.449: E/AndroidRuntime(4578): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-30 20:06:39.449: E/AndroidRuntime(4578): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-30 20:06:39.449: E/AndroidRuntime(4578): at dalvik.system.NativeStart.main(Native Method)
11-30 20:06:39.449: E/AndroidRuntime(4578): Caused by: java.lang.NullPointerException
11-30 20:06:39.449: E/AndroidRuntime(4578): at com.sencide.Homepage.onCreate(Homepage.java:46)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.Activity.performCreate(Activity.java:5243)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-30 20:06:39.449: E/AndroidRuntime(4578): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-30 20:06:39.449: E/AndroidRuntime(4578): ... 11 more
11-30 20:06:42.569: I/Process(4578): Sending signal. PID: 4578 SIG: 9
这是我的全部代码:
AndroidLogin.java
public class AndroidLogin extends Activity implements OnClickListener {
Button ok,back,exit;
TextView result;
String thisisausername;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView)findViewById(R.id.tbl_result);
}
public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
// login.php returns true if username and password match in db
HttpPost httppost = new HttpPost("http://10.0.2.2/login.php");
try {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
result.setText("Login Successful! Please Wait...");
}else
{
Log.w("SENCIDE", "FALSE");
result.setText(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
public void RegisterButton(View view) {
String url = "http://10.0.2.2/register.php";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
@Override
public void onClick(View view) {
postLoginData();
// turns the text in the textview "Tbl_result" into a text string called "tblresult"
TextView tblresult = (TextView) findViewById(R.id.tbl_result);
// If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
Intent intent = new Intent(this, Homepage.class);
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
intent.putExtra("username2", username2);
startActivity(intent);
}
}
}
JSONParser.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Main.XML(第一个登录屏幕)
reshomepage.XML(我试图将意图发送到的第二个屏幕)
这里的问题是,您创建了两个意图(intent和intent2),然后将用户名保存在intent2中,但正在调用startActivity(intent) 因此,基本上,删除其中一个意图,您的代码应该如下所示:
Intent intent = new Intent(this, Homepage.class);
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
intent.putExtra("username2", username2);
startActivity(intent);
让我知道这是如何进行的这里的问题是您已将用户名2附加到intent2,但您没有使用intent2 您启动活动,然后在传入的意图中查找额外的“username2”,因为您发送的意图不包含您从login获得的额外“username2”,并为usernamefromlogin分配了null值,从而导致堆栈跟踪中出现null指针异常 只需去掉第二个意图,并在开始活动时使用的字符串中添加额外的字符串:
Intent intent2 = getIntent();
String usernamefromlogin = intent2.getExtras().getString("username2");
String url5 = url.concat(usernamefromlogin);
TextView text = (TextView) findViewById(R.id.errorchecking);
text.setText(url5);
Intent intent = new Intent(this, Homepage.class);
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
intent.putExtra("username2", username2);
startActivity(intent);
为了安全起见,在你跑掉并使用它之前,也许要确保它有额外的意图
Intent intent = getIntent();
if (intent.hasExtra("username2") {
String usernamefromlogin = intent.getExtras().getString("username2");
String url5 = url.concat(usernamefromlogin);
TextView text = (TextView) findViewById(R.id.errorchecking);
text.setText(url5);
} else {
// ...appropriate error handling goes here!
}
祝你好运 感谢您的快速回复,我现在在Logcat中发现了这个错误:有什么想法吗?我添加了全部代码以及第49行:
user=json.getJSONArray(TAG_user)代码>?如果是,请粘贴json数据的演示。如果第49行与ui有关,请粘贴布局xml代码。非常感谢@Aftab的帮助。我刚刚发布了我剩下的代码。main.xml文件中的第49行是第一个活动中的登录按钮。我指的是HomePage.java
中的第49行。不确定是否添加了额外的行或删除了任何行。LogCat显示导致错误的行,这使得修复变得容易。因此,如果您对代码进行了任何更改,请再次运行该应用程序,并从logCat中给出导致错误的行。应该是<代码>由…引起的。。。。(HomePage.Java:49)
看起来像“text.setText(url5);”是第49行。所以它在将文本设置为文本视图时遇到问题?这可能是什么原因?我甚至试着删除“TextView text=(TextView)findViewById(R.id.errorchecking);”并用“errorchecking.setText(url5);”替换setText,但我得到的错误仍然在同一行。我的猜测是字符串url5的格式不正确,有没有办法找出原因?
Intent intent = getIntent();
if (intent.hasExtra("username2") {
String usernamefromlogin = intent.getExtras().getString("username2");
String url5 = url.concat(usernamefromlogin);
TextView text = (TextView) findViewById(R.id.errorchecking);
text.setText(url5);
} else {
// ...appropriate error handling goes here!
}