我的代码有些麻烦,我一定错过了什么。 这是我的代码。 我正在尝试使用一个listview窗口为用户构建一个简单的片段应用程序。而我似乎无法做到这一点。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
}
activity_main.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.shoppinglist.MainActivity" >
<fragment
android:name="com.example.shoppinglist.CartList"
android:id="@+id/fragment1"
android:layout_width="0px"
android:layout_height="match_parent" />
</RelativeLayout>
购物清单代码
public class CartList extends ListFragment{
MyArray ma;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
// Create the adapter to convert the array to views
User newUser = new User("Nathan", "San Diego");
adapter.add(newUser);*/
ArrayList<User> arrayOfUsers = new ArrayList<User>();
UsersAdapter adapter = new UsersAdapter(getActivity(), arrayOfUsers);
ListView listView = (ListView) getView().findViewById(R.id.CartListView);
listView.setAdapter(adapter);
}
cartlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/CartListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
UsersAdapter代码
public class UsersAdapter extends ArrayAdapter<User> {
// View lookup cache
private static class ViewHolder {
TextView name;
TextView home;
}
public UsersAdapter(Context context, ArrayList<User> users) {
super(context, R.layout.item_user, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_user, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
viewHolder.home = (TextView) convertView.findViewById(R.id.tvHome);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
viewHolder.name.setText(user.name);
viewHolder.home.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
public class User {
public String name;
public String hometown;
public User(String name, String hometown) {
this.name = name;
this.hometown = hometown;
}
我得到了这个致命的例外
FATAL EXCEPTION: main
02-25 00:39:00.237: E/AndroidRuntime(1912): Process: com.example.shoppinglist, PID: 1912
02-25 00:39:00.237: E/AndroidRuntime(1912): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shoppinglist/com.example.shoppinglist.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.os.Handler.dispatchMessage(Handler.java:102)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.os.Looper.loop(Looper.java:135)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-25 00:39:00.237: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Native Method)
02-25 00:39:00.237: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Method.java:372)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-25 00:39:00.237: E/AndroidRuntime(1912): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.setContentView(Activity.java:2144)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.example.shoppinglist.MainActivity.onCreate(MainActivity.java:13)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.performCreate(Activity.java:5933)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-25 00:39:00.237: E/AndroidRuntime(1912): ... 10 more
02-25 00:39:00.237: E/AndroidRuntime(1912): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ' android.view.View android.view.View.findViewById(int)' on a null object reference
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.example.shoppinglist.CartList.onCreate(CartList.java:25)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Fragment.performCreate(Fragment.java:2031)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.onCreateView(Activity.java:5282)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
02-25 00:39:00.237: E/AndroidRuntime(1912): ... 20 more`
答案 0 :(得分:0)
更改
android:name="com.example.shoppinglist.CartList"
要
class="com.example.shoppinglist.CartList"
确保您的活动正在扩展FragmentActivity而不是Activity
还要确保您的arraylist不为空。以这种方式添加项目
ArrayList<User> arrayOfUsers = new ArrayList<User>();
User newUser = new User("Nathan", "San Diego");
arrayOfUsers.add(newUser);