创建按钮监听器第一个Android应用程序时出错

时间:2016-07-07 16:06:30

标签: java android

因为这是我的第一个Android应用程序;我无法弄清楚出了什么问题。

错误

在线错误" caller.textView.setText(“你点击了按钮!”);"

此行有多个标记

  • 语法错误,插入" AssignmentOperator Expression"去完成  分配
  • 语法错误,插入&#34 ;;"完成BlockStatements
  • 行断点:MyOnClickListener [line:12] - onClick(查看)
  • setText无法解析或不是字段

screenshot of error

MainActivity.java

package com.example.myfirstandroidapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button button;
    TextView textView;

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

        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new MyOnClickListener(this));
        textView = (TextView) findViewById(R.id.textView1);
    }


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

}

MyOnClickListener.java

package com.example.myfirstandroidapp;

import android.view.View;
import android.view.View.OnClickListener;

public class MyOnClickListener implements OnClickListener {
    MainActivity caller;
    public MyOnClickListener(MainActivity activity) {
    this.caller = activity;
    }
    public void onClick(View view) {
    caller.textView.setText(“You clicked the button!”);
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="70dp"
        android:text="Button" />

</RelativeLayout>

0 个答案:

没有答案