android.content.res.Resources$NotFoundException:字符串资源ID#0x2数



当我尝试测试我的应用程序时,我得到了一个错误。

致命异常:主进程:com.example.android.lab5,PID:12261android.content.res.Resources$NotFoundException:字符串资源ID#0x2

这是我的代码

public class MainActivity extends AppCompatActivity {    
private Spinner spinnerVille;
private TextView ville;
private ListView lstJours;
int posVille = 0;    
// Les items
private ArrayList<Jour> mJours = new ArrayList<Jour>();
// L'adapteur
private JourAdapteur mJoursAdapteur;
private List<MeteoJour> meteojours;
public static final int REQUEST_TEXT = 0;
public int laPosition = 0;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseHandler db = new DatabaseHandler(this);    
Log.d("Insert: ", "Inserting ..");
/*db.addMeteoJours(new MeteoJour(1, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(2, -1, 3, -4, 5, "NO", 8, 30, 0, 2));
db.addMeteoJours(new MeteoJour(3, 2, 6, 1, 2, "SO", 8, 20, 2, 0));
db.addMeteoJours(new MeteoJour(4, 3, 6, 3, 10, "SE", 8, 0, 0, 0));
db.addMeteoJours(new MeteoJour(5, 4, 5, 4, 350, "S", 8, 30, 4, 0));
db.addMeteoJours(new MeteoJour(6, 4, 7, 5, 0, "O", 8, 0, 0, 0));
db.addMeteoJours(new MeteoJour(7, 6, 8, 8, 10, "E", 9, 15, 1, 0));
db.addMeteoJours(new MeteoJour(8, 4, 5, 4, 350, "S", 8, 30, 4, 0));;
db.addMeteoJours(new MeteoJour(9, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(10, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(11, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(12, 6, 8, 8, 10, "E", 9, 15, 1, 0));
db.addMeteoJours(new MeteoJour(13, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(14, 6, 8, 8, 10, "E", 9, 15, 1, 0));
db.addMeteoJours(new MeteoJour(15, 4, 5, 4, 350, "S", 8, 30, 4, 0));
db.addMeteoJours(new MeteoJour(16, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(17, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(18, 4, 5, 4, 350, "S", 8, 30, 4, 0));
db.addMeteoJours(new MeteoJour(19, 6, 8, 8, 10, "E", 9, 15, 1, 0));
db.addMeteoJours(new MeteoJour(20, 2, 5, -2, 10, "SO", 9, 15, 1, 0));
db.addMeteoJours(new MeteoJour(21, -1, 3, -4, 5, "NO", 9, 30, 0, 2));
db.addMeteoJours(new MeteoJour(22, 2, 5, 2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(23, 2, 5, 2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(24, 2, 5, 2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(25, 2, 5, 2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(26, 6, 8, 8, 10, "E", 9, 15, 1, 0));
db.addMeteoJours(new MeteoJour(27, 2, 5, 2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(28, 2, 5, 2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(29, 2, 6, 1, 2, "SO", 9, 20, 2, 0));
db.addMeteoJours(new MeteoJour(30, 3, 6, 3, 10, "SE", 9, 0, 0, 0));
db.addMeteoJours(new MeteoJour(31, 2, 5, -2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(32, 7, 10, 10, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(33, 2, 5, 2, 10, "SO", 8, 15, 1, 0));
db.addMeteoJours(new MeteoJour(34, 6, 8, 8, 10, "E", 9, 15, 1, 0));
db.addMeteoJours(new MeteoJour(35, 2, 5, 2, 10, "SO", 8, 15, 1, 0));*/
// Reading all contacts
Log.d("Reading: ", "Reading all meteojours..");
//final List<Contact> contacts = db.getAllContacts();
meteojours = db.getAllMeteoJours();
for (MeteoJour cn : meteojours) {
mJours.add(new Jour((String.valueOf(cn.get_id()) + " Mars 2016"), cn.get_minimum(), cn.get_maximum()));
String log = "Id: " + cn.get_id() + " ,Minimum: " + cn.get_minimum() + " ,Maximum: " + cn.get_maximum();
// Writing to log
Log.d("Name: ", log);
}    
spinnerVille = (Spinner) findViewById(R.id.spinnerVille);
ville = (TextView) findViewById(R.id.Ville);
lstJours = (ListView) findViewById(R.id.lstJours);    
// Adapteur
//mHerosAdapteur = new HeroAdapteur(this, MonSingleton.getInstance().getHeros());
mJoursAdapteur = new JourAdapteur(this, mJours);
// Assigner l'adapteur à la liste
lstJours.setAdapter(mJoursAdapteur);    
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.villes, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerVille.setAdapter(adapter);
spinnerVille.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
switch (position) {
case 0:
ville.setText("Gatineau");
posVille = 0;
break;
case 1:
ville.setText("Trois-Rivière");
posVille = 1;
break;
case 2:
ville.setText("Montréal");
posVille = 2;
break;
case 3:
ville.setText("Sherbrooke");
posVille = 3;
break;
case 4:
ville.setText("Alma");
posVille = 4;
break;
}    
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}    
});
//lstJours.setOnItemClickListener(new AdapterView.OnItemClickListener() {
lstJours.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// La seconde activité
Intent intent = new Intent(MainActivity.this, Meteo.class);
// Les paramètres
meteojours = db.getAllMeteoJours();
laPosition = position;
MeteoJour cn = meteojours.get(position);
// Les paramètres
intent.putExtra("Id", String.valueOf(cn.get_id()));
intent.putExtra("Min", cn.get_minimum());
intent.putExtra("Max", cn.get_maximum());
intent.putExtra("Ressentie", cn.get_ressentie());
intent.putExtra("VitesseVent", cn.get_vent_vitesse());
intent.putExtra("VetesseDirec", cn.get_vent_direction());
intent.putExtra("Ensoleillement", cn.get_ensoleillement());
intent.putExtra("Prob", cn.get_probabilite());
intent.putExtra("Pluie", cn.get_pluie());
intent.putExtra("Neige", cn.get_neige());
intent.putExtra("villeslct", spinnerVille.getItemAtPosition(posVille).toString());
intent.putExtra("joursslct", lstJours.getItemAtPosition(position).toString());
startActivity(intent);
// Lancer la seconde activité

}
});    
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TEXT) {
if (resultCode == Meteo.RETURN_CODE_OK) {
//Toast.makeText(MainActivity.this, data.getStringExtra("prenom"), Toast.LENGTH_SHORT).show();
mJours.add(new Jour(data.getStringExtra("date"), data.getIntExtra("minimum", 0), data.getIntExtra("maximum", 0)));
mJoursAdapteur.notifyDataSetChanged();
}
}    
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.android.lab5/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.android.lab5/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}

