Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: incompatible types: View cannot be converted to TextView and Error:Execution failed for task ':app:compileDebugJavaWithJavac'

I am new to Android programming and trying to create an app with the following Java activity codes,

So in com.ezbatech.healthtips i have,

Main Activity.java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
static final String EXTRA_MESSAGE = "com.ezbatech.healthtips.MESSAGE";

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

/** Called when the user taps the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
    // Do something in response to button
}`

Main2Activity.java:

package com.ezbatech.healthtips;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView = findViewById(R.id.textView);
    textView.setText(message);
}

}

However, the code returns the following errors in the headline and as below,

Error:(19, 41) error: incompatible types: View cannot be converted to TextView

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

The document I refered to is https://developer.android.com/training/basics/firstapp/starting-activity.html

I believe this is basic. Any Help will be appreciated.

Thanks!

like image 605
TechSav Avatar asked Nov 22 '17 23:11

TechSav


2 Answers

If you are not using the new library of the app-compat then you should do the cast manually:

Change this :

TextView textView = findViewById(R.id.textView);

To this:

TextView textView = (TextView)findViewById(R.id.textView); 
like image 143
diegoveloper Avatar answered Nov 15 '22 06:11

diegoveloper


Try this code:

MainActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
 static final String EXTRA_MESSAGE = "com.ezbatech.healthtips.MESSAGE";
 EditText editText;

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

 /** Called when the user taps the Send button */
 public void sendMessage(View view) {
   Intent intent = new Intent(this, DisplayMessageActivity.class);
   String message = editText.getText().toString();
   intent.putExtra(EXTRA_MESSAGE, message);
   startActivity(intent);
  }
  // Do something in response to button
}

MainActivity2

 package com.ezbatech.healthtips;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView = (TextView)findViewById(R.id.textView);
    textView.setText(message);
}
like image 31
Sanal Avatar answered Nov 15 '22 06:11

Sanal