Android Stopwatch : Time in Milliseconds not working - java

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!

Related

I want to make a search bar for this code to search by buttons (between btn1 and btn2 and btn3) and hide the non-matched results

I am trying to add a search bar that enables me to search by buttons, search between btn1 and btn2 and btn3. I tried a lot of codes but all of them were focused on TextView and not on buttons. So hope to help me and if I missed details please let me know to describe more.
The two files (.java and .xml) are typed below. thank you.
file.java
package com.example.myapp1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
SearchView sv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("My First App");
Button btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}});
Button btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity3.class);
startActivity(intent);
}});
Button btn3 = findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity4.class);
startActivity(intent);
}});
List<Button> buttonList = new ArrayList<>();
buttonList.add(view.findViewById(R.id.btn1));
buttonList.add(view.findViewById(R.id.btn2));
buttonList.add(view.findViewById(R.id.btn3));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
for(int i = 0; i < buttonList.size(); i++) {
String buttonText = (String) buttonList.get(i).getText();
if(buttonText.toLowerCase(Locale.ROOT).equals(s.toLowerCase())) {
buttonList.get(i).setVisibility(View.VISIBLE);
} else {
buttonList.get(i).setVisibility(View.INVISIBLE);
}
}
return false;
}
#Override
public boolean onQueryTextChange(String s) {
if(s.isEmpty()) {
for(int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setVisibility(View.VISIBLE);
}
}
return false;
}
});
}
}
file.xml
<?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"
tools:context=".MainActivity"
tools:ignore="ExtraText"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="vertical"
android:weightSum="2">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:textSize="25dp"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:background="#drawable/gradient"
android:textColor="#color/white"
android:text="Absolute Pressure" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:textSize="25dp"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:background="#drawable/gradient"
android:textColor="#color/white"
android:text="Acoustic Flowmeter" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn3"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:textSize="25dp"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:background="#drawable/gradient"
android:textColor="#color/white"
android:text="Bernoulli Number" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
What do you mean by search by buttons? Are you looking to search the text that is held within the button?
Here is a way you can go about this:
Make a list that contains all the instances of your buttons.
List(findViewById(R.id.btn1), etc)
List<Button> buttonList = new ArrayList<>();
buttonList.add(view.findViewById(R.id.button_search1));
buttonList.add(view.findViewById(R.id.button_search2));
buttonList.add(view.findViewById(R.id.button_search3));
We are going to use the listeners available to the search view to do the work.
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// You can use this for when the submit is clicked
#Override
public boolean onQueryTextSubmit(String s) {
// We loop over the button list
for(int i = 0; i < buttonList.size(); i++) {
// We get the text of the button
String buttonText = (String) buttonList.get(i).getText();
// We turn both the button text and search result to lowercase
// So that results are not case sensitive
// If they match
if(buttonText.toLowerCase(Locale.ROOT).equals(s.toLowerCase())) {
// We make button visible
buttonList.get(i).setVisibility(View.VISIBLE);
} else {
// If it doesn't, we make invisible
buttonList.get(i).setVisibility(View.INVISIBLE);
}
}
return false;
}
// You can use this when the text is changing.
#Override
public boolean onQueryTextChange(String s) {
// If text is empty, reset results
if(s.isEmpty()) {
for(int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setVisibility(View.VISIBLE);
}
}
return false;
}
});

I can see the preview of the layout but when i run the app the layout is not showing

I have a layout with 3 buttons. I worked on the second and the third button. When i press the second button the layout changes but when i press the third button the layout changes but its an empty layout.
This is the layout that should show up when i press the button:
<android.support.constraint.ConstraintLayout
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.myappcompany.rob.basicphrases.Premium"
>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="56dp"
android:layout_marginTop="20dp"
android:fontFamily="serif"
android:text="PREMIUM SOUNDS"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="36sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="#+id/gridLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.GridLayout
android:id="#+id/gridLayout"
android:layout_width="413dp"
android:layout_height="614dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="56dp"
app:columnCount="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:rowCount="4">
<Button
android:id="#+id/button"
android:onClick="playPhrase"
android:tag="baiatuvreilepra"
android:text="Baiatu vrei lepra?"
android:textAllCaps="false"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_rowWeight="1" />
<Button
android:id="#+id/button1"
android:onClick="playPhrase"
android:tag="cristelnitapopii"
android:text="Baiat vrei lepra?"
android:textAllCaps="false"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_rowWeight="1" />
<Button
android:id="#+id/button2"
android:onClick="playPhrase"
android:tag="tagani"
android:text="Baiatuu vrei lepra?"
android:textAllCaps="false"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_rowWeight="1" />
<Button
android:id="#+id/button3"
android:onClick="playPhrase"
android:tag="tietisemaiscoalama"
android:text="Baia vrei lepra?"
android:textAllCaps="false"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_rowWeight="1" />
</android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>
but nothing show up , only an empty layout
This is the code of the layout that shows up emty:
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Premium extends AppCompatActivity {
public void playPhrase(View view){
Button buttonPressed = (Button) view;
Log.i("info", buttonPressed.getTag().toString());
MediaPlayer mediaPlayer = MediaPlayer.create(this, getResources().getIdentifier(buttonPressed.getTag().toString(), "raw", getPackageName()));
mediaPlayer.start();
}
}
And this is the code of the main file with the intent:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openActivity2();
}
public void openActivity2() {
Button button = (Button) findViewById(R.id.oldSchool);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Premium.class));
}
});
}
}
You are missing something important here! You are missing onCreate and setContentView.
public class Premium extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_premium_YOUR_LAYOUT_NAME);
}
public void playPhrase(View view){
Button buttonPressed = (Button) view;
Log.i("info", buttonPressed.getTag().toString());
MediaPlayer mediaPlayer = MediaPlayer.create(this, getResources().getIdentifier(buttonPressed.getTag().toString(), "raw", getPackageName()));
mediaPlayer.start();
}
}
Hope this helps. Feel free to ask for clarifications...

