For my class I have to develop a trivia game in Android Studio and I thought
I got it all figured out but now my app crashes as soon as I run it on the emulator. Can somebody tell me what's going on? It's saying i have a nullpointerException on line 24 (The line that says mQuestionTv.setText(R.string.question_text1);
This is what it says in logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.TextView.setText(int)' on a null object
reference
at edu.unf.n01044854.unftrivia.MainActivity.onCreate(MainActivity.java:24)
Here is my java code for the app:
package edu.unf.n01044854.unftrivia;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mQuestionTv;
private TextView mAnswerTv;
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private int mCurrentIndex = 0;
private int[] mQuestionBank = new int[] {R.string.question_text1,
R.string.question_text2,
R.string.question_text3};
private boolean[] mAnswerBank = new boolean[] {true, false, true};
#Override
protected void onCreate(Bundle savedInstanceState) {
mQuestionTv = (TextView)findViewById(R.id.question_textView);
mQuestionTv.setText(R.string.question_text1);
mAnswerTv = (TextView)findViewById(R.id.answer_textView);
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mAnswerBank[mCurrentIndex])
mAnswerTv.setText(R.string.correct_text);
else
mAnswerTv.setText(R.string.incorrect_text);
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!mAnswerBank[mCurrentIndex])
mAnswerTv.setText(R.string.correct_text);
else
mAnswerTv.setText(R.string.incorrect_text);
}
});
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mCurrentIndex == 3)
mCurrentIndex = 0;
else
mCurrentIndex++;
mQuestionTv.setText(mQuestionBank[mCurrentIndex]);
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Here is the xml code for my string resources:
<resources>
<string name="app_name">UNF Trivia</string>
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="next_button">Next</string>
<string name="answer_text">Correct/Incorret</string>
<string name="correct_text">Correct</string>
<string name="incorrect_text">Incorrect</string>
<string name="question_text1">Full-stage opera productions
are offered by student performers at UNF.</string>
<string name="question_text2">The armadillo was never
considered for the mascot adoption at UNF.</string>
<string name="question_text3">UNF offers Open Zip [Line] Nights for
students.</string>
</resources>
Main Xml layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/question_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
<Button
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next_button"/>
<TextView
android:id="#+id/answer_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/answer_text"/>
</LinearLayout>
Move these statements:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
before any other statement inside onCreate().
Any findViewById() method you use to initialize your views must be called after the layout is inflated otherwise the views are null.
Here is the documentation of On create from android. Note that If you override this method you must call through to the superclass implementation. which is done like this:
super.onCreate(savedInstanceState);
After this, you need to inflate your view using setContentView() method. which will set the activity content from a layout resource:
setContentView(R.layout.activity_main);
Now you will be able to retrieve the widgets in that UI (activity_main) which you need to interact with programmatically.
rivate int[] mQuestionBank = new int[] {R.string.question_text1,
R.string.question_text2, R.string.question_text3};
Resources are string. Typecast resources to integer.
Related
I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}
Re-Edit: For those interested the method for onClick was resolved below. Turns out you cannot create separate classes for controls so the button controls need to be inside the current active class.
The issue: In my fragment I have included several controls, one of which is a button that should open a new activity. When clicking the button I get the following crash...
This is the LogCat error when clicking the button in my current active Fragment
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paymentdemo, PID: 14352
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_next_trans'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
This is the fragment hosting the controls
<?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:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/transaction_header"
style="?textGroupheader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="20dp"
android:text="#string/group_header"
android:textStyle="bold" />
<!-- Spinner displays accounts available using SpinnerActivity -->
<include
layout="#layout/fragment_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Radio checks on or off. Still need to link selected as fragments for button on click behaviour -->
<include
layout="#layout/fragment_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Button will take selected radio and open related activity, for now we just go to MainActivity -->
<include
layout="#layout/fragment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The button control code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonActivity">
<Button
android:id="#+id/button_next_trans"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#drawable/action_button"
android:paddingStart="24dp"
android:paddingLeft="24dp"
android:paddingTop="8dp"
android:paddingEnd="24dp"
android:paddingRight="24dp"
android:paddingBottom="8dp"
android:text="#string/next"
android:textColor="#color/colorWhite"
android:onClick="goToPayments"/>
</FrameLayout>
The button activity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class ButtonActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_button);
}
public void goToPayments(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
I'm not experienced so my implementation may be a bit rusty. If you have any advice on how to properly implement a button onClick method I would be grateful.
You need to declare the button and View for framgment
Like this
Button button;
public View myView;
Then in onCreate initialize like this
myView = inflater.inflate(R.layout.fragment_button,container,false);
button = (Button) myView.findViewById(R.id.button_next_trans);
then create method like this this,
public void payments(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),MainActivity.class)
startActivity(i);
}
}
}
And at end of onCreate() method return view.
return myView;
And call payments(); in onCreate
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_button);
Button btnPay = (Button)findViewById(R.id.button_next_trans);
btnPay.setOnClickListener(this);
}
public void goToPayments(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
#Override
public void onClick(View v) {
{
switch (v.getId()) {
case R.id.button_next_trans:
goToPayments();
break;
}
}
}}
Remove below line from xml
android:onClick="goToPayments"
I have just started programming in Android and I couldn't get the result on clicking button that I expected. Here s the code.
.java file
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class AndroidLove extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_love, menu);
return true;
}
public void onButtonClicked(View view) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
}
.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".AndroidLove" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="104dp"
android:text="#string/hello_world"
android:visibility="invisible" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:text="#string/Button"
android:onClick="onButtonClicked" />
strings.xml file
<resources>
<string name="app_name">AndroidLove</string>
<string name="action_settings">Settings</string>
<string name="hello_world">First Line\nSecond Line\nThird Line</string>
<string name="Button">Get Lyrics</string>
On clicking the button, the following text should be displayed but its not displayed:-
First Line
Second Line
Third Line
Please help
Edit: Maybe and probably your binding is wrong about onclick, try below code instead of top. Put that code into your onCreate method.
Button button= (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
});
You need to get the string value from the strings.xml file which can be acheived by
String st =getResources.getString(R.string.hello_world);
Then set that to textview by
textView.setText(st);
Try this code:
public class AndroidLove extends Activity {
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
textView=(TextView)findViewById(R.id.textView1);
button1 = (Button)findViewByid(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// do your stuff here
String _data =getResources.getString(R.string.hello_world);
textView.setText(_data);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Can't figure out why, but I have no errors on eclipse, (simple classroom app - button creates new activity, sending in a textView and an imageView to the new activity) but as soon as I run my app and I press my button, the app crashes and I recieve this error on the LogCat
Logcat
02-12 17:13:26.297: E/AndroidRuntime(398): Caused by: java.lang.NullPointerException
02-12 17:13:26.297: E/AndroidRuntime(398): at edu.colum.iam.SecondPage.onCreate(SecondPage.java:38)
When I take that line out (and any other line that would affect it) the app runs fine, and it sends my textView over. Here is the .java I have, and I'll post the xml just incase as well.
IntentsActivity.java
public class IntentsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)this.findViewById(R.id.button1);
btn.setOnClickListener(myListener);
}
// Create an anonymous implementation of OnClickListener
View.OnClickListener myListener = new View.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(IntentsActivity.this, SecondPage.class);
String text = ((TextView)findViewById(R.id.textView1)).getText().toString();
ImageView Selection = ((ImageView) findViewById(R.id.imageView1));
myIntent.putExtra("Text", text);
myIntent.putExtra("img", R.drawable.icon);
or an image)
startActivity(myIntent);
//endclass
SecondPage.java
public class SecondPage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//get extras
Intent intent = this.getIntent();
Bundle b = intent.getExtras();
String text = b.getString("Text");
//show text
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(text);
//show image
ImageView image = (ImageView) findViewById(R.id.imageView1);
int resource = getIntent().getIntExtra("img", R.drawable.icon);
image.setImageDrawable(getResources().getDrawable(resource));
// ^ this is the line my logcat crashes as an error
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:text="#string/Button1" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:id="#+id/textView1" android:text="#string/FirstLayout"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"></TextView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:id="#+id/textView1"
android:text="#string/SecondLayout"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"></TextView>
<Button android:text="#string/Button2" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
According to the layout you posted, imageView1 is in your first xml not in second.xml which is the one you inflate in SecondPage.java so naturally it is null when you try to call a method on it.
You will need to add the ImageView to your second.xml.
You can have the same ImageViews in different layout files (I don't know why you would name them the same) and then access them from within your code as long as you have set the right layout inside onCreate or wherever you want to access those components. So, yes, just copy the ImageView to the second.xml and it should work.
Hi I'm new to Android Programming and I'm trying to make a simple program that changes the text by clicking a button. Here's my code:
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button_scan = (Button) findViewById(R.id.button_scan);
button_scan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonBeenPressed();
}
});
}
public void buttonBeenPressed(){
final Button button_scan = (Button) findViewById(R.id.button_scan);
TextView tv_barcode = (TextView)findViewById(R.id.textview_barcode);
if (tv_barcode != null){
tv_barcode.setText("been pressed.");
} else {
button_scan.setText("it's null dawg.");
}
}
}
And my 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:text="Scan" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/button_scan"></Button>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:id="#+id/textview_barcode"></TextView>
</LinearLayout>
However the TextView is returning NULL and i don't know why. Any suggestions? Thanks.
Hmmm i got no problems with your code, runs without any error! Are you working with eclipse? Then try to clean your project.
Looks all correct to me....
Your best bet to debug this may be to run your app on the emulator or a phone and run hierarchyviewer, find your TextView and check the id.
You can use textview or a button. Here is an example with a textview I've found from http://www.ahotbrew.com/android-textview-example/
<TextView
android:text="#string/textview_onclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview_onclick"
android:layout_below="#+id/textview_center"
android:textSize="25dp"
android:onClick="changeTextColor"
android:clickable="true"/>
In your MainActivity do this
public void changeTextColor (View view)
{
TextView textView = (TextView) view.findViewById(R.id.textview_onclick);
textView.setText("newWord");
}