Android 在片段中创建 Listview 时应用崩溃
Android app crashes when creating Listview in fragments
每次我移动到这个 activity 时,我的应用程序总是崩溃。我认为问题在于在 Fragment 中创建和填充 ListView。
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);
}
}
这是占位符片段 class 所在的 activity:
public class ListRoutinesActivity extends ActionBarActivity {
private static int type;
private static Context list_context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_routines);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
这是我的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"
/>
这是我的routine_items.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.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.onClick(View.java:3808)
03-09 20:27:01.934: E/AndroidRuntime(7016): ... 11 more
几个小时都找不到解决方案。 android还是个初学者呵呵
看起来你的应用程序崩溃了:
Caused by: java.lang.NoSuchMethodException: arms_and_chest [class android.view.View]
如果你想使用它,你需要定义这个方法。
您的 XML 似乎正在通过按钮
引用此方法
for onClick handler on view class android.widget.Button with id 'btn_arms'
在您的 XML 中找到此引用并删除它并在您的代码中定义一个 OnClickListener,或者将此方法添加到您的 activity:
public void arms_and_chest(View view){
// ... Operations here
}
每次我移动到这个 activity 时,我的应用程序总是崩溃。我认为问题在于在 Fragment 中创建和填充 ListView。
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);
}
}
这是占位符片段 class 所在的 activity:
public class ListRoutinesActivity extends ActionBarActivity {
private static int type;
private static Context list_context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_routines);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
这是我的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"
/>
这是我的routine_items.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.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.onClick(View.java:3808)
03-09 20:27:01.934: E/AndroidRuntime(7016): ... 11 more
几个小时都找不到解决方案。 android还是个初学者呵呵
看起来你的应用程序崩溃了:
Caused by: java.lang.NoSuchMethodException: arms_and_chest [class android.view.View]
如果你想使用它,你需要定义这个方法。
您的 XML 似乎正在通过按钮
引用此方法for onClick handler on view class android.widget.Button with id 'btn_arms'
在您的 XML 中找到此引用并删除它并在您的代码中定义一个 OnClickListener,或者将此方法添加到您的 activity:
public void arms_and_chest(View view){
// ... Operations here
}