当我尝试测试我的应用程序时,出现错误。



这是我的代码

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类(class):
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/? D/插入::正在插入.. 04-27 08:21:25.210 2342-2342/? D/阅读::阅读所有 meteor 。.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/GooglePlayServicesUtil:缺少Google Play服务。 04-27 08:21:25.254 2342-2361/? D/OpenGLRenderer:使用EGL_SWAP_BEHAVIOR_PRESERVED:true 04-27 08:21:25.259 2342-2342/? D/ map 集:正在验证 map ... 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:致命异常(exception):主进程:com.example.android.lab5,PID:2342 android.content.res.Resources $ NotFoundException:字符串资源ID#0x2,位于android.content.res.Resources.getText(Resources.java :299),位于com.example.android.lab5.JourAdapteur.getView(JourAdapteur.java:41),位于android.widget.TextView.setText(TextView.java:4132),位于android.widget.AbsListView.obtainView(AbsListView.java: 2347),位于android.widget.ListView.measureHeightOfChildren(ListView.java:1270),位于android.widget.ListView.onMeasure(ListView.java:1182),位于android.view.View.measure(View.java:17547)。 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)的Widget.RelativeLayout.measureChild(RelativeLayout.java:697)android.view.ViewGroup.measureChildWithMargins的android.view.View.measure(View.java:17547) (ViewGroup.java:5535)在android.widget.FrameLayout.onMeasure(FrameLayout.java:436)在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)在android.v android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)上的iew.View.measure(View.java:17547)android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)的android.widget.FrameLayout.onMeasure(FrameLayout.java:436)的.View.measure(View.java:17547) android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)的View.java:17547)android.widget.LinearLayout.measureVertical(LinearLayout.java:722)的android.widget.LinearLayout.measureChildChildBeforeLayout(LinearLayout.java:1436) )在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)在android.view.View.measure(View.java:17547)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)在android.widget .com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2615)在android.view.View的.FrameLayout.onMeasure(FrameLayout.java:436) android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)的.measure(View.java:17547)android.view.ViewRootImpl.java:1173)的android.view.ViewRootImpl.java:1173)android.view.ViewRootImpl.performTraversals(ViewRootImpl。的android.view.ViewRootImpl.doTraversal(java:1379)(ViewRootImpl.java:1061)android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5885)android.view.Choreographer $ CallbackRecord.run(Choreographer.java :767)在android.view.Choreographer.doCallbacks(Choreographer.java:580)在android.view.Choreographer.doFrame(Choreographer.java:550)在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)在android.os.Looper.loop(Looper.java:135)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:135)在android.os.Handler.handleCallback(Handler.java:739) com.android.internal上java.lang.reflect.Method.invoke(Method.java:372)上的ActivityThread.main(ActivityThread.java:5254) .os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)04-27 08:21:28.734 2342-2342/com.example。 android.lab5 I/Process:正在发送信号。 PID:2342 SIG:9

JourAdapteur类:

公共(public)类JourAdapteur扩展了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());

您正在调用需要资源ID的setText(int)。要改为使用setText(CharSequence),您可以将代码更改为例如
lblMin.setText(String.valueOf(jour.getmTempMin()));
lblMax.setText(String.valueOf(jour.getmTempMax()));

07-26 06:26