我遇到了Android应用程序的问题。我是初学者,我倾向于自学。所以我想创建一个MediaPlayer,为了做到这一点,我需要使用Fragments。问题不在于Fragment本身,而是我需要在MediaPlayer一侧添加一些额外的onClickListeners按钮,我的应用程序崩溃了,因为fragment_main.xml文件具有布局参数但我不能在Fragment中使用OnClickListener函数(我在findViewById时遇到错误(不能对非静态方法进行静态引用)。请帮帮我。
public class MainActivity extends Activity {
Button blade;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
/* blade = (Button)findViewById(R.id.fantab);
blade.setOnClickListener(new OnClickListener(){
@Override
//On click function
public void onClick(View view) {
//Create the intent to start another activity
Intent vent = new Intent(MainActivity.this, Ventscreen.class);
startActivity(vent);
finish();
}
});
*/
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends ListFragment {
private static final String[] FROM = { MediaStore.Audio.Media.TITLE };
private static final int[] TO = { android.R.id.text1 };
private CursorAdapter mAdapter;
Button blade;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
return view;
blade = (Button)findViewById(R.id.fantab);
blade.setOnClickListener(new OnClickListener(){
@Override
//On click function
public void onClick(View view) {
//Create the intent to start another activity
Intent vent = new Intent(MainActivity.this, Ventscreen.class);
startActivity(vent);
finish();
}
});
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context context = getActivity();
int layout = android.R.layout.simple_list_item_1;
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
MediaStore.Audio.Media.TITLE + " ASC");
int flags = 0;
mAdapter = new SimpleCursorAdapter(context, layout, cursor, FROM, TO, flags);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mAdapter.notifyDataSetChanged();
}
}
}
这不仅仅是一个按钮,上面的代码就是例如,总共应该有五个按钮。 谢谢。
答案 0 :(得分:0)
blade = (Button)findViewById(R.id.fantab);
您尝试调用外findViewById(int)
的{{1}}方法。这是不可能的,因为你的内部Activity
类是静态的。您要执行的操作是在Fragment
findViewById(int)
上致电Fragment
:
View
另请注意@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
blade = (Button) view.findViewById(R.id.fantab);
blade.setOnClickListener(new OnClickListener(){
@Override
//On click function
public void onClick(View view) {
//Create the intent to start another activity
Intent vent = new Intent(getActivity(), Ventscreen.class);
getActivity().startActivity(vent);
getActivity().finish();
}
});
return view;
}
声明的位置。