java.lang.RuntimeException: 从 Intent 调用时无法启动 activity ComponentInfo
java.lang.RuntimeException: Unable to start activity ComponentInfo when called from an Intent
我知道这个问题在我做研究时已经被问过很多次了。事实上,我检查了以下帖子,但它们没有帮助,所以我发布了我的问题:
java.lang.RuntimeException: Unable to start activity ComponentInfo
Android unable to start activity componentinfo error when calling another class
我的代码的基本思想是从另一台设备获取一个字符串,根据定界符对其进行标记,然后将其发送给登录 activity。到目前为止标记化有效,但错误在于打开 activity 页面,所以我怀疑错误在 Login.java 中,但我无法弄清楚它是什么。 Android Studio 本身在编译期间不会显示任何错误。另外,我确实在清单文件中包含了所有内容。
这里是 logcat:
03-23 13:41:04.528 24068-24068/com.example.home.mysqltest I/message﹕ 1
03-23 13:41:04.528 24068-24068/com.example.home.mysqltest I/message﹕ b
03-23 13:41:04.528 24068-24068/com.example.home.mysqltest I/message﹕ c
03-23 13:41:04.701 24068-24068/com.example.home.mysqltest D/AndroidRuntime﹕
Shutting down VM
03-23 13:41:04.702 24068-24068/com.example.home.mysqltest E/AndroidRuntime﹕
FATAL EXCEPTION: main
Process: com.example.home.mysqltest, PID: 24068
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.home.mysqltest/com.example.home.mysqltest.login}: java.util.NoSuchElementException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.util.NoSuchElementException
这里是login.java
package com.example.home.mysqltest;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class login extends Activity implements OnClickListener{
private EditText user, pass;
private String user1,pass1;
private Button mSubmit;
String fileno;
String name;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://nfcquickbanker.esy.es/WebServer/login.php";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//setup input
fileno=(String)MainActivity.uni.nextElement().toString();
user1 = (String) MainActivity.uni.nextElement().toString();
pass1 = (String)MainActivity.uni.nextElement().toString();
user = (EditText)findViewById(R.id.username);
user.setText(user1, TextView.BufferType.EDITABLE);
pass = (EditText)findViewById(R.id.password);
pass.setText(pass1, TextView.BufferType.EDITABLE);
//setup buttons
mSubmit = (Button)findViewById(R.id.login);
//register listeners
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(login.this);
SharedPreferences.Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
switch (fileno)
{
case "1": name="dd";
break;
case "2": name="deposit";
break;
case "3": name="travellerscheck";
break;
default: break;
}
try
{
Class openact = Class.forName("com.example.home.mysqltest." + name);
Intent a = new Intent(login.this, openact);
startActivity(a);
}
catch (Exception e)
{
e.printStackTrace();
}
return json.getString(TAG_MESSAGE);
}
else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
Intent a = new Intent(login.this, loginfail.class);
startActivity(a);
return json.getString(TAG_MESSAGE);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
这里是最初启动的activity,它负责获取字符串并打开登录activity。
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.TextView;
import java.util.*;
public class MainActivity extends Activity {
private TextView mTextView;
static StringTokenizer uni;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_view);
}
@Override
protected void onResume(){
super.onResume();
Intent intent = getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMessages = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage message = (NdefMessage) rawMessages[0]; // only one message transferred
uni=new StringTokenizer(new String(message.getRecords()[0].getPayload()),"~");
while(uni.hasMoreElements())
{
String s=uni.nextElement().toString();
Log.i("message",s);
}
/* mTextView.setText(new String(message.getRecords()[0].getPayload()));
Intent i=new Intent(MainActivity.this,login.class);
finish();
startActivity(i);
*/
try
{
Intent a = new Intent(MainActivity.this, login.class);
startActivity(a);
}
catch (Exception e)
{
e.printStackTrace();
}
mTextView.setText(new String(message.getRecords()[0].getPayload()));
} else
mTextView.setText("Waiting for NDEF Message");
}
}
您正在使用 StringTokenizer
所以
替换
while(uni.hasMoreElements())
{
String s=uni.nextElement().toString();
Log.i("message",s);
}
与
while(uni.hasMoreTokens())
{
String s=uni.nextToken().toString();
Log.i("message",s);
}
您应该检查您的 manifest.xml 文件。您是否为 'login' activity 添加了 activity 标签。
在 login.onCreate()
你做的:
//setup input
fileno=(String)MainActivity.uni.nextElement().toString();
user1 = (String) MainActivity.uni.nextElement().toString();
pass1 = (String)MainActivity.uni.nextElement().toString();
其中一个失败了。任何对 nextElement()
的调用都可能抛出 NoSuchElementException
,这将导致此问题。
您需要更健壮地编写代码。将这些东西包装在 try/catch 中或在尝试解析之前验证您的输入。
此外,如果您进一步查看堆栈跟踪,它会准确告诉您问题出在哪里。你在你的 post 中切断了你的堆栈跟踪。
我知道这个问题在我做研究时已经被问过很多次了。事实上,我检查了以下帖子,但它们没有帮助,所以我发布了我的问题:
java.lang.RuntimeException: Unable to start activity ComponentInfo
Android unable to start activity componentinfo error when calling another class
我的代码的基本思想是从另一台设备获取一个字符串,根据定界符对其进行标记,然后将其发送给登录 activity。到目前为止标记化有效,但错误在于打开 activity 页面,所以我怀疑错误在 Login.java 中,但我无法弄清楚它是什么。 Android Studio 本身在编译期间不会显示任何错误。另外,我确实在清单文件中包含了所有内容。
这里是 logcat:
03-23 13:41:04.528 24068-24068/com.example.home.mysqltest I/message﹕ 1
03-23 13:41:04.528 24068-24068/com.example.home.mysqltest I/message﹕ b
03-23 13:41:04.528 24068-24068/com.example.home.mysqltest I/message﹕ c
03-23 13:41:04.701 24068-24068/com.example.home.mysqltest D/AndroidRuntime﹕
Shutting down VM
03-23 13:41:04.702 24068-24068/com.example.home.mysqltest E/AndroidRuntime﹕
FATAL EXCEPTION: main
Process: com.example.home.mysqltest, PID: 24068
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.home.mysqltest/com.example.home.mysqltest.login}: java.util.NoSuchElementException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.util.NoSuchElementException
这里是login.java
package com.example.home.mysqltest;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class login extends Activity implements OnClickListener{
private EditText user, pass;
private String user1,pass1;
private Button mSubmit;
String fileno;
String name;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://nfcquickbanker.esy.es/WebServer/login.php";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//setup input
fileno=(String)MainActivity.uni.nextElement().toString();
user1 = (String) MainActivity.uni.nextElement().toString();
pass1 = (String)MainActivity.uni.nextElement().toString();
user = (EditText)findViewById(R.id.username);
user.setText(user1, TextView.BufferType.EDITABLE);
pass = (EditText)findViewById(R.id.password);
pass.setText(pass1, TextView.BufferType.EDITABLE);
//setup buttons
mSubmit = (Button)findViewById(R.id.login);
//register listeners
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(login.this);
SharedPreferences.Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
switch (fileno)
{
case "1": name="dd";
break;
case "2": name="deposit";
break;
case "3": name="travellerscheck";
break;
default: break;
}
try
{
Class openact = Class.forName("com.example.home.mysqltest." + name);
Intent a = new Intent(login.this, openact);
startActivity(a);
}
catch (Exception e)
{
e.printStackTrace();
}
return json.getString(TAG_MESSAGE);
}
else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
Intent a = new Intent(login.this, loginfail.class);
startActivity(a);
return json.getString(TAG_MESSAGE);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
这里是最初启动的activity,它负责获取字符串并打开登录activity。
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.TextView;
import java.util.*;
public class MainActivity extends Activity {
private TextView mTextView;
static StringTokenizer uni;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_view);
}
@Override
protected void onResume(){
super.onResume();
Intent intent = getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMessages = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage message = (NdefMessage) rawMessages[0]; // only one message transferred
uni=new StringTokenizer(new String(message.getRecords()[0].getPayload()),"~");
while(uni.hasMoreElements())
{
String s=uni.nextElement().toString();
Log.i("message",s);
}
/* mTextView.setText(new String(message.getRecords()[0].getPayload()));
Intent i=new Intent(MainActivity.this,login.class);
finish();
startActivity(i);
*/
try
{
Intent a = new Intent(MainActivity.this, login.class);
startActivity(a);
}
catch (Exception e)
{
e.printStackTrace();
}
mTextView.setText(new String(message.getRecords()[0].getPayload()));
} else
mTextView.setText("Waiting for NDEF Message");
}
}
您正在使用 StringTokenizer
所以
替换
while(uni.hasMoreElements())
{
String s=uni.nextElement().toString();
Log.i("message",s);
}
与
while(uni.hasMoreTokens())
{
String s=uni.nextToken().toString();
Log.i("message",s);
}
您应该检查您的 manifest.xml 文件。您是否为 'login' activity 添加了 activity 标签。
在 login.onCreate()
你做的:
//setup input
fileno=(String)MainActivity.uni.nextElement().toString();
user1 = (String) MainActivity.uni.nextElement().toString();
pass1 = (String)MainActivity.uni.nextElement().toString();
其中一个失败了。任何对 nextElement()
的调用都可能抛出 NoSuchElementException
,这将导致此问题。
您需要更健壮地编写代码。将这些东西包装在 try/catch 中或在尝试解析之前验证您的输入。
此外,如果您进一步查看堆栈跟踪,它会准确告诉您问题出在哪里。你在你的 post 中切断了你的堆栈跟踪。