当我尝试测试时,应用程序崩溃,我正在使用片段来创建应用程序: App crashed 我在audioFragment.java中有这个:
package amaguenet.com.reproductorxassidas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class audioFragment extends Fragment {
// variable declaration
private ListView mainList;
private MediaPlayer mp;
private final String[] listContent = {"Ahonzubilahi Min Mayli", "Ajabani Khayru Baqin", "Ajabani Rabbu Sama", "Ala Innani Usni",
"Alal Muntaqa Khayril Baraya"};
private final int[] resID = {R.raw.ahonzu_bilahi_min_mayli, R.raw.ajabani_khayru_baqin, R.raw.ajabani_rabbu_sama,
R.raw.ala_innani_usni, R.raw.alal_muntaqa_khayril_baraya};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_audio, container, false);
// Initializing variables
mp = new MediaPlayer();
mainList = (ListView) v.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listContent);
mainList.setAdapter(adapter);
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
playSong(position);
}
});
return v;
}
public void playSong(int songIndex) {
// Play song
mp.reset();// stops any current playing song
mp = MediaPlayer.create(getActivity().getApplicationContext(), resID[songIndex]);// create's
// new
// mediaplayer
// with
// song.
mp.start(); // starting mediaplayer
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public interface OnFragmentInteractionListener {
}
}
这是在fragment_audio(布局)中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="amaguenet.com.reproductorxassidas.audioFragment"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
有人可以帮忙解决这个问题,我在Android Studio中使用Fragments吗?
答案 0 :(得分:0)
你可以使用getIdentifier()方法,这是一个简单的例子:
private final int resID = getContext().getResources().getIdentifier(direction, "raw", PACKAGE_NAME);
direction - 文件名为f.e的字符串:&#34; ahonzu_bilahi_min_mayli&#34;
PACKAGE_NAME - 是您的包裹名称