所以我得到我的Intent
未定义,与该类的构造函数有关,但我在Java上太新了解问题,也许你可以帮忙?
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 :(得分:1)
我猜this
指的是适配器而非上下文。尝试:
serviceIntent = new Intent(getApplicationContext(), myPlayService.class);
或
serviceIntent = new Intent(MainActivity.this, myPlayService.class);
// Where MainActivity is the Activity class's name
原因是Activity是Context的子类,而Adapters不是......
答案 1 :(得分:0)
意图未定义
如果未在范围(或)类型中定义变量,则会在编译器中清除此类错误。
serviceIntent = new Intent(BROADCAST_SEEKBAR);
在此行中,您没有输入变量intent
。
应该是
Intent serviceIntent = new Intent(BROADCAST_SEEKBAR);
答案 2 :(得分:0)
确保您拥有:
import android.content.Intent;
在您的导入中,变量intent
和serviceIntent
声明为类变量或局部变量,如:
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);