Java 对于用户数据库的activity_main中使用的类型,未定义getactivity()方法
我正在尝试在我的activity_主页上创建一个用户数据库,这样当用户登录应用程序时,它将引导他们进入下一个主页面。以下是当前的ActivityMain.java文件:Java 对于用户数据库的activity_main中使用的类型,未定义getactivity()方法,java,android,Java,Android,我正在尝试在我的activity_主页上创建一个用户数据库,这样当用户登录应用程序时,它将引导他们进入下一个主页面。以下是当前的ActivityMain.java文件: package com.example.awesomefilebuilder; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.
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()选项,或者只是一般地修复错误?此外,我不想连接到一个单独的数据库,即使我有一个随时可用的数据库,当我可以在我的应用程序中创建数据库时,我已经这样做了。如果有人需要看到任何额外的编码页,这将使这更容易解决,请让我知道。
提前感谢!:) 这应该能奏效。这里有一个很好的答案
无法在活动类中使用getActivity(),因为此方法属于片段类。在您的情况下,您可以执行如下操作:
DbHelper dbhelper = new DbHelper(this);
或
或
注意:粘贴在此处的代码在语法上是错误的,因为onCreateView()不是Activity类的方法,而是Fragment类的方法。您应该使用onCreateView()方法编写代码。
strong text
您可以尝试使用此方法吗?
DbHelper dbhelper = new DbHelper(this);
DbHelper dbhelper = new DbHelper(MainActivity.this);
DbHelper dbhelper = new DbHelper(getApplicationContext());