Android 更改活动时应用程序崩溃

Android 更改活动时应用程序崩溃,android,Android,由于我有编程经验,我自愿帮助一个社区开发Android应用程序。此应用程序的一部分是需要从一个活动更改为另一个活动。当我尝试运行代码执行此操作时,程序崩溃,LogCat中出现以下异常: 03-16 20:25:29.164: ERROR/AndroidRuntime(497): FATAL EXCEPTION: main 03-16 20:25:29.164: ERROR/AndroidRuntime(497): java.lang.RuntimeException: Unable t

由于我有编程经验,我自愿帮助一个社区开发Android应用程序。此应用程序的一部分是需要从一个活动更改为另一个活动。当我尝试运行代码执行此操作时,程序崩溃,LogCat中出现以下异常:

 03-16 20:25:29.164: ERROR/AndroidRuntime(497): FATAL EXCEPTION: main
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.a.android.app/com.a.android.app.Card}: java.lang.InstantiationException: com.a.android.app.Card
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2585)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2679)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.app.ActivityThread.access$2300(ActivityThr ead.java:125)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:2033)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.os.Handler.dispatchMessage(Handler.java:99 )
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.os.Looper.loop(Looper.java:123)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at java.lang.reflect.Method.invokeNative(Native Method)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at java.lang.reflect.Method.invoke(Method.java:521)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:868)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at dalvik.system.NativeStart.main(Native Method)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): Caused by: java.lang.InstantiationException: com.a.android.app.Card
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at java.lang.Class.newInstanceImpl(Native Method)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at java.lang.Class.newInstance(Class.java:1429)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.app.Instrumentation.newActivity(Instrument ation.java:1021)
    03-16 20:25:29.164: ERROR/AndroidRuntime(497): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2577)
在谷歌搜索了一段时间后,我发现了一些建议,可能是第二个活动没有在我的AndroidManifest.xml文件中正确注册。我通过eclipse的GUI添加了第二个活动,所以我很确定它是好的,但以防万一,这里是XML文件的应用程序节点

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:icon="@drawable/icon" android:name="Card" android:label="@string/app_name"></activity>
</application>
这可能是什么原因造成的?如果它能帮助我张贴任何其他东西,请让我知道。这个错误让我耽搁了几天,我根本找不到什么

编辑:这是第二个活动的代码。不确定是什么原因造成的,所以把它全部贴了出来

public class Card extends Activity
{

    private int m_Id;
    private EditText m_SearchText;

    public Card(Bundle savedInstanceState)
    {
        try
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.card);

            Bundle extras = getIntent().getExtras();
            if(extras == null)
            {
                // nothing to do here
                return;
            }

            m_Id = extras.getInt("id");

            m_SearchText = (EditText)findViewById(R.id.txtSearch2);
            m_SearchText.setText(extras.getString("searchText"));

            Button search = (Button)findViewById(R.id.btnSearch2);

            search.setOnClickListener(new View.OnClickListener() { 
                public void onClick(View v) { 
                    ReturnToMain(m_SearchText.getText());
                }
            }); 

            m_SearchText.addTextChangedListener(new TextWatcher(){
                public void afterTextChanged(Editable s) { }

                public void beforeTextChanged(CharSequence s, int start, int count, int after)
                {
                    ReturnToMain(s);
                }

                public void onTextChanged(CharSequence s, int start, int before, int count) {}
            });

