Android应用程序在片段中创建Listview时崩溃
每次我移动到此活动时,我的应用程序都会崩溃。我认为问题在于在片段中创建和填充ListViewAndroid应用程序在片段中创建Listview时崩溃,android,listview,android-activity,android-fragments,Android,Listview,Android Activity,Android Fragments,每次我移动到此活动时,我的应用程序都会崩溃。我认为问题在于在片段中创建和填充ListView public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_routines,
container, false);
ListView list = (ListView)rootView.findViewById(R.id.listview_routines);
populateListView(list);
return rootView;
}
private void populateListView(ListView list){
String[] items = null;
int work = 1;
switch(work){
case 1:
items = new String[]{"Dumbbell Curls","Dumbbell Kickbacks","Bench Dips","Dumbbell Hammer Curls", "Diamond Pushups", "Dumbbell Flyes"};
break;
case 2:
items = new String[]{"Squats","Plank","Lunges","Leg Raises","Calf Raises","Crunches"};
break;
case 3:
items = new String[]{"Dumbbell Rows","Superman","Arnold Press","Dumbbell Reverse Flyes","Lateral Raises","Pull-Up"};
break;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),
R.layout.routine_items,
items
);
list.setAdapter(adapter);
}
}
这是我的fragment_list_routines.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tutorial.ListRoutinesActivity$PlaceholderFragment" >
<ListView
android:id="@+id/listview_routines"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
几个小时都找不到解决方案。还是一个android新手呵呵看起来你的应用程序崩溃了:
原因:java.lang.NoSuchMethodException:arms\u and\u cost[class android.view.view]
如果要使用此方法,则需要定义此方法
似乎您的XML正在通过按钮引用此方法
用于id为“btn\u arms”的视图类android.widget.Button上的onClick处理程序
在XML中找到此引用,然后删除此引用并在代码中定义OnClickListener,或者将此方法添加到活动中:
public void arms_and_chest(View view){
// ... Operations here
}
你能发布崩溃日志吗?什么是
私有静态上下文列表\u上下文代码>?for?找不到方法arms_和_-cost(View)@Blackbelt别介意,我以前在fragment类中引用过活动,现在,我使用getActivity()方法。你在哪里定义了btn_-arms按钮?谢谢!它现在可以工作了:)错误在于从xml到类方法的引用的拼写错误。哈哈哈
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
03-09 20:26:58.354: I/PGA(7016): New SOCKET connection: com.example.tutorial (pid 7016, tid 7016)
03-09 20:27:01.904: D/AndroidRuntime(7016): Shutting down VM
03-09 20:27:01.904: W/dalvikvm(7016): threadid=1: thread exiting with uncaught exception (group=0x95d9cb20)
03-09 20:27:01.904: D/AndroidRuntime(7016): procName from cmdline: com.example.tutorial
03-09 20:27:01.904: E/AndroidRuntime(7016): in writeCrashedAppName, pkgName :com.example.tutorial
03-09 20:27:01.934: I/Process(7016): Sending signal. PID: 7016 SIG: 9
03-09 20:27:01.934: D/AndroidRuntime(7016): file written successfully with content: com.example.tutorial StringBuffer : ;com.example.tutorial
03-09 20:27:01.934: E/AndroidRuntime(7016): FATAL EXCEPTION: main
03-09 20:27:01.934: E/AndroidRuntime(7016): Process: com.example.tutorial, PID: 7016
03-09 20:27:01.934: E/AndroidRuntime(7016): java.lang.IllegalStateException: Could not find a method arms_and_chest(View) in the activity class com.main.workoutinstructor.MainActivity for onClick handler on view class android.widget.Button with id 'btn_arms'
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.view.View$1.onClick(View.java:3815)
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.view.View.performClick(View.java:4443)
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.view.View$PerformClick.run(View.java:18433)
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.os.Handler.handleCallback(Handler.java:733)
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.os.Handler.dispatchMessage(Handler.java:95)
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.os.Looper.loop(Looper.java:136)
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.app.ActivityThread.main(ActivityThread.java:5021)
03-09 20:27:01.934: E/AndroidRuntime(7016): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 20:27:01.934: E/AndroidRuntime(7016): at java.lang.reflect.Method.invoke(Method.java:515)
03-09 20:27:01.934: E/AndroidRuntime(7016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
03-09 20:27:01.934: E/AndroidRuntime(7016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
03-09 20:27:01.934: E/AndroidRuntime(7016): at dalvik.system.NativeStart.main(Native Method)
03-09 20:27:01.934: E/AndroidRuntime(7016): Caused by: java.lang.NoSuchMethodException: arms_and_chest [class android.view.View]
03-09 20:27:01.934: E/AndroidRuntime(7016): at java.lang.Class.getConstructorOrMethod(Class.java:472)
03-09 20:27:01.934: E/AndroidRuntime(7016): at java.lang.Class.getMethod(Class.java:857)
03-09 20:27:01.934: E/AndroidRuntime(7016): at android.view.View$1.onClick(View.java:3808)
03-09 20:27:01.934: E/AndroidRuntime(7016): ... 11 more
public void arms_and_chest(View view){
// ... Operations here
}