Developing my first app for android. Works OK on emulator, but on phone I get a "Source not found error" ViewRoot.handleMessage(Message)line:1757. I get the error when I press button number 4 on the application and try to display my media images on phone.
Code:
package com.example.famiily_connect_v1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class family_connect extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button incrementButton, decrementButton, makecallButton,
media_cameraButton;
TextView numberdisplayTextView;
EditText inputEditText;
int A = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
incrementButton = (Button) findViewById(R.id.Button01);
decrementButton = (Button) findViewById(R.id.Button02);
makecallButton = (Button) findViewById(R.id.Button03);
media_cameraButton = (Button) findViewById(R.id.Button04);
numberdisplayTextView = (TextView) findViewById(R.id.TextView01);
inputEditText = (EditText) findViewById(R.id.EditText01);
incrementButton.setOnClickListener(this);
decrementButton.setOnClickListener(this);
makecallButton.setOnClickListener(this);
media_cameraButton.setOnClickListener(this);
numberdisplayTextView.setOnClickListener(this);
inputEditText.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.Button01:
A++;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button02:
A--;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button03:
break;
case R.id.Button04:
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivity(myIntent);
break;
default:
break;
}
}
}
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" android:background="#00AAAA">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="increment"></Button>
<Button android:id="#+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="decrement"></Button>
<Button android:id="#+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="make call"></Button>
<Button android:id="#+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="media_camera"></Button>
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="24sp"
android:text="0" android:textStyle="bold" android:textColor="#000000"></TextView>
<EditText android:id="#+id/EditText01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="enter new button text"></EditText>
<RadioButton android:id="#+id/RadioButton01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="camera"></RadioButton>
</LinearLayout>
Enable in your Eclipse the LogCat
Window > Show View > Other > Android >
LogCat
then you can find what is the real description of your Exception.
Related
I'm building a Contact app using ListView. My ListView has 2 button. In this app, to test the respond ability of the buttons, I intended to set the button "Edit" so that when I click on it, it will change to "Clicked", and the button "Delete" will change to "Clicked", too. When I ran the Debug, the app stopped working and I couldn't get it work again (before I added the onClickListener, this app had worked property).
I don't know what is the error, and have tried many ways to fix.
Here is my row_listview.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" android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:src="#drawable/if_male3_403019" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.7"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="Akai Shuichi"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="0982xxxxxx"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.3"
android:weightSum="2"
android:orientation="vertical">
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#3FE0FF"
android:onClick="myClickHandler"
android:text="Edit"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#F73131"
android:onClick="myClickHandler"
android:text="Delete"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
And here is my MainActivity.java:
package com.huy9515gmail.mycontact;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
#BindView(R.id.edt_inputName) EditText edtName;
#BindView(R.id.btnAdd) Button btnAdd;
#BindView(R.id.btnEdit) Button btnEdit;
#BindView(R.id.btnDelete) Button btnDelete;
#BindView(R.id.edt_inputNumber) EditText edtNumber;
#BindView(R.id.rdbtn_male) RadioButton rdbtn_male;
#BindView(R.id.rdbtn_female) RadioButton rdbtn_female;
#BindView(R.id.rdbtn_others) RadioButton rdbtn_others;
#BindView(R.id.gender) RadioGroup genderSelection;
private ListView lvContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
lvContact = (ListView) findViewById(R.id.lv_contact);
final ArrayList<Contact> arrContact = new ArrayList<>();
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//validating contact info
if (((edtName.getText().toString().trim()) == "") || (edtNumber.getText().toString().trim() == "") || ((rdbtn_male.isChecked()==false) && (rdbtn_female.isChecked()==false) &&(rdbtn_others.isChecked()==false))) {
Toast.makeText(MainActivity.this, "Invalid contact info! Please try again!", Toast.LENGTH_SHORT).show();
}
//adding contact info
else {
Contact contact = new Contact(Gender.male, "", "");
//adding info
contact.setName(edtName.getText().toString());
contact.setNumber(edtNumber.getText().toString());
arrContact.add(contact);
}
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, R.layout.row_listview, arrContact);
lvContact.setAdapter(customAdapter);
}
});
}
public void myClickHandler(View v) {
LinearLayout vwParentRow = (LinearLayout) v.getParent();
Button btnEdit = (Button) vwParentRow.getChildAt(0);
Button btnDelete = (Button) vwParentRow.getChildAt(1);
btnEdit.setText("Clicked");
btnDelete.setText("Clicked");
}
}
And here is the row_listview.xml layout:
Instead of setting the android:onClick in XML, do the same as you did for btnAdd.
Find the view by id or use butterknife, then put the following in onCreate()
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
Make sure you have an element in your XML file with the id like this:
android:id="#+id/btnAdd"
This is my working code for passing checked and unchecked checkbox value to another Activity:
This is First Activity image.
FIRST ACTIVITY Image
This is Second Activity Image.
SECOND ACTIVITY Image
1.This is First Activity xml file.
<?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:orientation="vertical"
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="com.example.himanshu.checkbox_module.FirstActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Activity"
android:textSize="20dp"
android:textColor="#f00"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday"
android:onClick="onCheckedBox"
android:id="#+id/mon"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tuesday"
android:onClick="onCheckedBox"
android:id="#+id/tue"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wednesday"
android:onClick="onCheckedBox"
android:id="#+id/wed"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Thursday"
android:onClick="onCheckedBox"
android:id="#+id/thu"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friday"
android:onClick="onCheckedBox"
android:id="#+id/fri"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Saturday"
android:onClick="onCheckedBox"
android:id="#+id/sat"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sunday"
android:onClick="onCheckedBox"
android:id="#+id/sun"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send to second activity"
android:layout_marginTop="20dp"
android:id="#+id/button1"/>
</LinearLayout>
2.This is First Activity java file.
package com.example.himanshu.checkbox_module;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextClock;
public class FirstActivity extends AppCompatActivity {
Intent intentData;
Button buttonSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentData = new Intent(FirstActivity.this,SecondActivity.class);
buttonSend = (Button) findViewById(R.id.button1);
buttonSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intentData);
}
});
}
public void onCheckedBox(View view){
boolean checked = ((CheckBox)view).isChecked();
switch (view.getId()){
case R.id.mon:if (checked){
intentData.putExtra("MON","Monday");
}else {
intentData.removeExtra("MON");
}break;
case R.id.tue:if (checked){
intentData.putExtra("TUE","Tuesday");
}else {
intentData.removeExtra("TUE");
}break;
case R.id.wed:if (checked){
intentData.putExtra("WED","Wednesday");
}else {
intentData.removeExtra("WED");
}break;
case R.id.thu:if (checked){
intentData.putExtra("THU","Thursday");
}else {
intentData.removeExtra("THU");
}break;
case R.id.fri:if (checked){
intentData.putExtra("FRI","Friday");
}else {
intentData.removeExtra("FRI");
}break;
case R.id.sat:if (checked){
intentData.putExtra("SAT","Saturday");
}else {
intentData.removeExtra("SAT");
}break;
case R.id.sun:if (checked){
intentData.putExtra("SUN","Sunday");
}else {
intentData.removeExtra("SUN");
}break;
default:break;
}
}
}
3.This is Second Activity xml file
<?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_secon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.example.himanshu.checkbox_module.SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="20dp"
android:textColor="#f00"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textview_result"
android:textColor="#000000"
android:textSize="15dp"
android:textAllCaps="true"
android:layout_marginTop="20dp"/>
</LinearLayout>
4.This is Second Activity java file.
package com.example.himanshu.checkbox_module;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView showResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secon);
showResult = (TextView) findViewById(R.id.textview_result);
Intent intentResult = this.getIntent();
String monday = intentResult.getStringExtra("MON");
String tuesday = intentResult.getStringExtra("TUE");
String wednesday = intentResult.getStringExtra("WED");
String thursday = intentResult.getStringExtra("THU");
String friday = intentResult.getStringExtra("FRI");
String saturday = intentResult.getStringExtra("SAT");
String sunday = intentResult.getStringExtra("SUN");
showResult.setText(monday+" , "+tuesday+" , "+wednesday+" , "+thursday+" , "+friday+", "+saturday+" , "+sunday);
}
}
boolean isChecked = myCheckBox.isChecked();
Intent i = new Intent(this, secondActivity.class);
i.putExtra("checkBoxValue", isChecked);
startActivity(i);
In your second activity onCreate
boolean isChecked = this.getIntent().getBooleanExtra("checkBoxValue", false);
I've asked a question similar to this. The point of this code is to check if a checklist is checked, if it is than it adds 1 to the score.
I've been told that the problem is that I haven't set an event. Please explain what I need to do to make it work, I'm new to android development and have been stuck on this for a few days. Please give a code example with your answer.
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
// Connects The variable to an xml id
TextView output = (TextView) findViewById(R.id.output);
final int[] score = {0};
//sets the variable to 0
OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.concern:
score[0]++;
break;
case R.id.faith:
score[0]++;
break;
case R.id.respect:
score[0]++;
break;
case R.id.education:
score[0]++;
break;
case R.id.community:
score[0]++;
break;
}
}
};
// adds the variables together to form a score
if(score[0] == 0){
output.setText("Come on! Get involved, your la sallian community needs you.");
} else if(score[0] == 1){
output.setText("Good start, keep going!");
} else if(score[0] == 2){
output.setText("Room to improve but doing good!");
} else if(score[0] == 3){
output.setText("Very good, others look up to you!");
} else if(score[0] == 4){
output.setText("Wow, you really are an inspiration");
} else if(score[0] == 5){
output.setText("Excellent! You're a leader in your la sallian community. Nice work!");
} else{
output.setText("Unknown");
}
// changes the output text based on score value
}
}
^^checklist code^^
package xyz.ashraf.whoisdelasalle;
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);
Button b = (Button) findViewById(R.id.who);
Button today = (Button) findViewById(R.id.today);
Button sallian = (Button) findViewById(R.id.sallian);
Button how = (Button) findViewById(R.id.toBe);
Button moreInfo = (Button) findViewById(R.id.info);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop.class));
}
});
today.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_today.class));
}
});
sallian.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_sallian.class));
}
});
how.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_how.class));
}
});
moreInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_info.class));
}
});
}
}
^^Code where the button is and, when pressed calls on the checklist code, The Ok button is to close the pop up when pressed^^
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
android:elevation="8dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you a Sallian?"
android:id="#+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you concerened for the poor and Social Justice?"
android:id="#+id/concern"
android:textSize="18sp"
android:layout_below="#+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have faith in the presence of God?"
android:id="#+id/faith"
android:textSize="15sp"
android:layout_below="#+id/concern"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have Respect for all people?"
android:id="#+id/respect"
android:layout_below="#+id/faith"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:textSize="15sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you value education?"
android:id="#+id/education"
android:textSize="15sp"
android:layout_below="#+id/respect"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you inclusive in your community?"
android:id="#+id/community"
android:layout_below="#+id/education"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15sp"
android:checked="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okButton_sallian"
android:layout_below="#+id/community"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="20dp"
android:background="#FAFAFA"
android:textColor="#00E676"
android:elevation="2dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/check"
android:textColor="#00E676"
android:elevation="2dp"
android:background="#FAFAFA"
android:layout_alignTop="#+id/okButton_sallian"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/output"
android:textColor="#1eff00"
android:textSize="23sp"
android:layout_below="#+id/community"
android:layout_centerHorizontal="true"
android:layout_above="#+id/check" />
</RelativeLayout>
</ScrollView>
^^ xml code the code takes for^^
You are defining a checked listener, but doing nothing with it. You need to set it to your checkbox or checkboxes.
mCheckBox = (CheckBox) findViewById(R.id.concern);
mCheckBox.setOnCheckedChangedListener(checkedListener);
Just like you do on your buttons.
My program opens with a login page, then moves to a page with two buttons (go to counter and go to scan barcode).
Very basic at this point, but I'm stuck because I press the scan barcode button and it moves to the new page fine, but if I press the 'go to counter' button (which goes to a chronometer class), the program crashes - even though there is a class made for it.
It gives me this error:
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
Here is my code
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class page1 extends AppCompatActivity implements View.OnClickListener {
Button scanner,go2count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
scanner = (Button) findViewById(R.id.scanner);
go2count = (Button) findViewById(R.id.go2count);
scanner.setOnClickListener(this);
go2count.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.scanner:
startActivity(new Intent(this, ScanPage.class));
break;
case R.id.go2count:
startActivity(new Intent(this, ChronoPage.class));
break;
}
}
So if I press the 'scanner' button, it works fine. But if I press the 'go2count' button, the program gives me the error mentioned.
This is the chronometer code:
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Chronometer;
import android.widget.Button;
public class ChronoPage extends AppCompatActivity implements View.OnClickListener {
private Button start;
private Button stop;
private Button resume;
private Button reset;
private long time = 0;
private Chronometer myChrono;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uI();
}
public void uI(){
start = (Button) findViewById(R.id.bStart);
start.setOnClickListener(this);
stop = (Button) findViewById(R.id.bStop);
stop.setOnClickListener(this);
resume = (Button) findViewById((R.id.bResume));
resume.setOnClickListener(this);
reset = (Button) findViewById(R.id.bReset);
reset.setOnClickListener(this);
myChrono = (Chronometer) findViewById(R.id.chronometer);
}
#Override
public void onClick(View v){
if (v == start){
myChrono.setBase(SystemClock.elapsedRealtime());
myChrono.start();
}
else if (v == stop){
time = SystemClock.elapsedRealtime();
myChrono.stop();
// printing();
}
else if (v == resume){
myChrono.setBase(myChrono.getBase() + SystemClock.elapsedRealtime() - time);
myChrono.start();
}
else if (v == reset){
myChrono.setBase(SystemClock.elapsedRealtime());
}
}
}
Another post said to try unchecking the "Use Host GPU" in the AVD manager, but that didn't solve the problem either.
Other posts have the same error, but are more specific to their code. All help is appreciated!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_width="match_parent">
<ImageView
android:id="#+id/logo1"
android:layout_marginTop="100dp"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/swordlogocopy"/>
<Button
android:id="#+id/scanner"
android:text="Scan Barcode"
android:textColor="#DDC004"
android:background="#000000"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<Button
android:id="#+id/go2count"
android:text="Go to counter"
android:textColor="#DDC004"
android:background="#000000"
android:layout_width="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_height="wrap_content" />
And the XML layout of the Chrono Page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_width="match_parent">
<ImageView
android:id="#+id/logo1"
android:layout_marginTop="100dp"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/swordlogocopy"/>
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:id="#+id/chronometer1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start"
android:layout_gravity="center_horizontal"
android:id="#+id/bStart"
android:textColor="#DDC004"
android:background="#000000"
android:layout_marginTop="50dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stop"
android:layout_gravity="center_horizontal"
android:textColor="#DDC004"
android:background="#000000"
android:layout_marginTop="5dp"
android:id="#+id/bStop" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/resume"
android:layout_gravity="center_horizontal"
android:textColor="#DDC004"
android:background="#000000"
android:layout_marginTop="5dp"
android:id="#+id/bResume" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset"
android:layout_gravity="center_horizontal"
android:textColor="#DDC004"
android:background="#000000"
android:layout_marginTop="5dp"
android:id="#+id/bReset" />
If you get this error, then most likely the findViewById(id) is not finding your Button variable in your xml to set the corresponding Button and therefore returning null. Which results in a NullpointerException when trying to set a OnClickListener on this view
Check if all Button Ids are valid
As my below codes shows that once button five and six shows with the titlesfourq1.
I click buttonfive it will show numten then if I click numten it will show numsix. But how do I show numsix with numten in the same layout?
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<AbsoluteLayout android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:background="#drawable/buttonsix" android:id="#+id/radio0"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="50dip" android:layout_x="20dip" android:layout_y="55dip">
</Button>
<Button android:background="#drawable/buttonfive" android:id="#+id/radio1"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="150dip" android:layout_x="200dip" android:layout_y="200dip">
</Button>
</AbsoluteLayout>
<ViewFlipper android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_x="20dip">
<!--adding views to ViewFlipper-->
<TextView
android:id="#+id/title"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center_vertical|center_horizontal"
android:text="#string/titlesfourq1" android:layout_height="wrap_content" android:layout_y="10dip" android:layout_x="5dip" android:textSize="20dip" android:layout_width="340dp"/>
<Button android:text="#string/numten" android:id="#+id/number" android:textStyle="bold" android:background="#color/translucent_black" android:textColor="#ffffff"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="150dip" android:layout_x="200dip" android:layout_y="200dip">
</Button>
<Button android:text="#string/numsix" android:id="#+id/number" android:textStyle="bold" android:background="#color/translucent_black" android:textColor="#ffffff"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="150dip" android:layout_x="200dip" android:layout_y="200dip">
</Button>
<TextView android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second view is now displayed"></TextView>
<TextView android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third view is now displayed"></TextView>
</ViewFlipper>
</AbsoluteLayout>
Java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class Sfourq1 extends Activity {
Button RB0;
Button RB1;
Button RB2;
ViewFlipper VF;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sfourq1);
/*
* Find the views declared in main.xml.
*/
RB0 = (Button) findViewById(R.id.radio0);
RB1 = (Button) findViewById(R.id.radio1);
RB2 = (Button) findViewById(R.id.number);
VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);
/*
* Set a listener that will listen for clicks on the radio buttons and
* perform suitable actions.
*/
RB0.setOnClickListener(button_one);
RB1.setOnClickListener(button_two);
RB2.setOnClickListener(button_test);
}
/*
* Define a OnClickListener that will change which view that is displayed by
* the ViewFlipper
*/
private OnClickListener button_one = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.radio0:
VF.setDisplayedChild(0);
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
break;
}
}
};
private OnClickListener button_two = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.radio1:
VF.setDisplayedChild(1);
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
break;
}
}
};
private OnClickListener button_test = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.number:
VF.setDisplayedChild(2);
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
Intent StartGameIntent = new Intent(Sfourq1.this,Home.class);
startActivity(StartGameIntent);
break;
}
}
};
}
for this you can give same tag to both buttons in different layout and whenever flip moves(can get by touch event)change focus to button with same tag.
thanx