-2

当我单击应该打开另一个屏幕的按钮时,我收到错误“应用程序已意外停止。请重试”。下面是java文件以及xml文件。java文件有一个按钮的代码,点击它我得到了错误。我尝试在清单中同时使用“NewNote”和“NEWNOTE”。在这两种情况下都会出错。

        package com.compilationofnotes.pragya;



        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;

        public class NotesView extends Activity {
            /** Called when the activity is first created. */

            Button bt;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                bt=(Button)findViewById(R.id.buttonaddnote);
                bt.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        Intent addnote = new Intent("com.compilationofnotes.pragya.NewNote");
                        startActivity(addnote);
                        }

                });
            }
        }




package com.compilationofnotes.pragya;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class SqlDatabase {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "notes_name";
    public static final String KEY_CONTENT = "notes_content";
    public static final String KEY_TIME = "notes_time";

    private static final String DATABASE_NAME = "Database Of Notes";
    private static final String DATABASE_TABLE = "List of Notes";
    private static final int DATABASE_VERSION = 1;


    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    public static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
                    KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_NAME + "TEXT NOT NULL " + 
                    KEY_CONTENT + "TEXT NOT NULL " +
                    KEY_TIME + "TEXT NOT NULL"
                    );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
            onCreate(db);
        }

    }

    public SqlDatabase(Context c){
        ourContext = c;
    }

    public SqlDatabase open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
    }
    public SqlDatabase close(){
    ourHelper.close();
    return this;
    }

    public long createEntry(String notename, String notecontent, String notetime) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, notename);
        cv.put(KEY_CONTENT, notecontent);
        cv.put(KEY_TIME, notetime);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

}


package com.compilationofnotes.pragya;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class NewNote extends Activity implements OnClickListener{
    EditText et;
    Button btsave, btcancel;
    long tp;
    int counter=1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et=(EditText)findViewById(R.id.editt);
        btsave=(Button)findViewById(R.id.buttonsave);
        btcancel=(Button)findViewById(R.id.buttoncancel);
        btsave.setOnClickListener(this);
        btsave.setOnClickListener(this);
    }
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
    switch(arg0.getId()){

         case R.id.buttonsave:

               try{
               String notename = "Note" + counter;
               String notecontent = et.getText().toString();
               tp=System.currentTimeMillis();
               String notetime = Long.toString(tp);
               SqlDatabase entry = new SqlDatabase(NewNote.this);
               entry.open();
               entry.createEntry(notename, notecontent, notetime);
               entry.close();
               Intent i = new Intent(NewNote.this, NotesView.class);
               startActivity(i);
               counter++ ;
               }
               catch(Exception e){
                   e.printStackTrace();
               }
              break;

          case R.id.buttoncancel:
              Intent i = new Intent(NewNote.this, NotesView.class);
               startActivity(i);
              break;
    }
    }

}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.compilationofnotes.pragya"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk android:minSdkVersion="8" />

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".NotesView"
                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:name="com.compilationofnotes.pragya.NewNote"
                android:label="@string/app_name" />

           </application>

我在logcat中收到此错误:

12-16 20:37:19.099: E/AndroidRuntime(276): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.compilationofnotes.pragya.

请请请帮助。提前致谢!

4

2 回答 2

0

尝试这个

Intent addnote = new Intent(NotesView.this,NewNote.class);
startActivity(addnote);

您正在使用带有操作而不是组件的构造函数。

于 2012-12-16T15:33:03.307 回答
0

这可能对您有所帮助

AndroidManifest.xml--

<activity android:name=".NewNote" android:label="@string/app_name" />

在 java 文件中,在按钮单击侦听器上写这个。

Intent addnote = new Intent(NotesView.this,NewNote.class);
startActivity(addnote);

并删除此行import com.compilationofnotes.pragya.R;

于 2012-12-16T15:35:07.603 回答