printDatabase()中的dbString获取空对象引用。我正在努力弄清楚为什么它是一个空对象,当我只是从我以前工作的应用程序移动此代码,但现在我收到此错误。 这是错误日志
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at binarysoup.mommy_info.ActivitiesAndFragments.KickCounter.printDatabase(KickCounter.java:178)
at binarysoup.mommy_info.ActivitiesAndFragments.KickCounter.onCreate(KickCounter.java:75)
KickCounter.java frgment正在发生。
public class KickCounter extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public KickCounter() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment KickCounter.
*/
// TODO: Rename and change types and number of parameters
public static KickCounter newInstance(String param1, String param2) {
KickCounter fragment = new KickCounter();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
//SQLite
MyDBHandler dbHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
//SQLite
dbHandler = new MyDBHandler(getContext(), null, null, 1);
printDatabase();
}
//SQLite
//Add a product to the database
public void addButtonClicked(View view){
Product product = new Product(kickString);
dbHandler.addProduct(product);
printDatabase();
}
Button kickButton;
Button removeKickButton;
Button startButton;
Chronometer chronometer;
TextView kickCount;
Integer kick = 0;
String kickString = kick.toString();
//SQLite
TextView outputText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_kick_counter, container, false);
kickButton = (Button)rootView.findViewById(R.id.kick_button_ID);
kickButton.setVisibility(View.INVISIBLE);
removeKickButton = (Button)rootView.findViewById(R.id.remove_kick_button_ID);
removeKickButton.setVisibility(View.INVISIBLE);
startButton = (Button)rootView.findViewById(R.id.start_button_ID);
chronometer = (Chronometer)rootView.findViewById(R.id.chronometer_ID);
kickCount = (TextView)rootView.findViewById(R.id.kickCount_ID);
//SQLite
outputText = (TextView)rootView.findViewById(R.id.outputText_ID);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startButton.setVisibility(View.GONE);
kickButton.setVisibility(View.VISIBLE);
removeKickButton.setVisibility(View.VISIBLE);
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if( chronometer.getText().toString().equalsIgnoreCase("00:05")) {
chronometer.stop();
kickButton.setVisibility(View.GONE);
removeKickButton.setVisibility(View.GONE);
Snackbar.make(getView(),"Your text",Snackbar.LENGTH_LONG).show();
}
if(kick == 10) {
chronometer.stop();
kickButton.setVisibility(View.GONE);
removeKickButton.setVisibility(View.GONE);
Snackbar.make(getView(),"heo",Snackbar.LENGTH_INDEFINITE).show();
}
}
});
}
});
kickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(kick < 10){
kick++;
setKickCountText();
}
else{
return;
}
}
});
removeKickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(kick != 0){
kick--;
setKickCountText();
}
else{
return;
}
}
});
// Inflate the layout for this fragment
return rootView;
}
//SQLite
//Print the database
public void printDatabase(){
String dbString = dbHandler.databaseToString();
outputText.setText(dbString);
//buckysInput.setText("");
}
public void setKickCountText(){
kickCount.setText(kick.toString());
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
如果需要,可以使用MyDBHandler.java
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
//We need to pass database information along to superclass
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_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
//Add a new row to the database
public void addProduct(Product product){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
//Delete a product from the database
public void deleteProduct(String productName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS + " 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("productname")) != null) {
dbString += c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
c.moveToNext();
}
db.close();
return dbString;
}
kickCounter.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/kick_counter_ID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/kickCounterFragmentBackground"
tools:context="binarysoup.mommy_info.ActivitiesAndFragments.KickCounter">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/start_button_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_above="@+id/kick_button_ID"
android:layout_centerHorizontal="true"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.651" />
<Button
android:id="@+id/kick_button_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kick!"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="71dp"
android:layout_marginLeft="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.758"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.728" />
<Button
android:id="@+id/remove_kick_button_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove Kick"
android:layout_alignBaseline="@+id/kick_button_ID"
android:layout_alignBottom="@+id/kick_button_ID"
android:layout_toRightOf="@+id/start_button_ID"
android:layout_toEndOf="@+id/start_button_ID"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.758"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.105" />
<Chronometer
android:id="@+id/chronometer_ID"
android:textSize="50dp"
android:layout_width="168dp"
android:layout_height="56dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.086"
app:layout_constraintHorizontal_bias="0.64" />
<TextView
android:id="@+id/kicksText_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Kicks:"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.386"
app:layout_constraintVertical_bias="0.299" />
<TextView
android:id="@+id/kickCount_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="00"
android:layout_marginLeft="8dp"
android:layout_marginRight="136dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.298"
app:layout_constraintLeft_toRightOf="@+id/kicksText_ID" />
<TextView
android:id="@+id/outputText_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
答案 0 :(得分:1)
问题在于您对片段生命周期的解释。片段生命周期onCreateView()
在onCreate
之前调用,因此printView引用中的TextView引用为null。
尝试在onCreateView()
中添加disp()
的代码。
答案 1 :(得分:1)
#。不是从printDatabase()
调用方法onCreate()
,而是从onCreateView()
调用它。您可以使用non-graphical
方法进行任何onCreate()
初始化。在onCreateView()
中,您应该分配并声明以后要使用的任何View
变量。
#。在您的方法databaseToString()
中使用:
String query = "SELECT * FROM " + TABLE_PRODUCTS;
而不是:
String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
更新您的databaseToString()
方法,如下所示:
public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS;
//Cursor points to a location in your results
Cursor c = db.rawQuery(query, null);
if(c.moveToFirst())
{
do
{
if (c.getString(c.getColumnIndex(COLUMN_PRODUCTNAME)) != null) {
dbString += c.getString(c.getColumnIndex(COLUMN_PRODUCTNAME));
dbString += "\n";
}
}while(c.moveToNext());
}
// Close
c.close();
return dbString;
}
希望这会有所帮助〜