-3

バーチャル キャンパス ツアー用のアプリのコードを書きました。さまざまなファイル (3 つのクラス ファイル) と 3 つの xml ファイルの下で共有しましたが、すべてにエラーはありません。

アプリはエミュレーターに正常にインストールされましたが、見出しに記載されているエラーがスローされます。

私はAndroidとJavaが初めてなので、どんな助けも本当に感謝しています!

誰かが私が間違っている場所を見つけることができますか?

BuildingEdit という名前の 1 つのクラスのコード

package com.example.udbuildingtour;

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

public class BuildingEdit extends Activity {
    private EditText mNameText;
    private EditText mLatText;
    private EditText mLongiText;
    private Long mRowId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.building_edit);
        setTitle(R.string.edit_building);

        mNameText = (EditText) findViewById(R.id.name);
        mLatText = (EditText) findViewById(R.id.lat);
        mLongiText = (EditText) findViewById(R.id.longi);

        Button confirmButton = (Button) findViewById(R.id.confirm);

        mRowId = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String name = extras.getString(BuildingsDbAdapter.KEY_BUILDINGNAME);
            String lat = extras.getString(BuildingsDbAdapter.KEY_LAT);
            String longi = extras.getString(BuildingsDbAdapter.KEY_LONGI);
            mRowId = extras.getLong(BuildingsDbAdapter.KEY_ID);

            if (name != null) {
                mNameText.setText(name);
            }
            if (lat != null) {
                mLatText.setText(lat);
            }
            if (longi != null) {
                mLongiText.setText(longi);
            }
        }

        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Bundle bundle = new Bundle();

                bundle.putString(BuildingsDbAdapter.KEY_BUILDINGNAME, mNameText.getText().toString());
                bundle.putString(BuildingsDbAdapter.KEY_LAT, mLatText.getText().toString());
                bundle.putString(BuildingsDbAdapter.KEY_LONGI, mLongiText.getText().toString());
                if (mRowId != null) {
                    bundle.putLong(BuildingsDbAdapter.KEY_ID, mRowId);
                }

                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }

        });
    }


    }

クラス UDBuildingTour のコード

package com.example.udbuildingtour;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

public class UDBuildingTour extends ListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;

    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private BuildingsDbAdapter mDbHelper;
    private Cursor mBuildingsCursor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.building_edit);


        mDbHelper = new BuildingsDbAdapter(this);
        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case INSERT_ID:
                createBuilding();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }



    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteBuilding(info.id);
            fillData();
            return true;
        }
        return super.onContextItemSelected(item);
    }

     private void createBuilding() {
            Intent i = new Intent(this, BuildingEdit.class);
            startActivityForResult(i, ACTIVITY_CREATE);
        }

     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Cursor c = mBuildingsCursor;
            c.moveToPosition(position);
            Intent i = new Intent(this, BuildingEdit.class);
            i.putExtra(BuildingsDbAdapter.KEY_ID, id);
            i.putExtra(BuildingsDbAdapter.KEY_BUILDINGNAME, c.getString(
                    c.getColumnIndexOrThrow(BuildingsDbAdapter.KEY_BUILDINGNAME)));
            i.putExtra(BuildingsDbAdapter.KEY_LAT, c.getString(
                    c.getColumnIndexOrThrow(BuildingsDbAdapter.KEY_LAT)));
            i.putExtra(BuildingsDbAdapter.KEY_LONGI, c.getString(
                    c.getColumnIndexOrThrow(BuildingsDbAdapter.KEY_LONGI)));
            startActivityForResult(i, ACTIVITY_EDIT);
        }

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            Bundle extras = intent.getExtras();
            switch(requestCode) {
                case ACTIVITY_CREATE:
                    Long rowId = extras.getLong(BuildingsDbAdapter.KEY_ID);
                    String name = extras.getString(BuildingsDbAdapter.KEY_BUILDINGNAME);
                    String lat = extras.getString(BuildingsDbAdapter.KEY_LAT);
                    String longi = extras.getString(BuildingsDbAdapter.KEY_LONGI);
                    mDbHelper.createBuilding(rowId, name, lat, longi);
                    fillData();
                    break;
                case ACTIVITY_EDIT:
                    Long Id = extras.getLong(BuildingsDbAdapter.KEY_ID);
                    if (Id != null) {
                        String editName = extras.getString(BuildingsDbAdapter.KEY_BUILDINGNAME);
                        String editLat = extras.getString(BuildingsDbAdapter.KEY_LAT);
                        String editLongi = extras.getString(BuildingsDbAdapter.KEY_LONGI);
                        mDbHelper.updateBuilding(Id, editName, editLat, editLongi);
                    }
                    fillData();
                    break;
            }
     }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.udbuilding_tour, menu);
        return true;
    }

    private void fillData() {
        // TODO Auto-generated method stub
        // Get all of the rows from the database and create the item list
        mBuildingsCursor = mDbHelper.fetchAllBuildings();
        startManagingCursor(mBuildingsCursor);

        // Create an array to specify the fields we want to display in the list (only BUILDINGNAME)
        String[] from = new String[]{BuildingsDbAdapter.KEY_BUILDINGNAME};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter building = 
            new SimpleCursorAdapter(this, R.layout.buildings_row, mBuildingsCursor, from, to);
        setListAdapter(building);


    }


}

