使用setOnclickListener和View时出错

时间:2018-07-04 18:38:00

标签: java android

在这里,用户必须在此应用中输入3个联系人的姓名和电话号码并进行注册。我使用了sharedpreferences方法来保存数据输入。但是setOnclickListener和View仍然无法解析符号。请为我初学者提供帮助。任何帮助表示赞赏。

这是MainActivity.java文件

package com.me.addcontact

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

SharedPreferences sharedPreferences;

SharedPreferences.Editor editor;

Button btn1= (Button) findViewById(R.id.button1);

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        EditText name1 = (EditText)findViewById(R.id.name1);
        EditText num1 = (EditText)findViewById(R.id.num1);

        EditText name2 = (EditText)findViewById(R.id.name2);
        EditText num2 = (EditText)findViewById(R.id.num2);

        EditText name3 = (EditText)findViewById(R.id.name3);
        EditText num3 = (EditText)findViewById(R.id.num3);

        editor = sharedPreferences.edit();
        editor.putString("name1", name1.getText().toString());
        editor.putInt("num1", Integer.parseInt(num1.getText().toString()));
        editor.putString("name2", name2.getText().toString());
        editor.putInt("num2", Integer.parseInt(num2.getText().toString()));
        editor.putString("name3", name3.getText().toString());
        editor.putInt("num3", Integer.parseInt(num3.getText().toString()));

        editor.commit();
        Toast.makeText(getApplicationContext(), "Details Saved", Toast.LENGTH_SHORT).show();

    }});


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sharedPreferences = this.getSharedPreferences("com.me.addcontact", Context.MODE_PRIVATE);

}
}

这是XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.ashmi.addcontact.MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="70sp"
    android:background="#cccccc"
    android:id="@+id/buttonLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true">

    <Button
        android:layout_width="300sp"
        android:layout_height="50sp"
        android:text=" Register"
        android:textColor="#bfdfdf"
        android:textSize="15sp"
        android:background="#003366"
        android:id="@+id/button1"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"

    android:ems="10"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/name1"
    android:hint="Emergency Contact Name 1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:layout_below="@+id/name1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/num1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Number" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"

    android:ems="10"
    android:layout_below="@+id/num1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="34dp"
    android:id="@+id/name2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Emergency Contact Name 2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:layout_below="@+id/name2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/num2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Number" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"

    android:ems="10"
    android:layout_marginTop="34dp"
    android:id="@+id/name3"
    android:hint="Emergency Contact Name 3"
    android:layout_below="@+id/num2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:layout_below="@+id/name3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/num3"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Number" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您必须在此类onCreate方法中使用setOnclickListener接口

package com.me.addcontact

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

SharedPreferences sharedPreferences;

SharedPreferences.Editor editor;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sharedPreferences = this.getSharedPreferences("com.me.addcontact", Context.MODE_PRIVATE);


    Button btn1= (Button) findViewById(R.id.button1);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            EditText name1 = (EditText)findViewById(R.id.name1);
            EditText num1 = (EditText)findViewById(R.id.num1);

            EditText name2 = (EditText)findViewById(R.id.name2);
            EditText num2 = (EditText)findViewById(R.id.num2);

            EditText name3 = (EditText)findViewById(R.id.name3);
            EditText num3 = (EditText)findViewById(R.id.num3);

            editor = sharedPreferences.edit();
            editor.putString("name1", name1.getText().toString());
            editor.putInt("num1", Integer.parseInt(num1.getText().toString()));
            editor.putString("name2", name2.getText().toString());
            editor.putInt("num2", Integer.parseInt(num2.getText().toString()));
            editor.putString("name3", name3.getText().toString());
            editor.putInt("num3", Integer.parseInt(num3.getText().toString()));

            editor.commit();
            Toast.makeText(getApplicationContext(), "Details Saved", Toast.LENGTH_SHORT).show();
        }
    });

}

答案 1 :(得分:0)

chai version is 3.5

我希望这能解决您的错误..