Android应用程序强制关闭

时间:2014-01-19 17:42:53

标签: java android

我需要知道如何调试它。

 01-19 17:36:09.206: E/AndroidRuntime(487): FATAL EXCEPTION: main
    01-19 17:36:09.206: E/AndroidRuntime(487): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.aoutsu/com.example.aoutsu.AddNewItem}; have you declared this activity in your AndroidManifest.xml?
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.app.Activity.startActivityForResult(Activity.java:2827)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.app.Activity.startActivity(Activity.java:2933)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at com.example.aoutsu.HomePage$1.onClick(HomePage.java:50)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.view.View.performClick(View.java:2485)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.view.View$PerformClick.run(View.java:9080)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.os.Handler.handleCallback(Handler.java:587)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.os.Handler.dispatchMessage(Handler.java:92)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.os.Looper.loop(Looper.java:123)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at java.lang.reflect.Method.invoke(Method.java:507)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    01-19 17:36:09.206: E/AndroidRuntime(487):  at dalvik.system.NativeStart.main(Native Method)

这是我的Homepage.java

package com.example.aoutsu;

import java.util.ArrayList;




import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class HomePage extends Activity {
    private DbHelper mHelper;
    private SQLiteDatabase dataBase;

    private ArrayList<String> item_id = new ArrayList<String>();
    private ArrayList<String> name_item = new ArrayList<String>();
    private ArrayList<String> desc_item = new ArrayList<String>();
    private ArrayList<String> quantity = new ArrayList<String>();

    private ListView itemList;
    private AlertDialog.Builder build;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        itemList = (ListView) findViewById(R.id.listView);

        mHelper = new DbHelper(this);
        findViewById(R.id.addBtn).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),
                        AddNewItem.class);
                i.putExtra("update", false);
                startActivity(i);

            }
        });

        //click to update data
        itemList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Intent i = new Intent(getApplicationContext(),
                        AddNewItem.class);
                i.putExtra("ItemName", name_item.get(arg2));
                i.putExtra("ItemDesc", desc_item.get(arg2));
                i.putExtra("Quantity", quantity.get(arg2));
                i.putExtra("ID", item_id.get(arg2));
                i.putExtra("update", true);
                startActivity(i);
            }
        });



    //long click to delete data
    itemList.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {

            build = new AlertDialog.Builder(HomePage.this);
            build.setTitle("Delete " + name_item.get(arg2) + " "
                    + desc_item.get(arg2) + quantity.get(arg2));
            build.setMessage("Do you want to delete ?");
            build.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {

                            Toast.makeText(
                                    getApplicationContext(),
                                    name_item.get(arg2) + " "
                                            + desc_item.get(arg2) +" " + quantity.get(arg2)
                                            + " is deleted.", 3000).show();

                            dataBase.delete(
                                    DbHelper.TABLE_NAME,
                                    DbHelper.KEY_ID + "="
                                            + item_id.get(arg2), null);
                            displayData();
                            dialog.cancel();
                        }
                    });

            build.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = build.create();
            alert.show();

            return true;
        }
    });
}

@Override
    protected void onResume() {
    displayData();
    super.onResume();
    }

/**
 * displays data from SQLite
 */
    private void displayData() {
    dataBase = mHelper.getWritableDatabase();
    Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
            + DbHelper.TABLE_NAME, null);

    item_id.clear();
    name_item.clear();
    desc_item.clear();
    quantity.clear();
    if (mCursor.moveToFirst()) {
        do {
            item_id.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
            name_item.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ITEMNAME)));
            desc_item.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ITEMDESC)));
            quantity.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ITEMQUANTITY)));

        } while (mCursor.moveToNext());
    }
    DisplayAdapter disadpt = new DisplayAdapter(HomePage.this,item_id, name_item, desc_item,quantity);
    itemList.setAdapter(disadpt);
    mCursor.close();
}


}

这是我的AddNewItem.java

