konsei 2016-10-21 01:58 采纳率: 72.2%
浏览 1166
已采纳

【安卓】进入显示所有便签的界面就出错,是怎么回事啊

我在主页面创建的新建便签和显示全部便签的两个按钮,
点击新建便签的按钮就进入编辑新便签的界面,
点击全部便签进入一个显示所有便签的界面,但就在点击全部便签时出错了(全部便签里是一个列表视图,在里面点击一条便签进入编辑)
这是全部便签的代码

 public class All_note extends ListActivity {
    public final static String EXTRA_NOTE_ID = "com.example.android.notepad.NOTEID";

    private NoteDbHelper _db = null;

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.all_note);

            getListView().setOnCreateContextMenuListener(this);
            _db = new NoteDbHelper(this);

            String[] dataColumns = new String[] {"title", "content"};
            int[] viewIds = new int[] { R.id.title, R.id.summary };

            Cursor cursor = _db.getAllNotesCursor();
             SimpleCursorAdapter adapter
                = new SimpleCursorAdapter(
                          this,                             // The Context for the ListView
                          R.layout.notelist_item,           // Points to the XML for a list item
                          cursor,                           // The cursor to get items from
                          dataColumns,
                          viewIds
                  );    
            setListAdapter(adapter);
     }
     private void reloadNote() {
            SimpleCursorAdapter adapter = (SimpleCursorAdapter)getListAdapter();
            if (adapter != null) {
                Cursor cursor = _db.getAllNotesCursor();
                adapter.swapCursor(cursor);
            }
        }

        @Override
        public void onResume() {
            super.onResume();
            reloadNote();
        } 
        protected void onListItemClick(ListView l, View v, int position, long id) {
            Intent intent = new Intent(this, NewNote.class);
            intent.putExtra(EXTRA_NOTE_ID, (int)id);
            startActivity(intent);
        }
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
            // Inflate menu from XML resource
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.list_context_menu, menu);
        }
        public boolean onContextItemSelected(MenuItem item) {
            // The data from the menu item.
            AdapterView.AdapterContextMenuInfo info;
                try {
                    // Casts the data object in the item into the type for AdapterView objects.
                    info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                } catch (ClassCastException e) {

                    // If the object can't be cast, logs an error
                    Log.e("SimpleNotePad", "bad menuInfo", e);

                    // Triggers default processing of the menu item.
                    return false;
                }

                /*
                 * Gets the menu item's ID and compares it to known actions.
                 */
                switch (item.getItemId()) {
                case R.id.context_delete:

                    int noteId = (int)info.id;
                    if (noteId > 0)
                        _db.deleteNote(noteId);
                    reloadNote();
                    // Returns to the caller and skips further processing.
                    return true;
                default:
                    return super.onContextItemSelected(item);
                }
            }

}

主页面代码

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView img1=(ImageView)findViewById(R.id.setButton0);
        img1.setOnClickListener(new ImageView.OnClickListener(){
            public void onClick(View V){
                Intent intent=new Intent(MainActivity.this,SetActivity.class);
                startActivity(intent);
            }
        });
        ImageView img2=(ImageView)findViewById(R.id.newButton2);
        img2.setOnClickListener(new ImageView.OnClickListener(){
            public void onClick(View V){
                Intent intent=new Intent(MainActivity.this,NewNote.class);
                startActivity(intent);
            }
        });
        ImageView img3=(ImageView)findViewById(R.id.allButton1);
        img3.setOnClickListener(new ImageView.OnClickListener(){
            public void onClick(View V){
                Intent intent=new Intent(MainActivity.this,All_note.class);
                startActivity(intent);
            }
        });
    }   
}

配置文件

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name=".Welcome"
            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="cn.itcast.one.GuideActivity"></activity>
        <activity 
            android:name="cn.itcast.one.MainActivity"
            android:label="@string/app_name"></activity>
        <activity android:name="cn.itcast.set.SetActivity"></activity> 
        <activity android:name="cn.itcast.one.NewNote"></activity>
        <activity android:name="cn.itcast.one.All_note"></activity>
       </application>

这是Logcat
图片说明
图片说明

  • 写回答

1条回答 默认 最新

  • C盘无限大 2016-10-21 02:07
    关注

    报错原因是你继承了listActivity,xml文件的listview要设置id为androi.id.list,你看看listActivity的源码在源码有下面这个方法

         @Override
        public void onContentChanged() {
            super.onContentChanged();
            View emptyView = findViewById(com.android.internal.R.id.empty);
            mList = (ListView)findViewById(com.android.internal.R.id.list);
            if (mList == null) {
                throw new RuntimeException(
                        "Your content must have a ListView whose id attribute is " +
                        "'android.R.id.list'");
            }
            if (emptyView != null) {
                mList.setEmptyView(emptyView);
            }
            mList.setOnItemClickListener(mOnClickListener);
            if (mFinishedStart) {
                setListAdapter(mAdapter);
            }
            mHandler.post(mRequestFocus);
            mFinishedStart = true;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题