tags:

views:

64

answers:

2

Hello, I'm new to Android programming. I have a program that looks like this:

Here is the main java block:

public class MyAndroid extends Activity {
    private EditText input1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.main);
        input1 = (EditText) findViewById(R.id.input1);
}

This is what my main.xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget45"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android"&gt;

 <EditText android:id="@+id/input1" android:layout_width="160px"
  android:layout_height="wrap_content" android:textSize="18sp"

  android:numeric = "decimal|signed"
  android:layout_x="8px" android:layout_y="13px">
 </EditText>

</AbsoluteLayout>

However, I'm getting an error message:

R.id.input1 cannot be resolved. Why can't I access 'input1' from main.xml?

I've looked around on the web and haven't found this answer.

Thanks

+2  A: 

Most likely this problem occurs when instead of importing application specific R class the android.R class has been imported.

but then R.layout.main wouldn't be found either... or main does also exist in android.R,... don't know
Mathias Lin
You don't need to import R since it's in your main application package. android.R is different from your.package.R and it won't work
Falmarri
There's too much code unseen. There should be a package name for `MyAndroid.java` and there should be a package name in *R.java*. They need to be in the same package; or MyAndroid.java needs to import R from where ever it is. The import declarations aren't shown. Ensure the imports don't hide any needed symbols.
Frayser
yep sometime you can import the wrong R file by mistake, also make sure some other resource doesn't have a typo or something it will keep your R file from generating.
schwiz
A: 

Well there are two errors in your code, you need another bracket at the very end and mContext = this; should be Context mContext = this; but you don't even need that line. That's all I can see. Other than that it should work as expected.

EDIT: I am assuming nothing here except that you are an absolute beginner, in that case what you are trying to do should look exactly like this from start to finish (your main.xml is fine):

package com.myandroid; // This line may be different depending on what you named your    package when you created the project.

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class MyAndroid extends Activity {
    private EditText input1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        input1 = (EditText) findViewById(R.id.input1);
    }
}
ShadowGod
He probably has `Context mContext` in the class further up there an omitted it in the code - given that it's called mContext, that seems likely. Weird though - why would you have a member variable that essentially just contains `this`?!
EboMike