Ich versuche, benutzerdefinierte Schriftarten in meiner Android App hinzuzufügen.java.lang.RuntimeException: Aktivität kann nicht gestartet werden (TextView ist null)
LoginActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.app.Activity;
import android.widget.TextView;
import android.graphics.Typeface;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button login = (Button) findViewById(R.id.login);
Button signup = (Button) findViewById(R.id.signup);
TextView username = (TextView) findViewById(R.id.username);
Typeface custom_font = Typeface.createFromAsset(this.getAssets(), "fonts/smartwatch.ttf");
username.setTypeface(custom_font);
login.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent loginOnlyIntent = new Intent(view.getContext(), LoginOnlyActivity.class);
startActivity(loginOnlyIntent);
}
});
signup.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent signUpIntent = new Intent(view.getContext(), Signup.class);
startActivity(signUpIntent);
}
});
}
}
Ich erhalte die Fehlermeldung:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.LoginActivity}: java.lang.NullPointerException
I assests
Ordner im src/main
Ordner too.In der assests
Ordner fonts
Ordner hinzugefügt haben, in dem ich haben Schriftart-Datei smartwatch.tff
Ich habe fast jede Antwort auf SO überprüft, aber bisher hat noch nichts für mich funktioniert.
Update: Nach Debugger, fand ich, dass
TextView username = (TextView) findViewById(R.id.username);
kehrt null und damit der NPE.
Hinzufügen von Layout-Datei:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="155.0dip"
android:paddingTop="200.0dip"
android:paddingRight="4.0dip"
android:paddingBottom="1.0dip"
android:layout_below="@+id/TextView02">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UserName"
android:id="@+id/username"/>
<EditText
android:id="@+id/edit_phone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="number"
android:hint=" "/>
</LinearLayout>
Bitte hilf mir, warum Textview Benutzername null ist
Bitte mit der vollen logcat bearbeiten ... Nicht nur der Grund Ausnahme –
Ist diese XML-Datei 'activity_login.xml'? Wenn nicht, erklärt das, warum es null ist –
sind Sie das Aufblasen der richtigen Layout-Datei in Ihrer 'onCreate' Methode der Aktivität? Ich sehe keinen anderen Grund, warum 'TextView' möglicherweise null ist ... – AADProgramming