BuildingsDBAdapter のコード

package com.example.udbuildingtour;

import java.io.BufferedReader;
import java.io.FileReader;

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

public class BuildingsDbAdapter {

    public static final String KEY_ID = "_id";
    public static final String KEY_BUILDINGNAME = "name";
    public static final String KEY_LAT = "lat";
    public static final String KEY_LONGI = "longi";
    private static final String TAG = "BuildingsDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
        "create table buildings (_id integer primary key autoincrement, "
        + "title text not null, body text not null);";

    private static final String DATABASE_NAME = "Buildings";
    private static final String DATABASE_TABLE = "UDBuildings";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public BuildingsDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }



    public BuildingsDbAdapter open() throws SQLException  {
        // TODO Auto-generated method stub
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public boolean deleteBuilding(long id) {
        // TODO Auto-generated method stub
         return mDb.delete(DATABASE_TABLE, KEY_ID + "=" + id, null) > 0;
    }

    public long createBuilding(Long id, String name, String lat, String longi) {
        // TODO Auto-generated method stub
        ContentValues initialValues = new ContentValues();

        try{
            BufferedReader in = new BufferedReader(new FileReader("UDBuildings.txt"));
            String s;

            while((s = in.readLine()) != null){

                String[] var = s.split(":");
                     //var[0]=ID,var[1]=Name etc etc
                initialValues.put(KEY_BUILDINGNAME, var[1]);
                initialValues.put(KEY_LAT, var[2]);
                initialValues.put(KEY_LONGI, var[3]);
                initialValues.put(KEY_ID, var[0]);
            }

        }catch(Exception e){
            e.printStackTrace();
        }



        return mDb.insert(DATABASE_TABLE, null, initialValues);

    }

     /**
     * Return a Cursor over the list of all buildings in the database
     * 
     * @return Cursor over all buildings
     */
    public Cursor fetchAllBuildings() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_BUILDINGNAME,
                KEY_LAT, KEY_LONGI}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the building that matches the given building id
     * 
     * @param id of building to retrieve
     * @return Cursor positioned to matching building, if found
     * @throws SQLException if building could not be found/retrieved
     */
    public Cursor fetchBuilding(long id) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ID,
                    KEY_BUILDINGNAME, KEY_LAT, KEY_LONGI}, KEY_ID + "=" + id, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * Update the building using the details provided. The building to be updated is
     * specified using the id, and it is altered to use the name and lat, longi
     * values passed in
     * 
     * @param id of building to update
     * @param name value to set building name to
     * @param lat value to set building lat to
     * @param longi value to set building longi to
     * @return true if the building was successfully updated, false otherwise
     */


    public boolean updateBuilding(Long rowId, String editName, String editLat, String editLongi) {
        // TODO Auto-generated method stub
        ContentValues args = new ContentValues();
        args.put(KEY_BUILDINGNAME, editName);
        args.put(KEY_LAT, editLat);
        args.put(KEY_LONGI, editLongi);

        return mDb.update(DATABASE_TABLE, args, KEY_ID + "=" + rowId, null) > 0;

    }

}

building_edit の xml コード

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="name" />

        <EditText android:id="@+id/name" 
          android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1"/>
    </LinearLayout>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="lat" />
    <EditText android:id="@+id/lat" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical" />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="longi" />
    <EditText android:id="@+id/longi" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical" />

    <Button android:id="@+id/confirm" 
      android:text="confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

building_row の xml コード

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

building_list の xml コード

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <ListView android:id="@+id/android:list"
          android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <TextView android:id="@+id/android:empty"
          android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="no_buildings"/>
</LinearLayout>

androidmanifest.xml ファイル

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.udbuildingtour.UDBuildingTour"
            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=".BuildingEdit"></activity>
    </application>

</manifest>
4

2 に答える 2

1
 android:id="@android:id/list"

これではなく...

 android:id="@+id/android:list"

あなたのListViewで

于 2013-10-28T15:27:45.987 に答える