我隔离了我的错误,它似乎来自那行:

mJours.add(new Jour((String.valueOf(cn.get_id()) + " Mars 2016"), cn.get_minimum(), cn.get_maximum()));

我的java类Jour的代码是:

public class Jour {    
private int mTempMin;
private int mTempMax;
private String mDates;
public Jour(String date, int min, int max) {
mTempMin = min;
mTempMax = max;
mDates = date;
}    
public String getmDates() {
return mDates;
}
public void setmDates(String mDates) {
this.mDates = mDates;
}
public int getmTempMin() {
return mTempMin;
}
public void setmTempMin(int mTempMin) {
this.mTempMin = mTempMin;
}
public int getmTempMax() {
return mTempMax;
}
public void setmTempMax(int mTempMax) {
this.mTempMax = mTempMax;
}    
}

谢谢,我希望我们能找到答案!

*编辑

这是MeteoJour级别:

public class MeteoJour {
//private variables
int _id;
int _minimum;
int _maximum;
int _ressentie;
int _vent_vitesse;
String _vent_direction;
int _ensoleillement;
int _probabilite;
int _pluie;
int _neige;

// Empty constructor
public MeteoJour(){
}
// constructor
public MeteoJour(int id, int minimum, int maximum, int ressentie, int vent_vitesse, String vent_direction, int ensoleillement, int probabilite, int pluie, int neige){
this._id = id;
this._minimum = minimum;
this._maximum = maximum;
this._ressentie = ressentie;
this._vent_vitesse = vent_vitesse;
this._vent_direction = vent_direction;
this._ensoleillement = ensoleillement;
this._probabilite = probabilite;
this._pluie = pluie;
this._neige = neige;
}
// constructor
public MeteoJour(int minimum, int maximum, int ressentie, int vent_vitesse, String vent_direction, int ensoleillement, int probabilite, int pluie, int neige){
this._minimum = minimum;
this._maximum = maximum;
this._ressentie = ressentie;
this._vent_vitesse = vent_vitesse;
this._vent_direction = vent_direction;
this._ensoleillement = ensoleillement;
this._probabilite = probabilite;
this._pluie = pluie;
this._neige = neige;
}

public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public int get_minimum() {
return _minimum;
}
public void set_minimum(int _minimum) {
this._minimum = _minimum;
}
public int get_maximum() {
return _maximum;
}
public void set_maximum(int _maximum) {
this._maximum = _maximum;
}
public int get_ressentie() {
return _ressentie;
}
public void set_ressentie(int _ressentie) {
this._ressentie = _ressentie;
}
public int get_vent_vitesse() {
return _vent_vitesse;
}
public void set_vent_vitesse(int _vent_vitesse) {
this._vent_vitesse = _vent_vitesse;
}
public String get_vent_direction() {
return _vent_direction;
}
public void set_vent_direction(String _vent_direction) {
this._vent_direction = _vent_direction;
}
public int get_ensoleillement() {
return _ensoleillement;
}
public void set_ensoleillement(int _ensoleillement) {
this._ensoleillement = _ensoleillement;
}
public int get_probabilite() {
return _probabilite;
}
public void set_probabilite(int _probabilite) {
this._probabilite = _probabilite;
}
public int get_pluie() {
return _pluie;
}
public void set_pluie(int _pluie) {
this._pluie = _pluie;
}
public int get_neige() {
return _neige;
}
public void set_neige(int _neige) {
this._neige = _neige;
}
}

