实现我在如何将变量从一个类传递到另一个类时找到的代码 但要编译它会给我错误并且掉线,留下错误
我认为错误是在片段
中实现listview的问题 05-11 23:20:14.697 10975-10975/com.learn2crack.tab E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.learn2crack.tab, PID: 10975
java.lang.NullPointerException
at com.learn2crack.tab.Categoria.onCreateView(Categoria.java:53)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16537)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1947)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1137)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1326)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
at dalvik.system.NativeStart.main(Native Method)
this categoria.java
代码类
ListView Lv =(ListView) getActivity().findViewById(R.id.list_item);
Lv.setOnItemClickListener(new OnItemClickListener() {
// Listview on item click listener
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String tagID = ((TextView) view.findViewById(R.id.idCAT))
.getText().toString();
// Starting single contact activity
Intent in = new Intent(getActivity(),ListaCat.class);
//in.putExtra(TAG_title, name);
in.putExtra(TAG_ID, tagID);
//.putExtra(TAG_description, description);
startActivity(in);
}
});
列出项目
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:id="@+id/name"
android:textColor="#ff7900"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:gravity="fill_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/idCAT"
android:visibility="invisible" />
</LinearLayout>
远景
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list" />
答案 0 :(得分:0)
我能看到的第一件事是view.findViewById(R.id.idCAT)
仅用view
ListView Lv =(ListView) getActivity().findViewById(R.id.list_item);
Lv.setOnItemClickListener(new OnItemClickListener() {
// Listview on item click listener
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
/* String tagID = ((TextView) view)
.getText().toString();*/
TextView txt = (TextView) parent.getChildAt(position - lv.firstVisiblePosition()).findViewById(R.id.list_item);
String tagId = txt.getText().toString();
// Starting single contact activity
Intent in = new Intent(getActivity().getApplicationContext(),ListaCat.class);
//in.putExtra(TAG_title, name);
in.putExtra(TAG_ID, tagID);
//.putExtra(TAG_description, description);
startActivity(in);
}
});
您使用findViewById
获得了什么?
答案是view
所以你不能在视图中得到另一种观点
您必须将该视图投射到ListView
答案 1 :(得分:0)
而不是
Intent in = new Intent(getActivity(),ListaCat.class);
试试这个
Intent in = new Intent(getActivity().getApplicationContext(),ListaCat.class);
答案 2 :(得分:0)
我认为问题是你的Listview没有得到xml替换的ref ListView Lv =(ListView)getActivity()。findViewById(R.id.list_item);到 ListView Lv =(ListView)getView()。findViewById(R.id.list_item); 然后检查一下。