这是我的第一个Android应用程序,我遇到了一些问题。我想在teste_menu.xml中使用menu_item.xml作为Fragment
,但我不知道我做错了什么。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nunog.myapplication12, PID: 5342
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2743)
at android.widget.ListView.makeAndAddView(ListView.java:1811)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1641)
at android.widget.AbsListView.onLayout(AbsListView.java:2564)
at android.view.View.layout(View.java:15749)
at android.view.ViewGroup.layout(ViewGroup.java:4880)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15749)
at android.view.ViewGroup.layout(ViewGroup.java:4880)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15749)
at android.view.ViewGroup.layout(ViewGroup.java:4880)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15749)
at android.view.ViewGroup.layout(ViewGroup.java:4880)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2071)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1256)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6635)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at android.view.Choreographer.doCallbacks(Choreographer.java:613)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Menu.class
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Menu extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] menu_entries = getResources().getStringArray(R.array.menu_options);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.teste_menu, R.id.label, menu_entries));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
}
MenuItemFragment.class
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MenuItemFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_item, container, false);
return view;
}
}
teste_menu.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:weightSum="1">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.nunog.myapplication12.MenuItemFragment"
android:id="@+id/fragment2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
tools:layout="@layout/menu_item" />
</RelativeLayout>
menu_item.XML
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#670000"
android:textColor="#ffffff"
android:padding="25dip"
android:textSize="30sp"
android:textStyle="bold">
</TextView>
答案 0 :(得分:0)
在Fragment
使用的布局中使用ListView
没有意义。
另外,当您查看正在使用的ArrayAdapter
构造函数的文档时,第二个参数是要使用的布局文件的id,第三个参数是TextView
内的teste_menu
的id第二个参数中指定的相同布局文件。布局文件TextView
没有label
,标识为TextView
。
由于您要使用的布局文件中只有this.setListAdapter(new ArrayAdapter<String>(this, R.layout.menu_item, menu_entries));
,因此您可以使用不同的构造函数,如下所示,它将起作用:
left.png