I am trying to add custom fonts in my android app.
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);
}
});
}
}
I am getting the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.LoginActivity}: java.lang.NullPointerException
I have added assests folder in the src/main folder too.In the assests folder is fonts folder in which I have font file smartwatch.tff
I have checked almost every answer available on SO but nothing worked so far for me.
update:
After using Debugger, I found that
TextView username = (TextView) findViewById(R.id.username);
returns null and hence the NPE.
Adding layout file :
<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>
Please help me why textview username is null
Your code works fine for me.
In LinearLayout you provided missing buttons, but I think you have added them in your project. In addition, I have error because you have omitted the XML namespace
xmlns:android="http://schemas.android.com/apk/res/android"
into Linear Layout.
Try to Invalidate and restart project, It 's strange the null pointer problem on TextView, You've done all right, unless the problem lies elsewhere.
Make sure you have created the Assets folder doing:
Right click on "app" folder, new --> Folder --> Assets Folder
Related
Brand new to android. I have a button that is supposed to trigger an onClick method that is an override of the onClick method used with the onClickListener. The problem is that when I press the button an IllegalStateException is thrown saying that the onClick(MainActivity), as defined in the XML of the button, method cannot be found.
The code below is the code that throws the IllegalStateException. However if I change the XML of the button to android:onClick="onClick" then the button works completely fine. Why is this?
Button XML:
<Button
android:text="Accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:id="#+id/button2"
android:onClick="onClick (MainActivity)" />
MainActivity Code:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener{
private TextView textView;
private EditText username;
private EditText password;
private Button accept;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView3);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
accept = (Button) findViewById(R.id.button2);
}
public void logIn(View view)
{
Log.i("testUsername", username.getText().toString());
Log.i("testPassword", password.getText().toString());
}
#Override
public void onClick(View view)
{
if((Button)view == accept)
{
Log.i("testUsername", username.getText().toString());
Log.i("testPassword", password.getText().toString());
}
}
}
Why is this?
Because android:onClick="onClick" is correct syntax. None of the following are correct syntax:
android:onClick="onClick (MainActivity)"
android:onClick="onClick (View)"
android:onClick="onClick(View)"
android:onClick="onClick and anything else"
Quoting the documentation for android:onClick:
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).
OK, with the help of #HappyRavi on Twitter, I was able to reproduce the problem. It's an IDE bug that you will encounter if you do things in a certain order:
Add the method that you want to route the click event to
Drag the Button into the GUI editor
Click the onClick drop-down in the Properties pane of the GUI editor
Doing things in a different order will not reproduce the problem.
With luck, they can get this fixed in some patch release for Android Studio 2.3.
It should be implemented like below
<Button
android:text="Accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:id="#+id/button2"
android:onClick="buttonClickHandler" />//name of method should be given here
And add the method in Activity file
public void buttonClickHandler(View view) {
//handle button click here
}
This will work only when method is added in Activity class and not with Fragment
I'm trying to use the findViewById() method to to assign an action to a button I created, however it is giving me the error:
findViewById(int) in android.support.v7.app.AppCompatActivity cannot be applied to (android.widget.Button)
at the lines:
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
My code is as follows:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class UserTypeSelection extends AppCompatActivity implements View.OnClickListener{
private Button buttonStudentAccess;
private Button buttonGuestAccess;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_type_selection);
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
buttonStudentAccess.setOnClickListener(this);
buttonGuestAccess.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view == buttonStudentAccess) {
}
if (view == buttonGuestAccess) {
//startActivity(new Intent(this, MainActivity.class));
}
}
The corresponding xml file contains the buttons
<Button
android:text="Guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonStudentAccess"
android:layout_alignLeft="#+id/buttonStudentAccess"
android:layout_alignStart="#+id/buttonStudentAccess"
android:layout_marginTop="30dp"
android:id="#+id/buttonGuestAccess"
android:layout_alignRight="#+id/buttonStudentAccess"
android:layout_alignEnd="#+id/buttonStudentAccess" />
<Button
android:text="Student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/buttonStudentAccess"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true" />
I used the same syntax to create and assign actions to buttons in my login and registration classes and it didn't create this error. Any help is appreciated, thank you
You need to specify where you're getting the id "buttonStudentAccess" from. so here's how you can find the view:
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
To specify that the button or any other view is located in the "resources" package you refer to it as R and .id specifies that you want to grab the view using its id.
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(R.id.buttonGuestAccess);
I surdenly noticed that My project starts throwing error anytime I try to access a resources that is a button. It underlines R.id.button. I dont understand why. I even deleted the last xml that I created but problem persist.
This is an example of my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/layoutborder"
android:orientation="vertical" >
<TextView
android:id="#+id/chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/stepone"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/wine" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="#drawable/ai" />
<Button
android:id="#+id/drugdetails"
style="#style/smallButtonStyleBlackpearl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="#string/nextbut" />
</LinearLayout>
My Java code
package com.example.rhemahealthcare;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.actionbarsherlock.app.SherlockActivity;
import com.example.rhemahealthcare.R;
public class SteponeActivity extends SherlockActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.steponeactivity);
final Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent intent = new Intent(SteponeActivity.this,SteptwoActivity.class);
startActivity(intent);
}
});
}
}
i think you change any button1 id buy clicking right click and choose edit id. this option changes all the ids with that name in all the layouts.
As #Aleks G gussed it right in the comment, you don't have any button with id as button1 in your xml file. You've mentioned it :
final Button button = (Button)findViewById(R.id.button1);
Use the appropriate ID or put one in your layout file.
I have figured out the problem. My button ids were automatically change to button1 so they did not reference their previous ids that I gave to them. Thanks alot
Okay so I'm working on a project and I'm trying to use buttons with text inside of them in an activity, but eclipse can't find the id or chooser.xml
Here's myActivity class
package com.xxxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.*;
public class MyActivity extends Activity {`
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//chooser is underlined (chooser cannot be resolved or is not a field)
setContentView(R.layout.chooser);
//all ids are underlined(id cannot be resolved or is not a field)
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button4);
mButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity2.class);
startActivity(myIntent);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activty3.class);
startActivity(myIntent);
}
});
mButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity4.class);
startActivity(myIntent);
}
});
mButton4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity5.class);
startActivity(myIntent);
}
});
}
}
Here's the chooser.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_1" />
<Button
android:text="#string/button_2"
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_3"
/>
<Button
android:id="#+id/button_4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_4"
/>
</LinearLayout>
And here's the string resource xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test App</string>
<string name="test_image">test.png</string>
<string name="button_1">button_1</string>
<string name="button_2">button_2</string>
<string name="button_3">button_3</string>
<string name="button_4">button_4</string>
</resources>
Your gen files most likely aren't generated yet. Try building it, it should make the lines go away.
1) You need to import your R class. import <android package>.R Ref: R.id cannot be resolved AND R cannot be resolved - Android error
2) Aren't your R.id.button1 supposed to be R.id.button_1 and so forth for other buttons?
Is chooser.xml in the correct folder (the default layout folder provided with the project)? Have you perhaps tried to go to Project and choose Clean? Everything looks okay in your code.
I know this is a very old post, though I still stumbled upon it in my search for a similar problem. When I built the android project, it automatically added:
import android.R;
When I used the advice above,
import <android package>.R;
and I still used R.id....., it still propagated an error. I removed
import android.R;
and no errors.
Ok this may seem like a pointless example but if I can figure this out then the program I am trying to make will work. So I have two activities test and test two each with one button.
Test 1:
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
and test2
package thompson.cameron.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test2 extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
View test = findViewById(R.id.testButton);
test.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.testButton:
System.exit(1);
}
}
}
When I click the button on Test it is supposed to launch test2 however it is at this point I get an null pointer exception that I have narrowed down to test.setOnClickListener(this); line of code. Below are my two xml files for the layout. I can get the button to work when I only have one activity but as soon as I add a second activity with a different layout file it all falls apart
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
I'm still new at Android programming so thanks for all your help.
Open the Debug perspective in Eclipse
Choose the 'Breakpoints' view tab.
Select the 'Add Java Exception Breakpoint' and choose
NullPointerException.
Launch your activity, either by using 'Debug As...' or attaching
the debugger to a running instance via DDMS.
Execute the offending workflow. It will break on the line that
caused the NullPointerException.
In your test.java file give:
implements View.OnClickListener
Initialize your button as:
Button testButton = (Button) findViewById(R.id.testButton);
and inside your onClick method, check whether you are clicking button:
if(v == testButton) {
//give ur intent code
}
There are different ways to perform onClick functionality.
One is the above method which I have mentioned.
Another one is what ankit has mentioned.
Third way is through your layout.
Inside your layout for your button tag, you may give as:
<Button android:id="#+id/testButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Click" android:onClick="onTestButtonClick" />
And inside your class just mention the below details for button:
public void onTestButtonClick(View view) {
//give your intent code
}
You may refer to the link also:
http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html
Make sure that both activities are register at the application's manifest file.
As a side note never call System.exit in your code. You can call finish() to close an Activity and this will bring at the front the previous Activity on the stack.
The issue here is that you haven't typecasted your views to buttons.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; // Needed to add this import for the button casting below
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// I have changed View to Button and then typecasted
// with the "(Button)" the return of findViewById
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
Let me know if you have any issues with this. I just completed my first experiment through using the onClickListener implementation through the main class instead of individual anonymous listeners.
Andrew
I had the same problem, you may put the same content view that the button,
setContentView(R.layout.main); if the button is in that content view, in other case, you will put:
setContentView(R.layout.buttoncontentview);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
setContentView(R.layout.main);
sorry for my bad english, but i'm spanish
Implement OnClickListener interface
and set button.setOnClickListener(this);
and override
public void onClick(View v) {
}
I think your buttons IDs need to be different in different activities. R.id.testButton would refer to only one button.
The final solution is that you may modify the AndroidManifest.xml file, i finally solved my error in this link How to register a new activity in AndroidManifest.xml?
You can try this.it may work.
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity{
private Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i=new Intent().setClass(Test.this,Test2.class);
startActivity(i);
}
});
}
}
First of all in main.xml and main2.xml chage the button's ids like in below code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
It throws nullpointerexception because of id confict with each other so in your java file use following code to find button.
In Activity 1
Button button = findViewById(R.id.testButton);
button.setOnClickListener(this);
and
In Activity 2
Button button = findViewById(R.id.testButton1);
button.setOnClickListener(this);