0

activity_main ページにユーザー データベースを作成して、ユーザーがアプリにサインインしたときに次の主要なページに移動できるようにしようとしています。現在の ActivityMain.java ファイルは次のとおりです。

         package com.example.awesomefilebuilder;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
    public class MainActivity extends Base_Activity {

    public final static String EXTRA_MESSAGE = "com.example.awesomefilebuilder.MESSAGE";
    public final String TAG = "MainActivity";
    public final String edit_message ="Username";
    public static final String DATABASE_NAME = "data";
    public static final String TABLE_NAME = "comments_table";
    public static final String C_ID = "_id";
    public static final String NAME = "name";
    public static final String COMMENT = "comment";
    public static final String EMAIL = "email";
    public static final String TIME = "time";
    public static final String PASSWORD = "password";
    public static final int VERSION = 1;

    View view;
    SQLiteDatabase db;
    DbHelper dbhelper;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        DbHelper dbhelper = new DbHelper(getActivity());
        db = dbhelper.getWritableDatabase();
        view = inflater.inflate(R. layout.activity_main, container,false);


            ContentValues cv = new ContentValues();


        cv.put(DbHelper.NAME, NAME);
        cv.put(DbHelper.COMMENT, COMMENT);
        cv.put(DbHelper.EMAIL, EMAIL);
        cv.put(DbHelper.PASSWORD, PASSWORD);
        db.insert(DbHelper.TABLE_NAME, null, cv);


    Button query = (Button) view.findViewById(R.id.query);
    query.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            String [] columns = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.EMAIL};

            Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
            cursor.moveToFirst();

                while(cursor.moveToNext())
                {
                    String name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME));
                    String comment = cursor.getString(cursor.getColumnIndex(DbHelper.COMMENT));
                    String password = cursor.getString(cursor.getColumnIndex(DbHelper.PASSWORD));
                    String email = cursor.getString(cursor.getColumnIndex(DbHelper.EMAIL));

                    Toast.makeText(getActivity(), "Name = "+ name +"/nComment= "+ comment,Toast.LENGTH_SHORT).show();
                }

                cursor.close();




        }

    });
        return view;


    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate" );

}

@Override
public void onDestroy() {
    super.onDestroy();  // Always call the superclass

    // Stop method tracing that the activity started during onCreate()
    android.os.Debug.stopMethodTracing();
}


/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, HomePageMain.class);
    EditText editText = (EditText) findViewById(R.id.createpicture);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
    Log.i(TAG, "sendMessage" );
    }

}

サイトを読んで、 getActivity() オプションはフラグメントでのみ使用する必要があることを理解しましたが、単純なユーザーログイン画面にはフラグメントを使用したくありません。getActivity() オプションを置き換えるか、一般的にエラーを修正するにはどうすればよいですか? また、アプリケーションでデータベースを作成するだけですぐに使用できるデータベースを持っていても、別のデータベースに接続する必要はありません。これを解決しやすくするコーディングの余分なページを誰かが見る必要がある場合は、私に知らせてください. 前もって感謝します!:)

4

2 に答える 2

1

これでうまくいくはずです。getApplication()、getApplicationContext()、getBaseContext() および someClass.thisに関する非常に良い答えがあります

DbHelper dbhelper = new DbHelper(getApplicationContext());
于 2013-08-24T02:37:21.917 に答える
0

このメソッドは Fragment クラスであるため、Activity クラスで getActivity() を使用することはできません。あなたの場合、次のようなことができます:

DbHelper dbhelper = new DbHelper(this);

また

DbHelper dbhelper = new DbHelper(MainActivity.this);

また

DbHelper dbhelper = new DbHelper(getApplicationContext());

注: onCreateView() は Activity クラスのメソッドではなく、Fragment クラスのメソッドであるため、ここに貼り付けたコードは構文的に間違っています。onCreateView() メソッドで記述したコードを onCreate() メソッドで記述する必要があります。strong text

于 2013-08-24T04:23:23.220 に答える