将活动复制到碎片



我对Java非常陌生,因此请赦免我的错误或误解。我正在尝试将代码线从活动转移到片段。但是,当我将粘贴复制到片段上时,有错误。

我编辑的片段页面中的某些错误包括无法解析方法,无法解析构造函数,并且无法解析符号

活动代码

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

您在方法中声明方法。这可能会有所帮助。

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

最新更新