YXTS122
2017-01-21 01:19
采纳率: 100%
浏览 2.2k

安卓,一打开app就出现异常,崩溃了

图片说明

ActivityDiaryEdit.java

 package com.exae.shiyongsqlite;

import java.util.Calendar;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
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 ActivityDiaryEdit extends Activity implements OnClickListener {
    private Button bt;
    private EditText mTitleText,mBodyText;
    DatabaseHelper mDbHelper;
    private long mRowId;
    private SQLiteDatabase mDb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editdiary);
        mDbHelper=new DatabaseHelper(this);
        mDb=mDbHelper.getWritableDatabase();
        bt=(Button)findViewById(R.id.position);
        bt.setOnClickListener(this);
        mTitleText=(EditText)findViewById(R.id.et1);
        mBodyText=(EditText)findViewById(R.id.et2);
        Bundle extras=getIntent().getExtras();
        if(extras!=null)
        {
            String title=extras.getString("title");
            String body=extras.getString("body");
            mRowId=extras.getLong("_id");
            if(title!=null)
            {
                mTitleText.setText(title);
            }
            if(body!=null)
            {
                mBodyText.setText(body);
            }
        }
    }

    public long createDiary(String title,String body)
    {
        ContentValues initialValues=new ContentValues();
        initialValues.put("title", title);
        initialValues.put("body", body);
        Calendar calendar=Calendar.getInstance();
        String created=calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日"+calendar.get(Calendar.HOUR_OF_DAY)+"小时"+calendar.get(Calendar.MINUTE)+"分钟";
        initialValues.put("created", created);
        return mDb.insert("diary",null,initialValues);
    }

    public boolean updateDiary(long rowId,String title,String body)
    {
        ContentValues args=new ContentValues();
        args.put("title", title);
        args.put("body", body);
        Calendar calendar=Calendar.getInstance();
        String created=calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日"+calendar.get(Calendar.HOUR_OF_DAY)+"小时"+calendar.get(Calendar.MINUTE)+"分钟";
        args.put("created", created);
        return mDb.update("diary",args,"_id"+"="+rowId,null)>0;
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.position:
            String title=mTitleText.getText().toString();
            String body=mBodyText.getText().toString();
            if(mRowId!=0)
            {
                updateDiary(mRowId,title,body);
            }
            else
                createDiary(title,body);
            Intent mIntent=new Intent();
            setResult(RESULT_OK,mIntent);
            finish();
        }
    }



}

DatabaseHelper.java

 package com.exae.shiyongsqlite;

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

public class DatabaseHelper extends SQLiteOpenHelper{
    public DatabaseHelper(Context context)
    {
        super(context,"database",null,2);
    }

    public void onCreate(SQLiteDatabase db)
    {
        String DATABASE_CREATE="create table diary(_id integer primary key autoincrement,title text,body text,created text);";
        db.execSQL(DATABASE_CREATE);
    }

    public void onUpgrade(SQLiteDatabase db,int arg1,int arg2)
    {
        String sql="DROP TABLE IF EXISTS diary";
        db.execSQL(sql);
        this.onCreate(db);
    }

}

MainActivity.java

 package com.exae.shiyongsqlite;

import java.util.Calendar;

import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity {
    DatabaseHelper mDbHelper;
    private Cursor mDiaryCursor;
    private SQLiteDatabase mDb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDbHelper=new DatabaseHelper(this);
        mDb=mDbHelper.getWritableDatabase();
        renderListView();
    }

    public long createDiary(String title,String body)
    {
        ContentValues initialValues=new ContentValues();
        initialValues.put("title", title);
        initialValues.put("body", body);
        Calendar calendar=Calendar.getInstance();
        String created=calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日"+calendar.get(Calendar.HOUR_OF_DAY)+"小时"+calendar.get(Calendar.MINUTE)+"分钟";
        initialValues.put("created", created);
        return mDb.insert("diary",null,initialValues);
    }

    public boolean deleteDiary(long rowId)
    {
        return mDb.delete("diary","_id"+"="+rowId,null)>0;
    }

    public Cursor getAllNotes()
    {
        return mDb.query("diary",new String[]{"_id","title","body","created"},null,null,null,null,null);
    }

    public boolean updateDiary(long rowId,String title,String body)
    {
        ContentValues args=new ContentValues();
        args.put("title", title);
        args.put("body", body);
        Calendar calendar=Calendar.getInstance();
        String created=calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日"+calendar.get(Calendar.HOUR_OF_DAY)+"小时"+calendar.get(Calendar.MINUTE)+"分钟";
        args.put("created", created);
        return mDb.update("diary",args,"_id"+"="+rowId,null)>0;
    }



    @Override
    public boolean onMenuItemSelected(int featureId,MenuItem item)
    {
        switch (item.getItemId())
        {
        case Menu.FIRST:
            createDiary();
            return true;
        case Menu.FIRST+1:
            deleteDiary(getListView().getSelectedItemId());
            renderListView();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    private void createDiary()
    {
        Intent i=new Intent(this,ActivityDiaryEdit.class);
        startActivityForResult(i,0);
    }

    private void renderListView()
    {
        mDiaryCursor=getAllNotes();
        startManagingCursor(mDiaryCursor);
        String[] from=new String[]{"title","created"};
        int[] to=new int[]{R.id.text1,R.id.created};
        SimpleCursorAdapter notes=new SimpleCursorAdapter(this,R.layout.diary_row,mDiaryCursor,from,to);
        setListAdapter(notes);
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);
        renderListView();
    }

    protected void onListItemClick(ListView l,View v,int position,long id)
    {
        super.onListItemClick(l,v,position,id);
        Cursor c=mDiaryCursor;
        c.moveToPosition(position);
        Intent i=new Intent(this,ActivityDiaryEdit.class);
        i.putExtra("_id", id);
        i.putExtra("title", c.getString(c.getColumnIndexOrThrow("title")));
        i.putExtra("body", c.getString(c.getColumnIndexOrThrow("body")));
        startActivityForResult(i,1);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu);
        menu.add(0, Menu.FIRST, 0, R.string.menu_insert);
        menu.add(0, Menu.FIRST+1, 0, R.string.menu_delete);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • stone_you 2017-01-21 01:30
    已采纳

    你的mainactivity是一个listactivity,但是你的mainactivity的布局问件里面没有listview,请加一个进去,id就是错误里面显示的那个

    已采纳该答案
    打赏 评论
  • 技术分享 2017-01-21 07:20

    listview id没找到

    打赏 评论
  • 蚂蚁冲锋队 2017-01-21 08:02

    这种问题一dC般查看
    Caused by 后面的提示,比如你这个工程的错误提示 your content must have a ListView whoes...
    就知道ListView出了问题,查看你的界面布局看看哪里有用到ListView,再结合后面的提示就知道问题在哪了
    当然:英文要懂一点 方法也要有一点

    打赏 评论
  • worthgod 2017-01-21 09:06

    引用的第三方库有问题

    打赏 评论
  • dabocaiqq 2017-01-21 15:35
    打赏 评论
  • YXTS122 2017-01-22 03:12

    布局文件内容在这里。。。。。。。。。。图片图片

    打赏 评论

相关推荐 更多相似问题