java.lang.RuntimeException:从Intent调用时无法启动活动ComponentInfo



我知道这个问题在我做研究的时候被问了很多次。事实上,我检查了以下帖子,但它们没有帮助,所以我发布了我的问题:

java.lang.RuntimeException:无法启动活动ComponentInfo

Android无法启动活动-java.lang.RuntimeException:无法启动活动ComponentInfo

Android无法启动活动componentinfo错误时调用另一个类

我的代码的基本思想是从另一个设备获取一个字符串,根据分隔符对其进行标记,并将登录活动发送给它。到目前为止,标记化是有效的,但错误在于打开活动页面,所以我怀疑错误在Login.java中,但我不知道它是什么。Android Studio本身在编译过程中没有显示任何错误。此外,我确实在清单文件中包含了所有内容。

这是日志:

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.access$800(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();
        }
    }
}
 }

这是最初启动的活动,它负责获取字符串并打开登录活动。

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

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中,或者在尝试解析之前验证您的输入。

此外,如果您进一步查看堆栈跟踪,它将准确地告诉您问题发生的位置。你在帖子的好部分剪掉了你的堆栈痕迹。

您正在使用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文件。您是否为"登录"活动添加了活动标记。

最新更新