2013-01-16 47 views
0

所以我得到我的Intent是未定义的,与这个类的构造函数有关,但我太新了Java以找出问题,也许你可以帮忙吗?Intent is undefined

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

代码:

@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; 


} 

回答

0

意图是不明确的

时的范围是没有定义的变量,你会得到这些错误(或)类型不明确的编译器。

serviceIntent = new Intent(BROADCAST_SEEKBAR); 

在这一行中,您没有输入变量intent

它应该是这样的

Intent serviceIntent = new Intent(BROADCAST_SEEKBAR); 
1

我猜this指的是适配器不是一个语境。尝试:

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

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

原因是活动的背景下的一个子类,而适配器是不是......

0

请确保您有:

import android.content.Intent; 

在您的进口中,变量intentserviceIntent被声明为类别变量比如:

Intent intent, serviceIntent; 

此外,请确保您使用正确的上下文。在你的情况下,this可能不是你要找的。试试:

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