首页 > 解决方案 > 将活动复制到片段

问题描述

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

}

标签: javaandroidandroid-fragments

解决方案


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

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

推荐阅读