简体   繁体   中英

Intent is undefined

So I get that my Intent is undefined, something to do with the constructor of this class, but I'm too new at Java to figure out the problem, maybe you can help?

serviceIntent = new Intent(this, myPlayService.class);

the code:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    podcastPlayerLayout = inflater.inflate(R.layout.activity_podcast_player, container, false);


    try {
        serviceIntent = new Intent(this, myPlayService.class);

        // --- set up seekbar intent for broadcasting new position to service ---
        intent = new Intent(BROADCAST_SEEKBAR);

        initViews();
        setListeners();
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(),
                e.getClass().getName() + " " + e.getMessage(),
                Toast.LENGTH_LONG).show();
    }



   //Adding Listener to button
     streamSetButton.setOnClickListener(new View.OnClickListener() {

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

            fileStreamAdress = streamSetButton.getText().toString();


            playPauseButtonClicked();
        }
    });

     playPauseButton.setOnClickListener(new View.OnClickListener() {

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

            fileStreamAdress = streamSetButton.getText().toString();


             playPauseButtonClicked();
         }


     });



    // Inflate the layout for this fragment
    return podcastPlayerLayout;


}

I'm guessing this is referring to the Adapter not a Context. Try:

serviceIntent = new Intent(getApplicationContext(), myPlayService.class);

or

serviceIntent = new Intent(MainActivity.this, myPlayService.class);
// Where MainActivity is the Activity class's name

The reason is Activity is a subclass of Context, while Adapters are not...

Intent is undefined

You will get these errors when variable is not defined in scope (or) type is not clear for compiler.

serviceIntent = new Intent(BROADCAST_SEEKBAR);

In this line you haven't type of the variable intent .

It should be something like

Intent serviceIntent = new Intent(BROADCAST_SEEKBAR);

Make sure you have:

import android.content.Intent;

among your imports, and the variable intent and serviceIntent declared as either class variables or local variables, like:

Intent intent, serviceIntent;

Also, make sure you're using the correct Context. In your case, this probably isn't what you're looking for. Try:

serviceIntent = new Intent(MyActivity.this, myPlayService.class); //OR
serviceIntent = new Intent(getBaseContext(), myPlayService.class); //OR
serviceIntent = new Intent(getApplicationContext(), myPlayService.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM