简体   繁体   中英

cannot be resolved or is not a field error in android in eclipse

i get the error login cannot be resolved or is not a field error and same for passwd and ip. i get this error in eclipse while doing android program.

  public void onClick(View v) {
    if (v == this.viewcam)
      {
        Log.i("login", this.login.getText().toString());
        Log.i("Passwd", this.passwd.getText().toString());
        Intent localIntent1 = new Intent(v.getContext(), MjpegSample.class);
        Log.i("My ip", this.editTextIp.getText().toString());
        localIntent1.putExtra("ip", "http://" + this.editTextIp.getText().toString() + "/");
        localIntent1.putExtra("user", this.login.getText().toString());
        localIntent1.putExtra("passwd", this.passwd.getText().toString());
        startActivityForResult(localIntent1, 1000);
      }
    }
});

the following is the main.xml file corresponding to above

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IP CAM VIEWER"
        android:textAppearance="?android:attr/textAppearanceLarge"
        />
</LinearLayout>
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Username : "
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <EditText
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" >
            <requestFocus />
        </EditText>
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Password : "
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <EditText
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPassword" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="IP : "
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <EditText
            android:id="@+id/ip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" />
</TableLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.62" >
    <Button
        android:id="@+id/viewcam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="96dp"
        android:layout_marginTop="16dp"
        android:text="View cam" />
</RelativeLayout>
</LinearLayout>

I assume this code is in an OnClickListener, in this case, remove the this keyword, as it refers to the OnClickListener instance, and not the Activity instance:

 public void onClick(View v) {
    if (v == viewcam)
      {
        Log.i("login", login.getText().toString());
        Log.i("Passwd", passwd.getText().toString());
        Intent localIntent1 = new Intent(v.getContext(), MjpegSample.class);
        Log.i("My ip", editTextIp.getText().toString());
        localIntent1.putExtra("ip", "http://" + editTextIp.getText().toString() + "/");
        localIntent1.putExtra("user", login.getText().toString());
        localIntent1.putExtra("passwd", passwd.getText().toString());
        startActivityForResult(localIntent1, 1000);
      }
    }

Another option is to "fully qualify" this , for example:

Log.i("login", TheActivity.this.login.getText().toString());

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