简体   繁体   中英

Android project - cannot be resolved or is not a field

I have been working on various Android apps for quite some time now and am trying to finalize them. One of these apps involves creating a text field with a button. Below the button and the text field is some text (aligned left) and a spinner(calendar) which aligned right. Here is my code...

MainActivity.java

package com.miller.lab2;

import android.R;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item);

        Spinner spinner = (Spinner) findViewById(R.id.listofmonths);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.monthlist, 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);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // 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);
    }

    public void onClick(View view) {
        EditText input = (EditText) findViewById(R.id.text);
        String string = input.getText().toString();
        Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
    Resources res = getResources();
    String[] monthlist = res.getStringArray(R.array.monthlist);
}

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.miller.lab2.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:labelFor="@+id/text"
        android:text="@string/enter_message" />

    <TextView
        style="@+style/CustomText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_toLeftOf="@+id/button"
        android:text="@string/text"
        android:textColor="#347C17" />

   <!-- <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_toRightOf="@+id/button"
        android:text="@string/text2" /> -->

    <Spinner
        android:id="@+id/monthlist"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

    <EditText
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enter_message"
        android:inputType="text"
        android:text="@string/text_field" />

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

</RelativeLayout>

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.miller.lab2.MainActivity" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

I'm getting the following errors in my MainActivty.java:

  listofmonths cannot be resolved or is not a field
  monthlist cannot be resolved or is not a field
  main cannot be resolved or is not a field
  action_settings cannot be resolved or is not a field
  text cannot be resolved or is not a field

Any help would be greatly appreciated

You are importing the R class from android

Delete this import import android.R; and reimport the R class from your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM