Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Updating tabs to new tab fragments

OP dfwcomputer

30th June 2014, 02:53 AM   |  #1  
OP Senior Member
Flag Brisbane
Thanks Meter: 44
 
108 posts
Join Date:Joined: Mar 2011
More
Hi all,

I had an app that was working about 3 years ago that I decided to update for the latest android sdk.I was using tabs but have decided to update to tab fragments with swipe.The problem I'm having is how to lay out the code.I have updated my app but have a few errors.Specifically the Book Now tab usually has a book now form so people can fill it out and upon clicking submit it sms's it to me.Below is the code for BookNowFragmnet.java.

I should mention that the tab and swipe part works flawlessly.I dont have a logcat because it wont build without removing the code

PHP Code:
package com.deano.dfw;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class 
BookNowFragment extends Fragment implements OnClickListener {
    
    
    
Button buttonSubmit;
    
EditText edittextPhone;
    
EditText editTextProblem;
    
    public 
View onCreateView(LayoutInflater inflaterViewGroup container,
            
Bundle savedInstanceState) {

        
View rootView inflater.inflate(R.layout.fragment_booknowcontainerfalse);
        
        
buttonSubmit = (ButtonrootView.findViewById(R.id.buttonSubmit);
        
edittextPhone = (EditTextrootView.findViewById(R.id.edittextPhone);
        
editTextProblem = (EditTextrootView.findViewById(R.id.editTextProblem);
        
        
buttonSubmit.setOnClickListener(new View.OnClickListener() {
            public 
void onClick(View v
            {                
                
String phoneNo edittextPhone.getText().toString();
                
String message editTextProblem.getText().toString();                 
                if (
phoneNo.length()>&& message.length()>0)                
                    
sendSMS(phoneNomessage);                
                else
                    
Toast.makeText(getBaseContext(), 
                        
"Please enter both phone number and message."
                        
Toast.LENGTH_SHORT).show();
            }
        });
        
        return 
rootView;
    }
    
    
    
    
// ---sends a SMS message to another device---
        
private void sendSMS(String phoneNumberString message) {

            
/*
             * PendingIntent pi = PendingIntent.getActivity(this, 0, new
             * Intent(this, test.class), 0); SmsManager sms =
             * SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null,
             * message, pi, null);
             */

            
String SENT "SMS_SENT";
            
// String DELIVERED = "SMS_DELIVERED";

            
PendingIntent sentPI PendingIntent.getBroadcast(this0, new Intent(
                    
SENT), 0);

            
/*
             * PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new
             * Intent(DELIVERED), 0);
             */

            // ---when the SMS has been sent---
            
registerReceiver(new BroadcastReceiver() {

                @
Override
                
public void onReceive(Context arg0Intent arg1) {
                    switch (
getResultCode()) {
                    case 
Activity.RESULT_OK:
                        
Toast.makeText(getBaseContext(), "Message Sent",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        
Toast.makeText(getBaseContext(), "Generic failure",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NO_SERVICE:
                        
Toast.makeText(getBaseContext(), "No service",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NULL_PDU:
                        
Toast.makeText(getBaseContext(), "Null PDU",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_RADIO_OFF:
                        
Toast.makeText(getBaseContext(), "Radio off",
                                
Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new 
IntentFilter(SENT));

            
SmsManager sms SmsManager.getDefault();

            
sms.sendTextMessage(phoneNumbernullmessagesentPInull);

        }
    @
Override
    
public void onClick(View v) {
        
// TODO Auto-generated method stub
        
    
}



The problem areas are as follows;


1. getBaseContext has the error "The method getBaseContext is undefined for the type new View.OnClickListener"
PHP Code:
Toast.makeText(getBaseContext(), 
                        
"Please enter both phone number and message."
                        
Toast.LENGTH_SHORT).show(); 
2. getBroadcast has the error "The method getBroadcast(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (BookNowFragment, int, Intent, int)"
PHP Code:
PendingIntent sentPI PendingIntent.getBroadcast(this0, new Intent(SENT), 0); 
3.registerReceiver has the error "The method registerReceiver(new BroadcastReceiver(){}, IntentFilter) is undefined for the type BookNowFragment"
PHP Code:
// ---when the SMS has been sent---
    
registerReceiver(new BroadcastReceiver() { 
4. all of the getBaseContext under the following have the error "The method getBaseContext() is undefined for the type new BroadcastReceiver"
PHP Code:
public void onReceive(Context arg0Intent arg1) {
                    switch (
getResultCode()) {
                    case 
Activity.RESULT_OK:
                        
Toast.makeText(getBaseContext(), "Message Sent",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        
Toast.makeText(getBaseContext(), "Generic failure",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NO_SERVICE:
                        
Toast.makeText(getBaseContext(), "No service",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NULL_PDU:
                        
Toast.makeText(getBaseContext(), "Null PDU",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_RADIO_OFF:
                        
Toast.makeText(getBaseContext(), "Radio off",
                                
Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new 
IntentFilter(SENT)); 

any help would be appreciated I have searched for the last two days trying different things.
30th June 2014, 03:54 AM   |  #2  
deanwray's Avatar
Senior Member
Thanks Meter: 429
 
1,145 posts
Join Date:Joined: Apr 2006
Donate to Me
Quote:
Originally Posted by dfwcomputer

1. getBaseContext has the error "The method getBaseContext is undefined for the type new View.OnClickListener"

PHP Code:
Toast.makeText(getBaseContext(), 
                        
"Please enter both phone number and message."
                        
Toast.LENGTH_SHORT).show(); 

So you are asking for the method "getBaseContext" within an onClickListener... it does not have a method called that... like it's telling you it's undefined
It would probably be a better idea to use application context either by direct use of getApplicationContext or by a "final" reference maybe...


Quote:
Originally Posted by dfwcomputer

2. getBroadcast has the error "The method getBroadcast(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (BookNowFragment, int, Intent, int)"

PHP Code:
PendingIntent sentPI PendingIntent.getBroadcast(this0, new Intent(SENT), 0); 

BookNowFragment is not extended from the base type of Context that is expected in the arguments.


Quote:
Originally Posted by dfwcomputer

3.registerReceiver has the error "The method registerReceiver(new BroadcastReceiver(){}, IntentFilter) is undefined for the type BookNowFragment"

PHP Code:
// ---when the SMS has been sent---
    
registerReceiver(new BroadcastReceiver() { 


Quote:
Originally Posted by dfwcomputer

4. all of the getBaseContext under the following have the error "The method getBaseContext() is undefined for the type new BroadcastReceiver"

PHP Code:
public void onReceive(Context arg0Intent arg1) {
                    switch (
getResultCode()) {
                    case 
Activity.RESULT_OK:
                        
Toast.makeText(getBaseContext(), "Message Sent",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        
Toast.makeText(getBaseContext(), "Generic failure",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NO_SERVICE:
                        
Toast.makeText(getBaseContext(), "No service",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NULL_PDU:
                        
Toast.makeText(getBaseContext(), "Null PDU",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_RADIO_OFF:
                        
Toast.makeText(getBaseContext(), "Radio off",
                                
Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new 
IntentFilter(SENT)); 

any help would be appreciated I have searched for the last two days trying different things.


Actually the last to are just repeats of the previous really... I'm guessing here but it seems you may have skipped ahead of where you should be in a learning sense.. cause it seems to be like your running without being knowing how to jog kinda thing. Not attempting to be condescending, just I'm a trainer and always attempt to point this stuff out when I see it

Maybe go back and do some of the basics again, and include fragments etc
30th June 2014, 04:42 AM   |  #3  
OP Senior Member
Flag Brisbane
Thanks Meter: 44
 
108 posts
Join Date:Joined: Mar 2011
More
thanks, Im well aware i'm running before im born but this is the only app I intend to write as it's specifically for my business.
30th June 2014, 04:48 AM   |  #4  
deanwray's Avatar
Senior Member
Thanks Meter: 429
 
1,145 posts
Join Date:Joined: Apr 2006
Donate to Me
Quote:
Originally Posted by dfwcomputer

thanks, Im well aware i'm running before im born but this is the only app I intend to write as it's specifically for my business.

hmmm, well if thats all thats wrong with it, all I could offer is for you to send me the packaged src+res and I will have a look... if it takes less than 20 min to fix and get running I have no problem fixing it for you. If it doesn't and maybe takes longer then I may have to leave it. Should be quick though, up to you

But with the info I gave you should be able to fix those 4 issues...if not hit me up on hangouts and will talk you though it
Last edited by deanwray; 30th June 2014 at 04:50 AM.
The Following User Says Thank You to deanwray For This Useful Post: [ View ]
30th June 2014, 05:00 AM   |  #5  
OP Senior Member
Flag Brisbane
Thanks Meter: 44
 
108 posts
Join Date:Joined: Mar 2011
More
thanks m8.Ill play around today but if I cant figure it out I might get you to have a look if time permits.I develop in eclipse.Is it a matter of just zipping up the project folder?
30th June 2014, 05:06 AM   |  #6  
deanwray's Avatar
Senior Member
Thanks Meter: 429
 
1,145 posts
Join Date:Joined: Apr 2006
Donate to Me
Quote:
Originally Posted by dfwcomputer

thanks m8.Ill play around today but if I cant figure it out I might get you to have a look if time permits.I develop in eclipse.Is it a matter of just zipping up the project folder?

Yeah, just zip the project.

But like I say just get a reference to the activity to use as the "context" part of the argument, either where you need it or as a final reference or member variable

so getActivity()

or memberVar

Context mContext = null;
(in onCreate) mContext = getActivity();


or as final

final Context c = getActivity();
The Following User Says Thank You to deanwray For This Useful Post: [ View ]
30th June 2014, 05:58 AM   |  #7  
OP Senior Member
Flag Brisbane
Thanks Meter: 44
 
108 posts
Join Date:Joined: Mar 2011
More
Quote:
Originally Posted by deanwray

Yeah, just zip the project.

But like I say just get a reference to the activity to use as the "context" part of the argument, either where you need it or as a final reference or member variable

so getActivity()

or memberVar

Context mContext = null;
(in onCreate) mContext = getActivity();


or as final

final Context c = getActivity();

lol now I have less faith ill be fixing it..... Ill let you know thanks again
1st July 2014, 03:41 AM   |  #8  
OP Senior Member
Flag Brisbane
Thanks Meter: 44
 
108 posts
Join Date:Joined: Mar 2011
More
sent you a private message m8, because there is some personal info in the app
1st July 2014, 06:11 AM   |  #9  
deanwray's Avatar
Senior Member
Thanks Meter: 429
 
1,145 posts
Join Date:Joined: Apr 2006
Donate to Me
Quote:
Originally Posted by dfwcomputer

sent you a private message m8, because there is some personal info in the app

fixed and sent private msg with class
The Following User Says Thank You to deanwray For This Useful Post: [ View ]
1st July 2014, 06:28 AM   |  #10  
OP Senior Member
Flag Brisbane
Thanks Meter: 44
 
108 posts
Join Date:Joined: Mar 2011
More
Thanks again, I apreciate it.I have changed the personal info and posted the working code here incase someone else has a similar issue.

PHP Code:
package com.deano.dfw;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class 
BookNowFragment extends Fragment implements OnClickListener {
    
    
    
Button buttonSubmit;
    
EditText edittextPhone;
    
EditText editTextProblem;
    
    public 
View onCreateView(LayoutInflater inflaterViewGroup container,
            
Bundle savedInstanceState) {

        final 
View rootView inflater.inflate(R.layout.fragment_booknowcontainerfalse);
        
        
buttonSubmit = (ButtonrootView.findViewById(R.id.buttonSubmit);
        
//edittextPhone = (EditText) rootView.findViewById(R.id.edittextPhone);
        //editTextProblem = (EditText) rootView.findViewById(R.id.editTextProblem);
        
        
buttonSubmit.setOnClickListener(new View.OnClickListener() {
            public 
void onClick(View v
            {                
                
String strPhoneNo "0000000000";
                
TextView txtName = (TextViewrootView.findViewById(R.id.edittextName);
                
TextView txtPhone = (TextViewrootView.findViewById(R.id.edittextPhone);
                
TextView txtProblem = (TextViewrootView.findViewById(R.id.editTextProblem);
                
String strName "Name: " txtName.getText().toString();
                
String strPhone "Phone: " txtPhone.getText().toString();
                
String strProblem "Problem: "
                        
txtProblem.getText().toString();
                
String strMessage strName "\n" strPhone "\n"
                        
strProblem;

                
BookNowSMS(strPhoneNostrMessage);
            }
        });
        
        return 
rootView;
    }
    
    
    
    
// ---sends a SMS message to another device---
        
private void BookNowSMS(String phoneNumberString message) {

            
/*
             * PendingIntent pi = PendingIntent.getActivity(this, 0, new
             * Intent(this, test.class), 0); SmsManager sms =
             * SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null,
             * message, pi, null);
             */

            
String SENT "SMS_SENT";
            
// String DELIVERED = "SMS_DELIVERED";

            
PendingIntent sentPI PendingIntent.getBroadcast(getActivity(), 0, new Intent(
                    
SENT), 0);

            
/*
             * PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new
             * Intent(DELIVERED), 0);
             */

            // ---when the SMS has been sent---
            
getActivity().registerReceiver(new BroadcastReceiver() {

                @
Override
                
public void onReceive(Context arg0Intent arg1) {
                    switch (
getResultCode()) {
                    case 
Activity.RESULT_OK:
                        
Toast.makeText(getActivity(), "Message Sent",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        
Toast.makeText(getActivity(), "Generic failure",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NO_SERVICE:
                        
Toast.makeText(getActivity(), "No service",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_NULL_PDU:
                        
Toast.makeText(getActivity(), "Null PDU",
                                
Toast.LENGTH_SHORT).show();
                        break;

                    case 
SmsManager.RESULT_ERROR_RADIO_OFF:
                        
Toast.makeText(getActivity(), "Radio off",
                                
Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new 
IntentFilter(SENT));

            
SmsManager sms SmsManager.getDefault();

            
sms.sendTextMessage(phoneNumbernullmessagesentPInull);

        }
    @
Override
    
public void onClick(View v) {
        
// TODO Auto-generated method stub
        
    
}



Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes