-1

Olá, estou a criar um app de login e quero alterar a fonte de um textView mas está me a dar o seguinte erro:

Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference

XML:

<?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"
        android:background="@color/colorPrimary"
        tools:context=".Login">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible"
            android:orientation="vertical">

            <TextView
                android:id="@+id/user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10sp"
                android:layout_marginTop="10sp"
                android:text="E-Mail ou NIF"
                android:textColor="#fff"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/mail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3sp"
                android:layout_marginTop="40sp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text=""
                android:textColor="#fff"
                android:textColorHighlight="#000" />

            <TextView
                android:id="@+id/pass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10sp"
                android:layout_marginTop="20sp"
                android:text="Password"
                android:textColor="#fff"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20sp"
                android:ems="10"
                android:inputType="textPassword"
                android:textColor="#fff"
                android:textColorHighlight="#000" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <Button
                    android:id="@+id/button"
                    android:layout_width="318dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20sp"
                    android:background="#09C"
                    android:onClick="entrar"
                    android:text="Entrar"
                    android:textColor="#fff" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="318dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10sp"
                    android:background="#09C"
                    android:text="Criar uma conta"
                    android:textColor="#fff" />

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvproc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#fff"
                android:textAlignment="center"
                android:layout_marginTop="200sp"
                android:textSize="30sp"
                android:text="A Processar" />

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyle"
                android:layout_marginTop="30sp"
                android:indeterminateTint="#fff"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>


    </RelativeLayout>

Java:

package imm.pt.immsmart;

    import android.content.Intent;
    import android.graphics.Typeface;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    import org.json.JSONException;
    import org.json.JSONObject;

    public class Login extends AppCompatActivity {
        public String res;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            TextView tvu = (TextView) findViewById(R.id.user);
            Typeface font = Typeface.createFromAsset(getAssets(), "fonts/open.ttf");
            tvu.setTypeface(font);
            TextView tvp = (TextView) findViewById(R.id.pass);
            Typeface fontp = Typeface.createFromAsset(getAssets(), "fonts/open.ttf");
            tvp.setTypeface(fontp);
        }

    }
1
  • 1
    Vejo o código do onCreate() de outra Activity e encontrará o que falta nessa. Quando encontrar a linha que falta tente perceber porque ela é necessária.
    – ramaral
    23/02/2019 às 20:51

1 Resposta 1

0

Você não está criando nenhum link da sua activity com esse xml. É necessário o setContentView(id) no onCreate. Ficaria +- assim

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.meu_xml);
            TextView tvu = (TextView) findViewById(R.id.user);
            [...]
            
            }

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .