我正在尝试在我的android项目上创建一个简单的SQLite来完成一些工作。
MyDBHandler中MyDBHandler()的(this,null, null,null 1)
行无法应用于此。
我有一种感觉,因为这是一个片段,而不是通常的活动。有什么想法吗?
package com.test.test.sql;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class MemoFragment extends Fragment {
EditText Input;
TextView LyricText;
MyDBHandler dbHandler;
Button addButtonClicked;
Button deleteButtonClicked;
public MemoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_memo, container, false);
Input = (EditText) getView().findViewById(R.id.Input);
LyricText = (TextView) getView().findViewById (R.id.LyricText);
dbHandler = new MyDBHandler(this, null, null, 1);
printDatabase ();
}
//add lyric to database
public void addButtonClicked(View view){
Lyrics lyrics = new Lyrics(Input.getText().toString());
dbHandler.addLyric(lyrics);
printDatabase();
}
//delete items
public void deleteButtonClicked(View view){
String inputText = Input.getText().toString());
dbHandler.deleteLyrics(inputText);
printDatabase();
}
public void printDatabase(){
String dbString = dbHandler.databasetoString();
LyricText.setText(dbString);
Input.setText("");
}
}
答案 0 :(得分:1)
你的onCreateView错了。当您将返回放置在该位置时,以下行不会被执行。因此,首先将膨胀的布局放在View变量上。然后使用它来实例化edittext和textview。请参阅下面的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_memo, container, false);
Input = (EditText) v.findViewById(R.id.Input);
LyricText = (TextView) v.findViewById (R.id.LyricText);
dbHandler = new MyDBHandler(this, null, null, 1);
printDatabase ();
return v;
}
答案 1 :(得分:1)
这个怎么样:
dbHandler = new MyDBHandler(getActivity(),null,null,1);