My android app stopping when I touch the button

This is my code guys when I touch the "saldir" button or another button my app
crashes and this is my app's image https://ibb.co/ce3ATm
This is my error (https://ibb.co/jBxb16):
Dergilik APP has stopped
package com.example.rg.myapplication;
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 implements
View.OnClickListener
{
int sayac =0;
TextView tv;
**//kind of turkish words don't focus the variables**
TextView tvkarekterOzellikleri;
Button byemekye;
#Override
**//THIS IS MY MAIN_Activity.Java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.Answer);
//I'm trying very basic app when I touch button this layout'll changed
tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
Button bsaldir = (Button) findViewById(R.id.saldir);
//comment/i have 3 buttons as you can see on link at top
Button buyu = (Button) findViewById(R.id.uyu);
Button byemekye = (Button) findViewById(R.id.yemekYe);
bsaldir.setOnClickListener(this);
buyu.setOnClickListener(this);
byemekye.setOnClickListener(this);*
//This part useless now because I didn't added this part later I'll add this part
//Character k = new Character();
//k.Movementnum = 10;
//k.Kilos = 10;
//k.FightPower = 10;
//}
#Override
//This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
public void onClick(View v){
if (v.getId()==byemekye.getId())
tv.setText("yemek yenildi");
else
tv.setText("Nar" + (sayac++));
}
}
When I delete if.(v.getId....) app is running normal.
This side is my 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.example.rg.myapplication.MainActivity">
<Button
android:id="#+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="#+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="#string/Dilektasi"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_above="#+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="#+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="#+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toLeftOf="#+id/saldir"
android:layout_toStartOf="#+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="#+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toEndOf="#+id/saldir"
android:layout_toRightOf="#+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
I think your problem is this:
Define Buttons:
private Button bt;
Set id of the button on xml, than on onCrate()
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
code:
public class YourActivity extends AppCompatActivity {
private Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
}
on xml
<Button
android:id="#+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

How Can I initialize my variables from another layout?

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.

Button click force closes the activity

My problem is when I click bSA button I encounter error and the activity closes.
java.lang.runtimeexception unable to start activity componentinfo ...
here is my code
Data.java :
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Data extends Activity implements View.OnClickListener {
Button start, startFor;
EditText sendET;
TextView gotAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
initialize();
}
private void initialize() {
start = (Button) findViewById(R.id.bSA);
startFor = (Button) findViewById(R.id.bSAFR);
sendET = (EditText) findViewById(R.id.etSend);
gotAnswer = (TextView) findViewById(R.id.tvGot);
start.setOnClickListener(this);
startFor.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bSA:
String bread = sendET.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
startActivity(a);
break;
case R.id.bSAFR:
break;
}
}
}
get.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etSend" android:layout_gravity="center_horizontal"/>
<Button
android:layout_below="#+id/etSend"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity"
android:id="#+id/bSA"/>
<Button
android:layout_toLeftOf="#+id/bSA"
android:layout_alignTop="#+id/bSA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity for Results"
android:id="#+id/bSAFR"/>
<TextView
android:layout_below="#+id/bSAFR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:id="#+id/tvGot"/>
</RelativeLayout>
=============================================================
OpenedClass.java
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
public class OpenedClass extends Activity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
TextView question, test;
Button returnData;
RadioGroup selectionList;
String gotBread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
initialize();
Bundle gotBasket = getIntent().getExtras();
gotBread = gotBasket.getString("key");
question.setText(gotBread);
}
private void initialize() {
question = (TextView) findViewById(R.id.tvQuestion);
test = (TextView) findViewById(R.id.tvText);
returnData = (Button) findViewById(R.id.bReturn);
returnData.setOnClickListener(this);
selectionList = (RadioGroup) findViewById(R.id.rgAnswers);
selectionList.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View view) {
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rCrazy:
break;
case R.id.rFun:
break;
case R.id.rBoth:
break;
}
}
}
send.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:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Hosein is ..."
android:id="#+id/tvQuestion"/>
<RadioGroup
android:id="#+id/rgAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crazy"
android:id="#+id/rCrazy"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Super Fun"
android:id="#+id/rFun"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Both"
android:id="#+id/rBoth"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return"
android:id="#+id/bReturn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tvText"/>
</LinearLayout>
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
You cannot put a inside itself. This causes infinite recursion and stack overflow.
You probably wanted
a.putExtras(basket);

Categories