简体   繁体   中英

Cannot resolve method findViewByID

I'm very new to android development, so I'm sorry if I can't understand what exactly is going on here.

I've created a shape, as can be seen below. This is contained within shape.xml within the drawable folder:

<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid
    android:color="@color/lboro_pink"/>

<stroke
    android:width="50dp"
    android:color="@color/lboro_blue"/>

<padding android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp"/>

<corners
    android:bottomRightRadius="7dp"
    android:bottomLeftRadius="7dp"
    android:topLeftRadius="7dp"
    android:topRightRadius="7dp"/>
</shape>

Now within my activity_main.xml I have this TextView:

<TextView
    android:id="@+id/grid_1"
    android:background="@drawable/shape"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:text="@string/event_1"/>

Now this is my MainActivity.java:

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.res.Resources;


public class MainActivity extends Activity {

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

    Resources res = getResources();
    Drawable shape = res. getDrawable(R.drawable.shape);

    TextView tv = (TextView)findViewByID(R.id.grid_1);
    tv.setBackground(shape);
}

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

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

I've been trying to follow this doc in order to do this, and by using their code I just get the error that it "Cannot resolve method findViewByID". I've searched for answers, but just can't understand what they're trying to explain, when the official android doc says to do this.

All I'm trying to do with this is apply the shape I've created as the background to a TextView.

Any help is greatly appreciated. Thank you :)

TextView tv = (TextView)findViewByID(R.id.grid_1); 

This line is misspeled. It should be

TextView tv = (TextView)findViewById(R.id.grid_1);

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