            new LongOperation().execute(this);
        }
        catch(Exception ex)
        {
            ShowError(ex.getMessage() + "\n" + ex.toString(), "Create Card");
            ex.printStackTrace();
        }
    }

    private void ReturnToMain(CharSequence s)
    {
        Intent i = new Intent();
        i.putExtra("searchText", s);
        setResult(RESULT_OK, i);
    }

    private void ShowError(String message, String title)
    {
        new AlertDialog.Builder(this)
        .setTitle("Error! - " + title)
        .setMessage(message)
        .setNeutralButton("Ok", 
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { }
        })
        .show();
    }

    private class LongOperation extends AsyncTask<Context, Void, String> {

        String cardNumber;
        String sortName;
        String cardName;
        String cardType;
        String tr3CardType;
        String attribute;
        String level;
        String icon;
        String atk;
        String def;
        String cardText;
        String cardRulings;

        Context currentContext;

        @Override
        protected String doInBackground(Context... params) {
            try {
                currentContext = params[0];
                // search the database for everything with given id
                DataBaseHelper myHelper = new DataBaseHelper(currentContext);

                try
                {
                    myHelper.createDataBase();
                }
                catch(IOException ioe)
                {
                    ShowError(ioe.getMessage(), "CDB");
                }

                try
                {
                    myHelper.openDataBase();
                }
                catch(SQLException sqle)
                {
                    ShowError(sqle.getMessage(), "ODB");
                }

                Cursor results = myHelper.ExecSql("SELECT [CardNumber], [SortName], [CardName], [CardType], [Tr3CardType], [Attribute], [Level], [Icon], [ATK], [DEF], [CardText], [CardRulings] FROM Card WHERE _id = " + String.valueOf(m_Id), null);

                results.moveToPosition(0);
                cardNumber = results.getString(1);
                sortName = results.getString(2);
                cardName = results.getString(3);
                cardType = results.getString(4);
                tr3CardType = results.getString(5);
                attribute = results.getString(6);
                level = results.getString(7);
                icon = results.getString(8);
                atk = results.getString(9);
                def = results.getString(10);
                cardText = results.getString(11);
                cardRulings = results.getString(12);

                results.close();

                results = myHelper.ExecSql("SELECT [CardName], [CardRulings] FROM Card WHERE CardRulings LIKE '%" + cardName + "%'", null);

                results.moveToFirst();
                while(!results.isAfterLast())
                {
                    String[] individualRulings = results.getString(2).split("<br>");
                    for (int i = 0; i < individualRulings.length; i++)
                    {
                        if (individualRulings[i].contains(cardName))
                        {
                            cardRulings += "\n\n[" + results.getString(1) + "]:  " + individualRulings[i];
                        }
                    }
                    results.moveToNext();
                }

                results.close();

                myHelper.close();
            } catch (Exception e) {
                ShowError(e.toString() + "\n" + e.getMessage(), "Thread");
            }

            return "";
        }

        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(String result) {
            try {
                // execution of result of Long time consuming operation
                ((TextView)findViewById(R.id.lblSortName)).setText(sortName);
                ((TextView)findViewById(R.id.lblCardName)).setText(cardName);
                ((TextView)findViewById(R.id.lblCardText)).setText(cardText);
                ((TextView)findViewById(R.id.lblCardNum)).setText("(" + cardNumber + ")");
                ((TextView)findViewById(R.id.lblCardRulings)).setText(cardRulings);

                if(tr3CardType == "Monster")
                {
                    ((TextView)findViewById(R.id.lblCardLevelAttr)).setText(", " + level + ", " + attribute);
                    ((TextView)findViewById(R.id.lblCardType)).setText(cardType + ", " + atk + "/" + def);
                }
                else
                {
                    ((TextView)findViewById(R.id.lblCardLevelAttr)).setText("");
                    if (icon != "")
                    {
                        ((TextView)findViewById(R.id.lblCardType)).setText(cardType + " (" + icon + ")");
                    }
                    else
                    {
                        ((TextView)findViewById(R.id.lblCardType)).setText(cardType);
                    }
                }

            } catch (Exception e) {
                ShowError(e.getMessage() + "\n" + e.toString(), "After Thread");
                e.printStackTrace();
            }
        }

        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
        // Things to be done before execution of long running operation. For example showing ProgessDialog
        }

        /* (non-Javadoc)
         * @see android.os.AsyncTask#onProgressUpdate(Progress[])
         */
        @Override
        protected void onProgressUpdate(Void... values) {
          // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
         }
    }
}
公共类卡扩展活动
{
私人国际货币单位Id;
私人编辑文本m_SearchText;
公共卡(捆绑存储状态)
{
尝试
{
super.onCreate(savedInstanceState);
setContentView(R.layout.card);
Bundle extras=getIntent().getExtras();
如果(附加==null)
{
//这里没什么可做的
返回;
}
m_Id=extras.getInt(“Id”);
m_SearchText=(EditText)findViewById(R.id.txtSearch2);
m_SearchText.setText(extras.getString(“SearchText”);
按钮搜索=(按钮)findViewById(R.id.btnSearch2);
search.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
ReturnToMain(m_SearchText.getText());
}
}); 
m_SearchText.addTextChangedListener(新的TextWatcher(){
公共无效后文本更改(可编辑的s){}
更改前文本之前的公共void(字符序列s、int start、int count、int after)
{
海洛托明(s);;
}
public void onTextChanged(字符序列,int start,int before,int count){}
});
new LongOperation()。执行(此);
}
捕获(例外情况除外)
{
淋浴错误(例如getMessage()+“\n”+例如toString(),“创建卡”);
例如printStackTrace();
}
}
私有void ReturnToMain(字符序列)
{
意图i=新意图();
i、 putExtra(“搜索文本”,s);
setResult(结果_OK,i);
}
私有无效错误(字符串消息、字符串标题)
{
新建AlertDialog.Builder(此)
.setTitle(“错误!-”+标题)
.setMessage(消息)
.setNeutralButton(“确定”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){}
})
.show();
}
私有类LongOperation扩展了异步任务{
字符串卡号;
字符串sortName;
字符串cardName;
字符串卡片类型;
字符串tr3CardType;
字符串属性;
字符串级;
字符串图标;
字符串atk;
字符串定义;
字符串文本;
串连裁决;
语境;
@凌驾
受保护字符串doInBackground(上下文…参数){
试一试{
currentContext=params[0];
//在数据库中搜索具有给定id的所有内容
DataBaseHelper myHelper=新的DataBaseHelper(currentContext);
尝试
{
myHelper.createDataBase();
}
捕获(ioe异常ioe)
{
淋浴ROR(ioe.getMessage(),“CDB”);
}
尝试
{
myHelper.openDataBase();
}
捕获(SQLException sqle)
{
淋浴错误(sqle.getMessage(),“ODB”);
}
游标结果=myHelper.ExecSql(“从卡片中选择[CardNumber]、[SortName]、[CardName]、[CardType]、[Tr3CardType]、[Attribute]、[Level]、[Icon]、[ATK]、[DEF]、[CardText]、[CardRulings],其中(u id=“+String.valueOf(m_id)),null);
结果:运动定位(0);
cardNumber=results.getString(1);
sortName=results.getString(2);
cardName=results.getString(3);
cardType=results.getString(4);
tr3CardType=results.getString(5);
attribute=results.getString(6);
级别=结果。getString(7);
icon=results.getString(8);
atk=results.getString(9);
def=results.getString(10);
cardText=results.getString(11);
cardRulings=results.getString(12);
结果:关闭();
结果=myHelper.ExecSql(“从卡片中选择[CardName],[CardRulings],其中CardRulings类似“%”“+CardName+“%”,null);
结果:moveToFirst();
而(!results.isAfterLast())
{
String[]individualRulings=results.getString(2).split(“
”); for(int i=0;ipublic class Card extends Activity { private int m_Id; private EditText m_SearchText; public Card(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); setContentView(R.layout.card); Bundle extras = getIntent().getExtras(); if(extras == null) { // nothing to do here return; } m_Id = extras.getInt("id"); m_SearchText = (EditText)findViewById(R.id.txtSearch2); m_SearchText.setText(extras.getString("searchText")); Button search = (Button)findViewById(R.id.btnSearch2); search.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ReturnToMain(m_SearchText.getText()); } }); m_SearchText.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { ReturnToMain(s); } public void onTextChanged(CharSequence s, int start, int before, int count) {} }); new LongOperation().execute(this); } catch(Exception ex) { ShowError(ex.getMessage() + "\n" + ex.toString(), "Create Card"); ex.printStackTrace(); } } private void ReturnToMain(CharSequence s) { Intent i = new Intent(); i.putExtra("searchText", s); setResult(RESULT_OK, i); } private void ShowError(String message, String title) { new AlertDialog.Builder(this) .setTitle("Error! - " + title) .setMessage(message) .setNeutralButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .show(); } private class LongOperation extends AsyncTask<Context, Void, String> { String cardNumber; String sortName; String cardName; String cardType; String tr3CardType; String attribute; String level; String icon; String atk; String def; String cardText; String cardRulings; Context currentContext; @Override protected String doInBackground(Context... params) { try { currentContext = params[0]; // search the database for everything with given id DataBaseHelper myHelper = new DataBaseHelper(currentContext); try { myHelper.createDataBase(); } catch(IOException ioe) { ShowError(ioe.getMessage(), "CDB"); } try { myHelper.openDataBase(); } catch(SQLException sqle) { ShowError(sqle.getMessage(), "ODB"); } Cursor results = myHelper.ExecSql("SELECT [CardNumber], [SortName], [CardName], [CardType], [Tr3CardType], [Attribute], [Level], [Icon], [ATK], [DEF], [CardText], [CardRulings] FROM Card WHERE _id = " + String.valueOf(m_Id), null); results.moveToPosition(0); cardNumber = results.getString(1); sortName = results.getString(2); cardName = results.getString(3); cardType = results.getString(4); tr3CardType = results.getString(5); attribute = results.getString(6); level = results.getString(7); icon = results.getString(8); atk = results.getString(9); def = results.getString(10); cardText = results.getString(11); cardRulings = results.getString(12); results.close(); results = myHelper.ExecSql("SELECT [CardName], [CardRulings] FROM Card WHERE CardRulings LIKE '%" + cardName + "%'", null); results.moveToFirst(); while(!results.isAfterLast()) { String[] individualRulings = results.getString(2).split("<br>"); for (int i = 0; i < individualRulings.length; i++) { if (individualRulings[i].contains(cardName)) { cardRulings += "\n\n[" + results.getString(1) + "]: " + individualRulings[i]; } } results.moveToNext(); } results.close(); myHelper.close(); } catch (Exception e) { ShowError(e.toString() + "\n" + e.getMessage(), "Thread"); } return ""; } /* (non-Javadoc) * @see android.os.AsyncTask#onPostExecute(java.lang.Object) */ @Override protected void onPostExecute(String result) { try { // execution of result of Long time consuming operation ((TextView)findViewById(R.id.lblSortName)).setText(sortName); ((TextView)findViewById(R.id.lblCardName)).setText(cardName); ((TextView)findViewById(R.id.lblCardText)).setText(cardText); ((TextView)findViewById(R.id.lblCardNum)).setText("(" + cardNumber + ")"); ((TextView)findViewById(R.id.lblCardRulings)).setText(cardRulings); if(tr3CardType == "Monster") { ((TextView)findViewById(R.id.lblCardLevelAttr)).setText(", " + level + ", " + attribute); ((TextView)findViewById(R.id.lblCardType)).setText(cardType + ", " + atk + "/" + def); } else { ((TextView)findViewById(R.id.lblCardLevelAttr)).setText(""); if (icon != "") { ((TextView)findViewById(R.id.lblCardType)).setText(cardType + " (" + icon + ")"); } else { ((TextView)findViewById(R.id.lblCardType)).setText(cardType); } } } catch (Exception e) { ShowError(e.getMessage() + "\n" + e.toString(), "After Thread"); e.printStackTrace(); } } /* (non-Javadoc) * @see android.os.AsyncTask#onPreExecute() */ @Override protected void onPreExecute() { // Things to be done before execution of long running operation. For example showing ProgessDialog } /* (non-Javadoc) * @see android.os.AsyncTask#onProgressUpdate(Progress[]) */ @Override protected void onProgressUpdate(Void... values) { // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog } } }
Intent i = new Intent(Main.this, Card.class);
android:name
public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.card);
      ....
}