After much research, I have been unable to figure out how I can initialize my TextView in my java file in Android Studio. The TextView in question is located in a different layout file so I don't know the correct syntax to use. I think my question is similar to: Null pointer Exception on .setOnClickListener
But the solution for him is not working for me.
Here is my troublesome code:
Microsoft = (Button) findViewById(R.id.Microsoft);
Microsoft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
//TextView score = (TextView) findViewById(R.id.score);
TextView score = (TextView)
score.findViewById(R.id.question2);
(score).setText(0);
}
});
TheFindViewById part is the part I need.
FULL CODE VVVV
package org.flinthill.finalprojectv3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button SuSe;
Button DOS;
Button B;
Button BIOS;
Button Microsoft;
Button LenBosackandSandyLerner;
Button HaskelDiklah;
Button SteveWozniak;
SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText(0);
}
});
DOS = (Button) findViewById(R.id.DOS);
DOS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
B = (Button) findViewById(R.id.B);
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
BIOS = (Button) findViewById(R.id.BIOS);
BIOS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Right!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("1");
setContentView(R.layout.question2);
}
});
//QUESTION 2
Microsoft = (Button) findViewById(R.id.Microsoft);
Microsoft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
//TextView score = (TextView) findViewById(R.id.score);
TextView score = (TextView)
score.findViewById(R.id.question2);
(score).setText(0);
}
});
/*LenBosackandSandyLerner = (Button)
findViewById(R.id.LenBosackandSandyLerner);
LenBosackandSandyLerner.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Right!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("2");
setContentView(R.layout.question3);
}
});
HaskelDiklah = (Button) findViewById(R.id.HaskelDiklah);
HaskelDiklah.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
SteveWozniak = (Button) findViewById(R.id.SteveWozniak);
SteveWozniak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});*/
}
}
XML CODE:
LAYOUT 1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="#+id/LUL"
android:textColor="#color/LightGreen"
android:layout_height="wrap_content"
android:typeface="serif"
android:text="Which is NOT an OS?"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textSize="24sp"/>
<Button
android:id="#+id/SuSe"
android:onClick="SuSeClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SuSe"
android:layout_marginTop="100dp"
android:layout_below="#+id/LUL"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/BIOS"
android:onClick="BIOSClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIOS"
android:layout_below="#+id/SuSe"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/DOS"
android:onClick="DOSClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOS"
android:layout_below="#+id/BIOS"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/B"
android:onClick="BClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_below="#+id/DOS"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/score"
android:textColor="#color/colorAccent"
android:text="0"
android:textSize="32dp"
android:layout_below="#+id/LUL"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
LAYOUT 2:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/question2"
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"
android:background="#drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="#+id/question2text"
android:textColor="#color/LightGreen"
android:layout_height="wrap_content"
android:typeface="serif"
android:text="Who created Cisco"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textSize="24sp"/>
<Button
android:id="#+id/Microsoft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Microsoft"
android:layout_marginTop="100dp"
android:layout_below="#+id/question2text"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/LenBosackandSandyLerner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Len Bosack and Sandy Lerner"
android:layout_below="#+id/Microsoft"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/HaskelDiklah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haskel Diklah"
android:layout_below="#+id/LenBosackandSandyLerner"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/SteveWozniak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Steve Wozniak"
android:layout_below="#+id/HaskelDiklah"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/score"
android:textColor="#color/colorAccent"
android:text="1"
android:textSize="32dp"
android:layout_below="#+id/question2text"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
//Change the name id in the xml file
Button microsoftButton = (Button) findViewById(R.id.microsoftButton);
TextView scoreTextView = (TextView) findViewById(R.id.scoreTextView);
microsoftButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Wrong!", Toast.LENGTH_SHORT).show();
// This was declared above so now you can use it. You can only set it to a String not to an Integer.
scoreTextView.setText("0");
}
});
You cannot do this unless your TextView is within the layout your Activity, Fragment or Dialog controls.
The findViewById method looks for Views within the layout previously configured by the setContentView(layout) or the layout inflated in your Fragment or Dialog. If it does not find anything, your TextView will have a null reference.
Related
I have tried to set an OnClickListener to my imageButton but it didn't work. I've seen other questions about the topic but none of the ansqers work for me. Am I doing something wrong?
MainActivity.java
public void Button()
{
View.OnClickListener listSet = new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
}
};
boton= (ImageButton) findViewById(R.id.imageButton7);
boton.setOnClickListener(listSet);
}
Activity_main.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"
tools:context="com.xxxx.xxx.MainActivity"
android:orientation="vertical"
android:weightSum="1"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
>
<WebView
android:id="#+id/webV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
</WebView>
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/background_light"
android:src="#drawable/ic_gear"
tools:background="#android:color/background_light"
tools:ignore="ContentDescription" />
</RelativeLayout>
Set clickable property of ImageButton to true
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:background="#android:color/background_light"
android:src="#drawable/ic_gear"
tools:background="#android:color/background_light"
tools:ignore="ContentDescription" />
Try this :
ImageButton ib = (ImageButton) ad.findViewById(R.id.imageButton7);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_SHORT).show();
}
});
try:
boton= (ImageButton) findViewById(R.id.imageButton7);
boton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(this, "settings", Toast.LENGTH_LONG).show();
}
};);
If i'm click OK how to choose pick a value one service in radio button but different value.
void OpenDialogService() {
closeLyt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
cancelTxt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
okTxt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
Bundle b = activity.getIntent().getExtras();
if(b == null)
b = new Bundle();
b.putString("service_id", "1");
startActivity(new Intent(activity, Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
}
});
dialog.show();
}
In this line for primary service :
b.putString("service_id", "1")
How i make conditional if service other give value :
b.putString("service_id", "2")
This is code snippet how you can achieve this with the help of RadioGroups
Your dialog layout should look like this :-
<LinearLayout 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:orientation="vertical"
tools:context="com.example.test.MainActivity" >
<TextView
android:id="#+id/headerTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#ff669900"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="Pick Service"
android:textColor="#FFF"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_margin="10dp"
android:layout_marginTop="32dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/primaryTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:checked="true"
android:text="Primary Service"
android:textSize="18sp" />
<TextView
android:id="#+id/primaryContentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/primaryTxt"
android:text=" -Dusting \n -Sweeping \n -Washing \n -Cleaning " />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="#+id/service_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/service_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" />
</RadioGroup>
<TextView
android:id="#+id/otherTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/primaryContentTxt"
android:layout_marginTop="10dp"
android:checked="true"
android:gravity="left"
android:text="Other Service(Soon)"
android:textSize="18sp" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/closeLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:weightSum="2" >
<TextView
android:id="#+id/cancelTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:padding="5dp"
android:text="CANCEL"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/okTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:padding="5dp"
android:text="OK"
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
And you can inflate this layout in custom dialog box and with the help of radio buttons you can choose service type
int serviceNumber = 0;
void OpenDialogService() {
final Dialog dialog = new Dialog(this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pop_new_order2);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
TextView headerTxt = (TextView) dialog.findViewById(R.id.headerTxt);
TextView okTxt = (TextView) dialog.findViewById(R.id.okTxt);
TextView cancelTxt = (TextView) dialog.findViewById(R.id.cancelTxt);
TextView primaryTxt = (TextView) dialog.findViewById(R.id.primaryTxt);
TextView otherTxt = (TextView) dialog.findViewById(R.id.otherTxt);
TextView primaryContentTxt = (TextView) dialog.findViewById(R.id.primaryContentTxt);
LinearLayout closeLyt = (LinearLayout) dialog.findViewById(R.id.closeLyt);
// Radio Buttons
final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = radio.findViewById(checkedId);
int index = radio.indexOfChild(radioButton);
Toast.makeText(getApplicationContext(), "service" +index, 500).show();
serviceNumber = index+1;
}
});
headerTxt.setText("Pick Services");
//TODO change color here
// headerTxt.setBackgroundResource(R.drawable.bg_dialog_header_success);
// closeLyt.setBackgroundResource(R.color.selector_close_alert_dialog_success);
closeLyt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
cancelTxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
okTxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Bundle b = getIntent().getExtras();
if (b == null)
b = new Bundle();
b.putString("service_id", String.valueOf(serviceNumber));
Toast.makeText(getApplicationContext(), "service " + String.valueOf(serviceNumber), 500).show();
//TODO Handle Activity Transition Here
// startActivity(new Intent(MainActivity.this,
// Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
// overridePendingTransition(R.anim.push_left_in,
// R.anim.push_left_out);
}
});
dialog.show();
}
Result
You can get the selected item id of RadioGroup using, radioGroup.getCheckedRadioButtonId() and use if-else to choose different strings.
int id = radioGroup.getCheckedRadioButtonId();
if(id == radioButton1)
{
b.putString("service_id", "1");
}
else if(id == radioButton1)
{
b.putString("service_id", "2");
}
I know, I know, there are tons of post on here about this particular problem, however, most of them are very outdated, and none of them comply with the way I wrote this app... I'm fairly new to coding, and I'm doing this on my own, so i need help to show me how to use what I have and get the chronometer to display milliseconds!
Here's my Java:
package com.jackson.eason.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends Activity {
Chronometer mChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get references to views
mChronometer = (Chronometer) findViewById(R.id.chronometer);
Button button1 = (Button) findViewById(R.id.start);
Button button2 = (Button) findViewById(R.id.stop);
Button button3 = (Button) findViewById(R.id.reset);
Button button4 = (Button) findViewById(R.id.set_format);
Button button5 = (Button) findViewById(R.id.clear_format);
// Declare the listeners
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase
(SystemClock.elapsedRealtime());
}
};
// You forgot to declare a listener for set format in your updated code
View.OnClickListener mSetFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat("Formatted time (%s)");
}
};
View.OnClickListener mClearFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat(null);
}
};
//Assign listeners to your buttons
button1.setOnClickListener(mStartListener);
button2.setOnClickListener(mStopListener);
button3.setOnClickListener(mResetListener);
button4.setOnClickListener(mSetFormatListener);
button5.setOnClickListener(mClearFormatListener);
}
}
Also, if needed, Here's my Main XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:background="#ff4f4a53">
<Chronometer android:id="#+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dip"
android:paddingTop="30dip"
android:focusableInTouchMode="true"
android:textSize="55sp"
android:textIsSelectable="false"
android:typeface="serif"
android:textColor="#fffcfffa" />
<Button android:id="#+id/start"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="start"
android:background="#ff5effab"
android:elegantTextHeight="false"
android:layout_gravity="center_horizontal">
<requestFocus />
</Button>
<Button android:id="#+id/stop"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="stop"
android:background="#fffe7c5b">
</Button>
<Button android:id="#+id/reset"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="reset"
android:layout_gravity="center_horizontal"
android:textColor="#fffffcfe">
</Button>
<Button android:id="#+id/clear_format"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="clear"
android:layout_gravity="center_horizontal"
android:textColor="#fffefdff">
</Button>
<Button android:id="#+id/set_format"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="format"
android:layout_gravity="center_horizontal"
android:textColor="#ffffffff">
</Button>
</LinearLayout>
Any help would be greatly appreciated!
I'm making a simple "novelty" app that counts how many sneezes are done, it's more of a fun app that builds up experience until I do my huge project during the summer. I have two buttons, one adds a sneeze and the other clears how many sneezes there currently are. It holds the highest number of sneezes that there were previously. The problem is, the TextViews never update, they only initialize to zero. I used a Toast.makeText() to make sure that the buttons are working (they are). Any help is appreciated. Thanks
Java code:
public class MainActivity extends ActionBarActivity {
private int record_number = 0;
private int current_number = 0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
Toast.makeText(MainActivity.this, "Button Clicked " + current_number, Toast.LENGTH_SHORT).show();
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
TextView rec_text = (TextView) findViewById(R.id.record_num);
TextView cur_text = (TextView) findViewById(R.id.current_num);
if (current_number >= record_number)
{
record_number = current_number;
}
rec_text.setText(String.valueOf(record_number));
cur_text.setText(String.valueOf(current_number));
}
}
XML:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.michail.sneezecounter.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sneeze Counter"
android:id="#+id/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Record Number of Sneezes:"
android:id="#+id/textView"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one Sneeze"
android:id="#+id/addone"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="76dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Current Number of Sneezes"
android:id="#+id/clear"
android:layout_marginBottom="13dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/record_num"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Current Number of Sneezes:"
android:id="#+id/currentLabel"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/currentLabel"
android:layout_alignLeft="#+id/record_num"
android:layout_alignStart="#+id/record_num"
android:layout_marginTop="21dp"
android:id="#+id/current_num" />
</RelativeLayout>
You need to update the text of the TextViews inside the onClickListeners. In fact, all your logic for counting, clearing, and recording the record needs to be done in your onClickListeners (or methods called by them). Right now you only do it once in onCreate, then never again. You can do this in onCreate:
final TextView cur_text = (TextView) findViewById(R.id.current_num);
add_one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
current_number += 1;
cur_text.setText(Integer.toString(current_number);
}
});
And similar for the other TextView & onClickListener.
You only set the contents of your TextViews once. You should update them every time you get a click event. Specifically:
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
{
record_number = current_number;
rec_text.setText(String.valueOf(record_number));
}
cur_text.setText(String.valueOf(current_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
}
});
NOTE: if your variables current_number, record_number, cur_text, and rec_text aren't already declared as class member variables, you'll want to do that do that so that they're accessible once you leave the scope of the method you're doing all this in (I assume it's onCreate(...).
What you are going to need to do here is update the labels during the on click events of the button. You currently only update them on activity create. This doesn't execute every time there is a click. Can I answer any questions about the fixed up version below?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
final TextView rec_text = (TextView) findViewById(R.id.record_num);
final TextView cur_text = (TextView) findViewById(R.id.current_num);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
record_number = current_number;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
I have a "Menu" imageview in all the pages of my app, if clicked on menu, all the menuitems like login, home, jobs, about will get open and If i click on "about" menuitem the app gets force close and getting null pointer exception on "OnClicklistener of menu" but other Menuitems are not having this issue, please can anybody solve this?
public class About extends Activity {
LinearLayout line1, line2;
ImageView menu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
menu.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
public void onClick(View v) {
menu.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});
ImageView home = (ImageView) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Home.class));
}
});
ImageView jobs = (ImageView) findViewById(R.id.jobs);
jobs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Jobs.class));
}
});
ImageView log = (ImageView) findViewById(R.id.log);
log.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Login.class));
}
});
ImageView about = (ImageView) findViewById(R.id.about);
about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(getApplicationContext(), About.class));
}
});
}
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:background="#color/black" >
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/menu" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_toRightOf="#+id/ll1"
android:visibility="gone"
>
<ImageView
android:id="#+id/about"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/jobs"
android:src="#drawable/about" />
<ImageView
android:id="#+id/jobs"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/jobs" />
<ImageView
android:id="#+id/log"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/log" />
<ImageView
android:id="#+id/home"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/home" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#color/white"
android:textColor="#color/white"/>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="710dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="46dp"
android:text="#string/AboutPage"
android:textColor="#color/white" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll1"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="#string/WelcometoRebuixcom"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
ImageView menu;
menu.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
Look at these code lines, the Second one is cause of Force Close, as menu is NULL.
Just interchange it like,
menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
Update:
You are declaring ImageView menu;
Now the second line, menu.setOnClickListener(new View.OnClickListener() {
without defining ImageView menu you are setting setOnCLickListener() to it, which cause NullPointerException.
So you have to define ImageView menu like menu = (ImageView)findViewById(R.id.menu); after declaring.
Just go through for basic Android Programming and Core Java tutorial for How to declare and define Objects and Member Variables. As I think you have poor programming concepts.
you should move the code
ImageView menu = (ImageView)findViewById(R.id.menu);
before
menu.setOnClickListener(new View.OnClickListener() {}
try this
ImageView menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {............
you should put
ImageView menu = (ImageView)findViewById(R.id.menu);
befor
menu.setOnClickListener
Initialize your imageview befor onclick listener as below:
ImageView menu = (ImageView)findViewById(R.id.menu); <<---- Here
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
menu.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});