这是R.array.ville XML:

<string name="app_name">Lab5</string>
<string-array name="joursSemaine">
<item>Dimanche le 28 février 2016</item>
<item>Lundi le 29 février 2016</item>
<item>Mardi le 1 mars 2016</item>
<item>Mercredi le 2 mars 2016</item>
<item>Jeudi le 3 mars 2016</item>
<item>Vendredi le 4 mars 2016</item>
<item>Samedi le 5 mars 2016</item>
</string-array>
<string-array name="villes">
<item>Gatineau</item>
<item>Trois-Rivière</item>
<item>Montreal</item>
<item>Sherbrooke</item>
<item>Alma</item>
</string-array>

我的日志:

04-27 08:21:24.986 2342-2342/?I/art:延迟启用-Xcheck:jni 04-27 08:21:25.038 2342-2342/?插入::插入。。04-27 08:21:25.210 2342-2342/?D/Reading::阅读所有的气象。。04-27 08:21:25.211 2342-2342/?D/名称::Id:1,最小值:2,最大值:5 04-27 08:21:25.211 2342-2342/?D/名称::Id:2,最小:-1,最大:3 04-27 08:21:25.211 2342-2342/?D/姓名::Id:3,最小:2,最大:6 04-27 08:21:25.211 2342-2342/?D/姓名::Id:4,最小值:3,最大值:6 04-27 08:21:25.211 2342-2342/?D/姓名::Id:5,最小值:4,最大值:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:6,最小:4,最大:7 04-27 08:21:25.211 2342-2342/?D/姓名::Id:7,最小:6,最大:8 04-27 08:21:25.211 2342-2342/?D/姓名::Id:8,最小:4,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:9,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:10,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:11,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:12,最小:6,最大:8 04-27 08:21:25.211 2342-2342/?D/姓名::Id:13,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:14,最小:6,最大:8 04-27 08:21:25.211 2342-2342/?D/姓名::Id:15,最小:4,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:16,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:17,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:18,最小:4,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:19,最小:6,最大:8 04-27 08:21:25.211 2342-2342/?D/姓名::Id:20,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/名称::Id:21,最小:-1,最大:3 04-27 08:21:25.211 2342-2342/?D/姓名::Id:22,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:23,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:24,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:25,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:26,最小:6,最大:8 04-27 08:21:25.211 2342-2342/?D/姓名::Id:27,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:28,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:29,最小:2,最大:6 04-27 08:21:25.211 2342-2342/?D/姓名::Id:30,最小值:3,最大值:6 04-27 08:21:25.211 2342-2342/?D/姓名::Id:31,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:32,最小:7,最大:10 04-27 08:21:25.211 2342-2342/?D/姓名::Id:33,最小:2,最大:5 04-27 08:21:25.211 2342-2342/?D/姓名::Id:34,最小:6,最大:8 04-27 08:21:25.211 2342-2342/?D/姓名::Id:35,最小:2,最大:5 04-27 08:21:25.218 2342-2360/?W/谷歌播放服务Util:谷歌播放服务丢失。04-27 08:21:25.254 2342-2361/?D/OpenGLRenderer:使用EGL_SWAP_BEHAVIOR_PRESERVED:true 04-27 08:21:25.259 2342-2342/?D/Atlas:正在验证地图。。。04-27 08:21:25.271 2342-2342/?W/ResourceType:获取资源编号0x00000002 04-27 08:21:25.272 2342-2342/?的值时没有包标识符?D/AndroidRuntime:关闭VM 04-27 08:21:25.291 2342-2342/?E/AndroidRuntime:致命异常:主进程:com.example.android.lab5,PID:2342 android.content.res.Resources$NotFoundException:android.widget.widget.ListView.measureHeightOfChildren的android.coontent.res.Resources.getText(Resources.java:299)处的字符串资源ID#0x2(ListView.java:1270)在android.widget.ListView.onMeasure(ListView.java:1182)在android.view.view.measure(view.java:17547android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.ja:135)android.view.ViewGroup.measureChildWithMargins(ViewGroup.ja:55535)android.ssupport.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlaylayLayout.ja:391)android.view.view.measureandroid.widget.LinearLayout.measureVertical的android.View.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)上的(View.java:17547)android.widget.FrameLayout.onMeasure(FrameLayout。

