http://www.devdiv.com/ContentProvider-weblog-65729-11284.html
我照着这个例子打的,在同一个程序中好使,但是我另建了一个应用程序,当我点击按钮时会跳到原来那个应用程序,怎么办

解决方案 »

  1.   

    请问以下代码有什么错,为什么一执行就跳到另外一个程序中了呢
    package com.example.hwtest1;import com.example.hwtest1.DatabaseHelper;
    import com.example.hwtest1.R;
    import com.example.hwtest1.ProviderConst;import android.os.Bundle;
    import android.app.Activity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;public class MainActivity extends Activity {
     private SQLiteDatabase mdb = null;
     private DatabaseHelper helper = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //initDatabase();
            setContentView(R.layout.activity_main);
        }
        private void initDatabase()
        {
         mdb = this.openOrCreateDatabase("dbfile", 0, null);
         String sql_create = "create table test (id int, name TEXT)";
         mdb.execSQL(sql_create);
         helper = new DatabaseHelper(this,"dbfile");
         //mdb = this.openOrCreateDatabase("dbfile", 0, null);
         //String sql_create = "create table test (id int, name TEXT)";
         //mdb.execSQL(sql_create);
         String sql_insert = "insert into test(id, name) values(56, 'namexx')";
         //mdb.execSQL(sql_insert);
         helper.insert(sql_insert);
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
          super.onCreateOptionsMenu(menu); 
         menu.add(1, Menu.FIRST +1, 1, "GetName");  
           menu.add(1, Menu.FIRST +2, 2, "Test");
         //getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
        
        private String getName1()
        {
         String name = null;      
         Cursor cur = this.getContentResolver().query(ProviderConst.MY_TEST_URI,new String[]{"id","name"},null, null, null);
         //Cursor cur = managedQuery(ProviderConst.MY_TEST_URI,new String[]{"id","name"},null,null,null);
         if (cur == null)
          return null;
         cur.moveToFirst();
         do
         {
          name = name  + cur.getString(1)+ "\n";      
          
         } while(cur.moveToNext());      
         
         return name;
        }
         
        public boolean onOptionsItemSelected(MenuItem item) {
           // TODO Auto-generated method stub
           super.onOptionsItemSelected(item);
           switch (item.getItemId())
           {
            case Menu.FIRST+ 1:
            {
             TextView tv = (TextView)this.findViewById(R.id.textView1);
             tv.setText(getName1());
             break;
            }
            case Menu.FIRST + 2:
            {
             this.setTitle("Del Item...");
             break;
            }   
           }
           return true;
          }
        
    }