簡體   English   中英

對於用戶數據庫,在activity_main中使用的類型未定義方法getactivity()

[英]the method getactivity() is undefined for the type Use In activity_main for User Database

我試圖在我的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()選項,或者一般來說可以解決該錯誤? 而且,即使我可以在我的應用程序中創建數據庫時也可以使用一個獨立的數據庫,我也不必連接到單獨的數據庫。 如果有人需要查看任何額外的編碼頁面,以便於解決,請告訴我。 提前致謝! :)

這應該可以解決問題。 這是有關getApplication(),getApplicationContext(),getBaseContext()和someClass的很好的答案

DbHelper dbhelper = new DbHelper(getApplicationContext());

您不能在Activity類中使用getActivity(),因為此方法屬於Fragment類。 在您的情況下,您可以執行以下操作:

DbHelper dbhelper = new DbHelper(this);

要么

DbHelper dbhelper = new DbHelper(MainActivity.this);

要么

DbHelper dbhelper = new DbHelper(getApplicationContext());

注意: 您在此處粘貼的代碼在語法上是錯誤的,因為onCreateView()不是Activity類的方法,它是Fragment類的方法。 您應該使用onCreateView()方法編寫的代碼編寫代碼。 strong text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM