Hey guys. I have a simple question about my android app. I know what you are thinking " this dumbass just posted this in the wrong thread". But I did it for a reason. The error that I have is related to java...not android.
(I will star every line of code that I talk about)
So here is the problem: I get a NullPointerException at line 75 in the MainActivity class. So what I do is look at that line. It reads:
if(number.length() == 0){
So what I'm thinking is that I forgot to set the value of number. Then I look right above that and it says:
SettingsActivity settings = new SettingsActivity();
number = settings.phone_number;
, and I find that I assigned the variable to phone_number, which if you are smart enough to figure out is a string with a phone number inside of it. At this point I don't know what to do. I made sure everything was public, but I'm sure it has to do with the placement of variables inside brackets but I am just not seeing it.
Here are the MainActivity and SettingsActivity classes:
thanks in advance for anything that you guys do
package com.bobrown101.gpsbuddy;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.telephony.SmsManager;
public class MainActivity extends Activity {
public String Lat;
public String Long;
boolean toggleBtn = false;
public int x = 7200;
public long y;
public String z;
public String phone_number;
boolean bool = false;
public TextView t;
public TextView s;
public TextView state;
public EditText edittext;
public String number;
public String notif;
public String actual_number;
public Button buttonOn;
public Button buttonOff;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button)findViewById(R.id.button1);
buttonOn = (Button)findViewById(R.id.button4);
buttonOff = (Button)findViewById(R.id.button5);
edittext = (EditText)findViewById(R.id.editText1);
buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above
t=new TextView(this);
s=new TextView(this);
state=new TextView(this);
t=(TextView)findViewById(R.id.textView3);
s=(TextView)findViewById(R.id.textView7);
state=(TextView)findViewById(R.id.textView6);
buttonOff.setEnabled(false);
}
public OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
SettingsActivity settings = new SettingsActivity();
number = settings.phone_number;
if(number.length() == 0){
//Toast.makeText(getBaseContext(), "Please check to make sure you have chosen the recipients contact in the settings menu", Toast.LENGTH_LONG).show();
System.out.println("here is the phone number that you were trying to send the message to: " + number);
}else{
doEverything();
Toast.makeText(getBaseContext(), "Manual message SENT!", Toast.LENGTH_SHORT).show();
}
}
};
public void button_on(View v) {
if(!bool){
Toast.makeText(getBaseContext(), "Please input a phone number, then press submit.", Toast.LENGTH_SHORT).show();
}else{
state.setText("On ");
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
System.out.println(x);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), x*1000, pintent); //7200 is 2 hours
//toggleBtn = true;
//System.out.println("On button pressed. Value of toggleBtn: " + toggleBtn);
Toast.makeText(getBaseContext(), "Service is now ON!", Toast.LENGTH_SHORT).show();
//Toast.makeText(getBaseContext(), String.valueOf(toggleBtn), Toast.LENGTH_SHORT).show();
s.setText("");
buttonOn.setEnabled(false);
buttonOff.setEnabled(true);
}
}
public void button_off(View v) {
MyService service = new MyService();
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
Toast.makeText(getBaseContext(), "Service is now OFF!", Toast.LENGTH_SHORT).show();
state.setText("Off");
service.counter = 0;
buttonOn.setEnabled(true);
buttonOff.setEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendSMS(String x) {
String phoneNumber = number;
String message = x;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}
public void getLocation(){
GPSTracker mGPS = new GPSTracker(this);
if(mGPS.canGetLocation ){
double mLat=mGPS.getLatitude();
double mLong=mGPS.getLongitude();
Lat = Double.toString(mLat);
Long = Double.toString(mLong);
}else{
// can't get the location
}
}
public void doEverything(){
getLocation();
String text = Lat + "," + Long;
sendSMS(text);
}
public void time_up(View v){ //stops the alarm and ads 30 minutes to the timer
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
System.out.println(x);
alarm.cancel(pintent);
//alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), x*1000, pintent); //7200 is 2 hours
x = x +3600;
y = x/3600;
z = y + " hours";
t.setText(z);
notif = s.getText().toString();
if(!(notif == "The Service was turned off because you adjusted the time. Please press 'on' to restart")){
Toast.makeText(getBaseContext(), "Service must be stopped to adjust time. If you would like GPS Buddy to restart, please pres 'ON' now", Toast.LENGTH_LONG).show();
s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
}else{
s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
}
}
public void time_down(View v){ //stops the alarm and subtracts 30 minutes to the timer
if(x == 3600){
x = 3600;
Toast.makeText(getBaseContext(), "I'm sorry. The lowest interval is 1 hour.", Toast.LENGTH_LONG).show();
}else{
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
System.out.println(x);
alarm.cancel(pintent);
x = x -3600;
y = x/3600;
z = y + " hours";
t.setText(z);
notif = s.getText().toString();
if(!(notif == "The Service was turned off because you adjusted the time. Please press 'on' to restart")){
Toast.makeText(getBaseContext(), "Service must be stopped to adjust time. If you would like GPS Buddy to restart, please pres 'ON' now", Toast.LENGTH_LONG).show();
s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
}else{
s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
}
}
}
public void number_submit(View v){
//number = edittext.getText().toString();
SettingsActivity settings = new SettingsActivity();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
Intent myIntent = new Intent(MainActivity.this, SettingsActivity.class);
//myIntent.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(myIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
here is the other
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;;
public class SettingsActivity extends Activity {
String DEBUG_TAG = "CONTACT CHOOSER ERROR: ";
private static final int CONTACT_PICKER_RESULT = 1001;
public String phone_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void button_submit(View v){
doLaunchContactPicker(v);
}
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything phone number
cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
phone_number = cursor.getString(phoneIdx);
Log.w(DEBUG_TAG, phone_number);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get phone data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText phoneEntry = (EditText) findViewById(R.id.editText1);
phoneEntry.setText(phone_number);
if (phone_number.length() == 0) {
Toast.makeText(this, "No phone number found for contact.", Toast.LENGTH_LONG).show();
}else{
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
}