Android App Error Handling

Asked 2 years ago, Updated 2 years ago, 108 views

The error was resolved a little, so I corrected it.
Therefore, "ListView cannot be resolved or is not a field"
I would like to resolve .
What should I do?
Details below.

I have a question about handling errors on Android apps.
I don't know much about Android apps. I'll write the code below, so please let me know what's wrong.
Also, please let me know what @Override is for.

Corrected Error

ListView listView=(ListView) findViewById (R.id.ListView);


in the ↑ section ListView cannot be resolved or is not a field
The error
appears. Also,

ListAdapter adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,DAYS);


in the ↑ section Multiple markers on this line
- Resolve ListAdapter into a mold
No
- Resolve ArrayAdapter as a mold
I can't
The error
appears. If you know how to deal with it, please.

package com.example.recip;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extensions ActionBarActivity {

    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView= (ListView) findViewById (R.id.ListView);

        // Create adapter
        ListAdapter adapter = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1,DAYS);
        // Adapter Configuration
        listView.setAdapter(adapter);
    }
    // String to display in ListView
    private static final String[]DAYS=new String[]{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    @ Override
    public boolean onCreateOptionsMenu(Menu){
        // Inflate the menu; this add items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @ Override
    public boolean onOptionsItemSelected(MenuItem){
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, solo
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if(id==R.id.action_settings){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

It also contains the code for the XML file.

<RelativeLayoutxmlns: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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.recip.MainActivity">

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Place Buttons"/>

    <Button
        android: id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android: text="gazou"/>

    <ListView
        android:id="@+id/ListView"
        android:layout_width="fill_parent"
        android:layout_height="0 dip"
        android:layout_weight="1"/>
</RelativeLayout>

android java android-layout xml

2022-09-30 20:48

1 Answers

The IDE you are using is eclipse, right?

ListView cannot be resolved or is not a field

This error indicates that the R.id.ListView name is not found in the findViewById(R.id.ListView) part.

ListView in XML@+id/ListView and ListView in findViewById(R.id.ListView) must be the same case.
Looking at the source, the above error does not appear.Eclipse compiles java and xml files when they are saved (ctrl+s).
Are you saving the xml file?

Cannot resolve ListAdapter to type
ArrayAdapter cannot be resolved to a type

I think this is because the import statement is not specified.
eclipse does not automatically add import statements.
Use the ctrl+shift+O key to add (organize) the import statement.

Also, please let me know what @Override is for.

@hogehoge above a method or field is called an annotation.
@Override indicates that the method is overriding.
For this annotation, I'm just telling the compiler that this is an overriding method, so erasing it won't cause any errors, and it won't change its behavior.
However, if you type the wrong method name, the compiler will give you an error, so don't erase it.
You might want to read a book about Java.


2022-09-30 20:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.