>

먼저 XML로이 뷰를 보았습니다.이 파일에서는 onClick 가 필요합니다.  간단한 토스트를 실행하는 이벤트 인 경우 메소드를 join이라고합니다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hrizi.onescore.community">
    <include
        android:id="@+id/toolbar"
        layout="@layout/apptoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:scaleType="fitCenter" />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="65dp"
        android:background="@android:color/white"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="16dp">
            <!--First name-->
            <!--last name-->
            <!--Date of Birth -->
            <android.support.design.widget.TextInputLayout
                android:id="@+id/ti_dateofbirth"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">
                <EditText
                    android:id="@+id/et_dateofbirth"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/edittextbackground"
                    android:ems="10"
                    android:hint="Date of birth"
                    android:inputType="date"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:singleLine="false"
                    android:textColorHint="@color/inputcolor" />
            </android.support.design.widget.TextInputLayout>
            <!--title-->
            <android.support.design.widget.TextInputLayout
                android:id="@+id/ti_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">
                <EditText
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/edittextbackground"
                    android:ems="10"
                    android:hint="Title"
                    android:inputType="textPersonName"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:singleLine="false"
                    android:textColorHint="@color/inputcolor" />
            </android.support.design.widget.TextInputLayout>
            <!--University-->
            <android.support.design.widget.TextInputLayout
                android:id="@+id/ti_university"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">
                <EditText
                    android:id="@+id/et_university"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/edittextbackground"
                    android:ems="10"
                    android:hint="University"
                    android:inputType="text"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:singleLine="false"
                    android:textColorHint="@color/inputcolor" />
            </android.support.design.widget.TextInputLayout>
            <!--About me-->
            <android.support.design.widget.TextInputLayout
                android:id="@+id/ti_aboutme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">
                <EditText
                    android:id="@+id/et_aboutme"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/edittextbackground"
                    android:ems="10"
                    android:hint="About me"
                    android:inputType="text"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:singleLine="false"
                    android:textColorHint="@color/inputcolor" />
            </android.support.design.widget.TextInputLayout>
            <Button
                android:id="@+id/btn_join"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="25dp"
                android:background="@color/coloronscore"
                android:text="Join us"
                android:textAllCaps="false"
                android:textColor="@android:color/background_light"
                android:textStyle="normal"
                android:onClick="join"/>

            <ProgressBar
                android:id="@+id/progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tv_forgot_password"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="20dp"
                android:visibility="gone" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

다른 Java 파일에는 다음 코드가 있습니다.

package com.example.hrizi.onescore;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.hrizi.onescore.model.Transcriber;
import com.example.hrizi.onescore.utils.Constants;
import rx.subscriptions.CompositeSubscription;
public class community extends AppCompatActivity {
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if(getSupportActionBar()!=null){
            getSupportActionBar().setTitle("Join Our Community !");
        }
        //initializeViews();
    }
    public void join(View view)
    {
        Snackbar.make(view, Constants.EMAIL ,Snackbar.LENGTH_SHORT).show();
        EditText title_Text = (EditText) view.findViewById(R.id.ti_title) ;
        String title = title_Text.getText().toString();
        Toast.makeText(community.this,"title",Toast.LENGTH_LONG).show();
    }
}

토스트가 나타나지 않고 응용 프로그램이 충돌하는 문제


  • 답변 # 1

    이거 해봐

    public class community extends AppCompatActivity {
    Toolbar toolbar;
    EditText title_Text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        title_Text = (EditText)findViewById(R.id.ti_title) ;  // here changes
        if(getSupportActionBar()!=null){
            getSupportActionBar().setTitle("Join Our Community !");
        }
        //initializeViews();
    }
    public void join(View view)
    {
        Snackbar.make(view, Constants.EMAIL ,Snackbar.LENGTH_SHORT).show();
        String title = title_Text.getText().toString();
        Toast.makeText(community.this,"title",Toast.LENGTH_LONG).show();
    }
    }
    
    

  • 답변 # 2

    ti_title은 실제로는 EditText가 아닌 TextInputLayout입니다. 이것이 충돌의 원인 일 수 있습니다. 줄을 바꾸십시오 :-

    EditText title_Text = (EditText) view.findViewById(R.id.title) ;
    
    

  • 답변 # 3

    코드에서 두 가지를 볼 수 있습니다

    1) "R.id.ti_title" belongs to "TextInputLayout" not "EditText"
    2) You cannot find id of your "EditText" from onClick(View v) [assuming you are passing this view object on button click in join(View view)]
    
    

    해결 방법

    public class community extends AppCompatActivity {
    Toolbar toolbar;
    EditText title_Text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        title_Text = (EditText)findViewById(R.id.title); // Modified Line 
        if(getSupportActionBar()!=null){
            getSupportActionBar().setTitle("Join Our Community !");    
        }
        //initializeViews();    
    }
     public void join(View view)
     {
        Snackbar.make(view, Constants.EMAIL ,Snackbar.LENGTH_SHORT).show();
        String title = title_Text.getText().toString();
        Toast.makeText(community.this,title,Toast.LENGTH_LONG).show();
     }
    }
    
    

    어쨌든 도움이 되길 바랍니다 ...

  • 이전 java - RestTemplate을 사용하여 널 응답을 제공하는 RestAPI에 대한 요청 게시
  • 다음 javascript - 이 JSON 검색 코드가 작동하지 않는 이유는 무엇입니까?