Java 将活动复制到片段
我对java非常陌生,请原谅我的错误或误解。我试图将我的代码行从一个活动转移到一个片段。但是,当我将粘贴复制到片段上时会出现错误 我编辑的片段页面中的某些错误包括无法解析方法、无法解析构造函数和无法解析符号 活动代码Java 将活动复制到片段,java,android,android-fragments,Java,Android,Android Fragments,我对java非常陌生,请原谅我的错误或误解。我试图将我的代码行从一个活动转移到一个片段。但是,当我将粘贴复制到片段上时会出现错误 我编辑的片段页面中的某些错误包括无法解析方法、无法解析构造函数和无法解析符号 活动代码 public class MainActivity extends AppCompatActivity { private SongCollection songCollection = new SongCollection(); @Override pr
public class MainActivity extends AppCompatActivity
{
private SongCollection songCollection = new SongCollection();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(this, view);
Song selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent (this, PlaySongActivity.class);
intent.putExtra("id", song.getId());
intent.putExtra("title", song.getTitle());
intent.putExtra("artist", song.getartist());
intent.putExtra("fileLink" ,song.getFileLink());
intent.putExtra("coverArt", song.getCoverArt());
startActivity(intent);
}
}
未编辑片段码
/**
* A simple {@link Fragment} subclass.
*/
public class TrendingFragment extends Fragment {
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
return v;
}
}
/**
* A simple {@link Fragment} subclass.
*/
public class TrendingFragment extends Fragment {
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(this, view);
Song selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent (this, PlaySongActivity.class);
intent.putExtra("id", song.getId());
intent.putExtra("title", song.getTitle());
intent.putExtra("artist", song.getartist());
intent.putExtra("fileLink" ,song.getFileLink());
intent.putExtra("coverArt", song.getCoverArt());
startActivity(intent);
}
return v;
}
}
编辑片段代码
/**
* A simple {@link Fragment} subclass.
*/
public class TrendingFragment extends Fragment {
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
return v;
}
}
/**
* A simple {@link Fragment} subclass.
*/
public class TrendingFragment extends Fragment {
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(this, view);
Song selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent (this, PlaySongActivity.class);
intent.putExtra("id", song.getId());
intent.putExtra("title", song.getTitle());
intent.putExtra("artist", song.getartist());
intent.putExtra("fileLink" ,song.getFileLink());
intent.putExtra("coverArt", song.getCoverArt());
startActivity(intent);
}
return v;
}
}
您正在方法中声明方法。这可能会有所帮助
Song selectedSong;
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(this, view);
selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent (this, PlaySongActivity.class);
intent.putExtra("id", song.getId());
intent.putExtra("title", song.getTitle());
intent.putExtra("artist", song.getartist());
intent.putExtra("fileLink" ,song.getFileLink());
intent.putExtra("coverArt", song.getCoverArt());
startActivity(intent);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
handleSelection(v);
sendDataToActivity(selectedSong)
return v;
}