错误:不兼容的类型:视图无法转换为TextView和错误:任务执行失败':app:compileDebugJavaWithJavac'

时间:2017-11-22 23:19:17

标签: android android-layout android-activity

我是Android编程的新手,并尝试使用以下Java活动代码创建应用程序

所以在com.ezbatech.healthtips我有,

主要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);
}

}

但是,代码在标题中返回以下错误,如下所示

错误:(19,41)错误:不兼容的类型:视图无法转换为TextView

错误:任务':app:compileDebugJavaWithJavac'执行失败。

编译失败;有关详细信息,请参阅编译器错误输出。

我提到的文件是https://developer.android.com/training/basics/firstapp/starting-activity.html

我相信这是基本的。任何帮助将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:7)

如果您没有使用app-compat的新库,那么您应该手动进行投射:

改变这个:

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

对此:

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

答案 1 :(得分:2)

试试这段代码:

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);
}