package com.example.aoutsu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddNewItem extends Activity implements OnClickListener{

    private Button btn_save;
    private EditText edit_name,edit_desc,edit_quantity;
    private DbHelper mHelper;
    private SQLiteDatabase dataBase;
    private String id,name,desc,quantity;
    private boolean isUpdate;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_item);

            btn_save=(Button)findViewById(R.id.save_btn);
            edit_name=(EditText)findViewById(R.id.item_name);
            edit_desc=(EditText)findViewById(R.id.item_desc);
            edit_quantity=(EditText)findViewById(R.id.Quantity);
           isUpdate=getIntent().getExtras().getBoolean("update");
            if(isUpdate)
            {
                id=getIntent().getExtras().getString("ID");
                name=getIntent().getExtras().getString("ItemName");
                desc=getIntent().getExtras().getString("ItemDesc");
                quantity=getIntent().getExtras().getString("Quantity");
                edit_name.setText(name);
                edit_desc.setText(desc);
                edit_quantity.setText(quantity);

            }

             btn_save.setOnClickListener(this);

             mHelper=new DbHelper(this);

        }

        // saveButton click event 
        public void onClick(View v) {
            name=edit_name.getText().toString().trim();
            desc=edit_desc.getText().toString().trim();
            quantity=edit_quantity.getText().toString().trim();
            if(name.length()>0 && desc.length()>0 && quantity.length()>0 )
            {
                saveData();
            }
            else
            {
                AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AddNewItem.this);
                alertBuilder.setTitle("Invalid Data");
                alertBuilder.setMessage("Please, Enter valid data");
                alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                    }
                });
                alertBuilder.create().show();
            }

        }

        /**
         * save data into SQLite
         */
        private void saveData(){
            dataBase=mHelper.getWritableDatabase();
            ContentValues values=new ContentValues();

            values.put(DbHelper.KEY_ITEMNAME,name);
            values.put(DbHelper.KEY_ITEMDESC,desc );
            values.put(DbHelper.KEY_ITEMQUANTITY,quantity );

            System.out.println("");
            if(isUpdate)
            {    
                //update database with new data 
                dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
            }
            else
            {
                //insert data into database
                dataBase.insert(DbHelper.TABLE_NAME, null, values);
            }
            //close database
            dataBase.close();
            finish();


        }

    }

这是我的DbHelper.java

package com.example.aoutsu;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * sqlite database helper to create table into SQLite database
 * @author ketan(Visit my <a
 *         href="http://androidsolution4u.blogspot.in/">blog</a>)
 */
public class DbHelper extends SQLiteOpenHelper {
    static String DATABASE_NAME="example_db";
    public static final String TABLE_NAME="inventory_db";
    public static final String KEY_ITEMNAME="itemname";
    public static final String KEY_ITEMDESC="itemdesc";
    public static final String KEY_ITEMQUANTITY="itemquantity";
    public static final String KEY_ID="id";
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_ITEMNAME+" TEXT, "+KEY_ITEMDESC+" TEXT, "+KEY_ITEMQUANTITY+ "TEXT)";
        db.execSQL(CREATE_TABLE);

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);

    }

}

这是我的DisplayAdapter.java

package com.example.aoutsu;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class DisplayAdapter extends BaseAdapter{
      private Context mContext;
        private ArrayList<String> id;
        private ArrayList<String> item;
        private ArrayList<String> description;
        private ArrayList<String> quantity;


        public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> itemname, ArrayList<String> itemdesc, ArrayList<String> itemquantity ) {
            this.mContext = c;
            this.id = id;
            this.item = itemname;
            this.description = itemdesc;
            this.quantity = itemquantity;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return id.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public View getView(int pos, View child, ViewGroup parent) {
            Holder mHolder;
            LayoutInflater layoutInflater;
            if (child == null) {
                layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                child = layoutInflater.inflate(R.layout.listcell, null);
                mHolder = new Holder();
                mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
                mHolder.txt_N = (TextView) child.findViewById(R.id.txt_name);
                mHolder.txt_D = (TextView) child.findViewById(R.id.txt_desc);
                mHolder.txt_Q = (TextView) child.findViewById(R.id.txt_quantity);
                child.setTag(mHolder);
            } else {
                mHolder = (Holder) child.getTag();
            }
            mHolder.txt_id.setText(id.get(pos));
            mHolder.txt_N.setText(item.get(pos));
            mHolder.txt_D.setText(description.get(pos));
            mHolder.txt_Q.setText(quantity.get(pos));

            return child;
        }

        public class Holder {
            TextView txt_id;
            TextView txt_N;
            TextView txt_D;
            TextView txt_Q;
        }

    }

2 个答案:

答案 0 :(得分:2)

问题是Android无法找到名为com.example.aoutsu.AddNewItem的方法,因此至少有一种方法适用:

1)未定义班级com.example.aoutsu

2)您错误地调用了package并且应该是上面的

3)您尚未在AndroidManifest.xml文件中定义此活动。

顺便说一句,要调试这些异常,请始终尝试查找引用任何文件的行。在这种情况下:

01-19 17:36:09.206: E/AndroidRuntime(487): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.aoutsu/com.example.aoutsu.AddNewItem}; have you declared this activity in your AndroidManifest.xml?

然后简单地将该文本放入google ant中,尝试在发生之前找到可能有数千名用户的内容。

答案 1 :(得分:0)

只需在清单文件中声明您的激活 AddNewItem -

<activity android:name=".AddNewItem" ></activity>