JourAdapter类别:

公共类JourAdapter扩展ArrayAdapter{

private Context mContext;
private LayoutInflater mInflater;
public JourAdapteur(Context context, ArrayList<Jour> jours) {
super(context, 0, jours);
mContext = context;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View theView = convertView;
if (theView == null) {
theView = mInflater.inflate(R.layout.jour_list_item, parent, false);
}
// Le hero
Jour jour = this.getItem(position);
// Aller chercher les objets
TextView lblMin = (TextView) theView.findViewById(R.id.lblMin);
TextView lblMax = (TextView) theView.findViewById(R.id.lblMax);
TextView lblDate = (TextView) theView.findViewById(R.id.lblDate);
// Assigner les valeurs
lblMin.setText(jour.getmTempMin());
lblMax.setText(jour.getmTempMax());
lblDate.setText(jour.getmDates());

// Retourner la vue
return theView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getView(position, convertView, parent);
}

}

lblMin.setText(jour.getmTempMin());
lblMax.setText(jour.getmTempMax());

您调用的setText(int)需要一个资源id。要使用setText(CharSequence),您可以将代码更改为例如

lblMin.setText(String.valueOf(jour.getmTempMin()));
lblMax.setText(String.valueOf(jour.getmTempMax()));

您需要使用字符串。这对我有效:

lblMin.setText(Integer.toString(jour.getmTempMin()))

这也会起作用,因为传递的参数应该是字符串当我们在java中使用+运算符时,它会将语句转换为一串

lblMin.setText(""+jour.getmTempMin());

相关内容

  • 没有找到相关文章

最新更新