我创建了一个用户可以编辑信息的片段,但每当我在android studio中运行应用程序时,它总是给我这个错误:获取资源号0x00000000的值时没有包标识符。我也无法从导航抽屉打开这个网页。
package com.example.redir.wealthtrack;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.redir.wealthtrack.db.InfoDataSource;
import com.example.redir.wealthtrack.db.LimitDataSource;
import com.example.redir.wealthtrack.model.Info;
import com.example.redir.wealthtrack.model.Limit;
/**
* A simple {@link Fragment} subclass.
*/
public class infoFragment extends Fragment {
public InfoDataSource infoDataSource;
String textInEditText;
String textInEditText2;
String textInEditText3;
public infoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((MainActivity) getActivity()).setActionBarTitle("User Target");
infoDataSource = new InfoDataSource(getActivity().getApplicationContext());
infoDataSource.open();
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_userinfo, container, false);
final EditText editText = (EditText) view.findViewById(R.id.editText);
final EditText editText2 = (EditText) view.findViewById(R.id.editText2);
final EditText editText3 = (EditText) view.findViewById(R.id.editText3);
editText.setText(infoDataSource.getFirst().getName());
editText2.setText((int) infoDataSource.getFirst().getIncome());
editText3.setText((int) infoDataSource.getFirst().getGoal());
Button AddButton = (Button) view.findViewById(R.id.AddButton);
AddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder a_builder = new AlertDialog.Builder(getActivity());
a_builder.setMessage("Confirm?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Info newInfo = new Info ();
newInfo.setName(editText.getText().toString());
newInfo.setIncome(Double.parseDouble(editText2.getText().toString()));
newInfo.setGoal(Double.parseDouble(editText3.getText().toString()));
createInfo(newInfo);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Confirmation</font>"));
alert.show();
}
});
Button EditButton = (Button) view.findViewById(R.id.EditButton);
EditButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder a_builder = new AlertDialog.Builder(getActivity());
a_builder.setMessage("Are you sure you want to edit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Info newInfo = new Info();
newInfo.setId(1);
newInfo.setName(editText.getText().toString());
newInfo.setIncome(Double.parseDouble(editText2.getText().toString()));
newInfo.setGoal(Double.parseDouble(editText3.getText().toString()));
updateInfo(newInfo);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Confirmation</font>"));
alert.show();
}
});
textInEditText = editText.getText().toString();
textInEditText2 = editText2.getText().toString();
textInEditText3 = editText3.getText().toString();
if(textInEditText.matches("")){
EditButton.setVisibility(View.INVISIBLE);
} else if(!textInEditText.matches("")){
AddButton.setVisibility(View.INVISIBLE);
}
if(textInEditText2.matches("")){
EditButton.setVisibility(View.INVISIBLE);
} else if(!textInEditText2.matches("")){
AddButton.setVisibility(View.INVISIBLE);
}
if(textInEditText3.matches("")){
EditButton.setVisibility(View.INVISIBLE);
} else if(!textInEditText3.matches("")){
AddButton.setVisibility(View.INVISIBLE);
}
return view;
}
public static void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
private void createInfo (Info info) {
infoDataSource.createInfo(info);
showToast(getActivity().getApplicationContext(), "Target added");
}
private void updateInfo (Info info) {
infoDataSource.updateInfo(info);
showToast(getActivity().getApplicationContext(), "Target updated");
}
THE DATA
package com.example.redir.wealthtrack.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.redir.wealthtrack.model.Info;
import com.example.redir.wealthtrack.model.Limit;
import java.net.URI;
/**
* Created by redir on 24/2/2016.
*/
public class InfoDataSource {
public static final String LOGTAG="WEALTHTRACK";
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
//Create some Static Variable for the Column Names
private static final String[] allColumns = {
WealthDBOpenHelper.INFO_ID,
WealthDBOpenHelper.COLUMN_NAME,
WealthDBOpenHelper.COLUMN_INCOME,
WealthDBOpenHelper.COLUMN_GOAL
};
public InfoDataSource(Context context) {
dbhelper = new WealthDBOpenHelper(context);
}
//open the DB to edit
public void open() {
Log.i(LOGTAG, "Database opened");
database = dbhelper.getWritableDatabase();
}
//Closing the DB
public void close() {
Log.i(LOGTAG, "Database closed");
dbhelper.close();
}
public Info createInfo (Info info) {
ContentValues values = new ContentValues();
values.put(WealthDBOpenHelper.COLUMN_NAME, info.getName());
values.put(WealthDBOpenHelper.COLUMN_INCOME, info.getIncome());
values.put(WealthDBOpenHelper.COLUMN_GOAL, info.getGoal());
long insertid = database.insert(WealthDBOpenHelper.TABLE_INFO, null, values);
info.setId(insertid);
return info;
}
public boolean updateInfo (Info info) {
ContentValues values = new ContentValues();
values.put(WealthDBOpenHelper.COLUMN_NAME, info.getName());
values.put(WealthDBOpenHelper.COLUMN_INCOME, info.getIncome());
values.put(WealthDBOpenHelper.COLUMN_GOAL, info.getGoal());
String where = WealthDBOpenHelper.INFO_ID + "=" + info.getId();
int result = database.update(WealthDBOpenHelper.TABLE_INFO, values, where, null);
return (result ==1);
}
public Info getFirst() {
Info info = new Info();
Cursor cursor = database.query(WealthDBOpenHelper.TABLE_INFO, allColumns,
null, null, null, null, null);
if (cursor.moveToFirst()){
info.setId(cursor.getLong(cursor.getColumnIndex(WealthDBOpenHelper.INFO_ID)));
info.setName(cursor.getString(cursor.getColumnIndex(WealthDBOpenHelper.COLUMN_NAME)));
info.setIncome(Double.parseDouble(cursor.getString(cursor.getColumnIndex(WealthDBOpenHelper.COLUMN_INCOME))));
info.setGoal(Double.parseDouble(cursor.getString(cursor.getColumnIndex(WealthDBOpenHelper.COLUMN_GOAL))));
}
return info;
}
}
THE LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".infoFragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="@+id/editText"
android:padding="8dp"
android:hint="Name"
android:layout_marginTop="93dp"
android:textColorHint="@color/textcolor"
android:textColor="@color/textcolor"
android:background="#08ae9e"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText2"
android:hint="Monthly income"
android:padding="8dp"
android:textColorHint="@color/textcolor"
android:textColor="@color/textcolor"
android:background="#08ae9e"
android:layout_below="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="@+id/editText3"
android:padding="8dp"
android:hint="Saving goal"
android:textColorHint="@color/textcolor"
android:textColor="@color/textcolor"
android:background="#08ae9e"
android:layout_below="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_marginTop="20dp" />
<Button
android:layout_width="200dp"
android:layout_height="45dp"
android:text="Add"
android:id="@+id/AddButton"
android:textColor="#ffffff"
android:background="#08ae9e"
android:padding="8dp"
android:clickable="true"
android:layout_below="@+id/editText3"
android:layout_alignEnd="@+id/editText3"
android:layout_marginTop="45dp"
android:layout_alignStart="@+id/editText3" />
<Button
android:layout_width="200dp"
android:layout_height="45dp"
android:text="Edit"
android:id="@+id/EditButton"
android:textColor="#ffffff"
android:background="#08ae9e"
android:padding="8dp"
android:clickable="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/AddButton"
android:layout_alignStart="@+id/AddButton"
android:layout_alignEnd="@+id/AddButton" />
</RelativeLayout>
答案 0 :(得分:3)
editText2.setText((int) infoDataSource.getFirst().getIncome());
editText3.setText((int) infoDataSource.getFirst().getGoal());
您无意中使用setText(int resId)
的重载版本而非setText(String text)
,在这种情况下,它无法找到标识为0的字符串资源。
答案 1 :(得分:0)
@Geralt_Encore是正确的。你应该使用:
editText2.setText(String.valueOf(infoDataSource.getFirst().getIncome()));
editText3.setText(String.valueOf(infoDataSource.getFirst().getGoal()));