嗨,我是一个全新的编程,我不知道为什么我的代码无法在片段中工作,我关注了newboston视频并进行了一些更改但它仍然无法正常工作,当我尝试保存时它会崩溃里面的价值。我添加了一个rootview,但除此之外,我认为我什么都没做,有人介意帮助一个可怜的灵魂出去吗?
package com.example.onTV;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class HistoryFragment extends Fragment {
EditText name;
TextView sqlDisplay;
MyDBHandler dbHandler;
public HistoryFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_history, container, false);
name = (EditText) rootView.findViewById(R.id.name);
sqlDisplay = (TextView) rootView.findViewById(R.id.sqlDisplay);
dbHandler = new MyDBHandler(this.getActivity(), null, null, 1);
printDatabase();
return rootView;
}
//Add a product to the database
public void saveOnClicked(View view) {
favourites favourites = new favourites(name.getText().toString());
dbHandler.addFavourites(favourites);
printDatabase();
}
//Delete items
public void deleteOnClicked(View view) {
String inputText = name.getText().toString();
dbHandler.deleteFavourites(inputText);
printDatabase();
}
//Print the database
public void printDatabase() {
String dbString = dbHandler.databaseToString();
sqlDisplay.setText(dbString);
name.setText("");
}
}
dbhandler类
package com.example.onTV;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHandler extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME ="favourites.db";
public static final String TABLE_FAVOURITES = "favourites";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_VIDEONAME = "videoname";
// public static final String COLUMN_LINK = "link";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_FAVOURITES + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_VIDEONAME + " TEXT " +/* COLUMN_LINK + " TEXT " +*/
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVOURITES);
onCreate(db);
}
public void addFavourites(favourites favourites){
ContentValues values = new ContentValues();
values.put(COLUMN_VIDEONAME, favourites.get_videoname());
// values.put(COLUMN_LINK, favourites.get_link());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_FAVOURITES, null, values);
db.close();
}
public void deleteFavourites(String videoName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_FAVOURITES + " WHERE " + COLUMN_VIDEONAME + "=\"" + videoName + "\" ;"
// + COLUMN_LINK + "=\"" + link + "\" ;"
);
}
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_FAVOURITES + " WHERE 1";
//Cursor points to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results
c.moveToFirst();
//Position after the last row means the end of the results
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex("videoname")) != null) {
dbString += c.getString(c.getColumnIndex("videoname"));
dbString += "\n";
}
c.moveToNext();
}
db.close();
return dbString;
}
}
日志出来
07-29 18:08:44.554 8051-8051/com.example.navigationdrawerexample D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
07-29 18:08:44.624 8051-8051/com.example.navigationdrawerexample D/AndroidRuntime﹕ Shutting down VM
07-29 18:08:44.624 8051-8051/com.example.navigationdrawerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.navigationdrawerexample, PID: 8051
java.lang.IllegalStateException: Could not find a method saveOnClicked(View) in the activity class com.example.onTV.MainActivity for onClick handler on view class android.widget.Button with id 'saveButton'
at android.view.View$1.onClick(View.java:4234)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NoSuchMethodException: saveOnClicked [class android.view.View]
at java.lang.Class.getMethod(Class.java:665)
at android.view.View$1.onClick(View.java:4227)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
07-29 18:08:49.164 8051-8051/com.example.navigationdrawerexample I/Process﹕ Sending signal. PID: 8051 SIG: 9
答案 0 :(得分:0)
在布局xml中,您定义了android:onClick
个属性,其值为saveOnClicked
,deleteOnClicked
。 Android将在您的活动类中查找具有相同名称的方法。可悲的是,它不适用于碎片。
使用片段时,最好以编程方式设置OnClickListener
。删除布局中的android:onClick
属性,并在片段中实现类似的内容:
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
rootView.findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveOnClicked(v);
}
});
rootView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteOnClicked(v);
}
});
}