gpt4 book ai didi

android - 应用程序甚至在启动前就崩溃了……我正在使用listview打开/调用 fragment

转载 作者:行者123 更新时间:2023-12-03 17:49:02 24 4
gpt4 key购买 nike

我在MainActivity中有一个列表 View
每个项目行中都有一个imageButton。
当单击该Imagebutton时,应显示一个片段.....

//main activity public class MainActivity extends AppCompatActivity{
ListView list;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
ListAdapter adapter = new ListAdapter(this, memTitles, images, memeDescriptions);
list.setAdapter(adapter);
} }

//列出从中调用片段的适配器类
class ListAdapter extends ArrayAdapter<String> {
ListAdapter(Context c, String[] titles, int imgs[], String[] desc) {

}
//extra details are omitted from this code
@Override
public View getView(final int postion, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
ImageButton myImage = (ImageButton) row.findViewById(R.id.imageButton);
myImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data;
SharedPreferences pref = getContext().getSharedPreferences("file", context.MODE_PRIVATE);
data = pref.getString("pos" + postion, "NOTHING");
Fragment_display fr = new Fragment_display ();
Bundle bundle = new Bundle();
FragmentManager fm = ((Activity)context).getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
bundle.putString("value", data);
fr.setArguments(bundle);
fragmentTransaction.replace(R.id.fragment, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return row;
}
}

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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<fragment
android:name="com.example.mishaal.mges_lab11.Fragment_display"
android:id="@+id/fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/listView"
tools:layout="@layout/fragment"
android:layout_alignParentTop="true" />
</RelativeLayout>

Fragment_Display .....此片段显示发送给它的数据
public class Fragment_display extends Fragment {

TextView textView;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
String valueData= getArguments().getString("value");
View myInflatedView = inflater.inflate(R.layout.fragment, container,false);
textView = (TextView) myInflatedView.findViewById(R.id.textView1);
textView.setText(valueData);
return myInflatedView;
}
}

fragment.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"
android:background="#ffff00">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold" />
</LinearLayout>

logcat显示:
11-17 19:12:18.051  26694-26694/? E/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file
11-17 19:12:18.051 26694-26694/? E/SELinux﹕ Function: selinux_android_load_priority , loading version is VE=SEPF_GT-I9505_4.3_0024
11-17 19:12:18.051 26694-26694/? E/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
11-17 19:12:18.051 26694-26694/? D/dalvikvm﹕ Late-enabling CheckJNI
11-17 19:12:18.161 26694-26694/com.example.mishaal.mges_lab11 W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
11-17 19:12:18.161 26694-26694/com.example.mishaal.mges_lab11 I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
11-17 19:12:18.161 26694-26694/com.example.mishaal.mges_lab11 W/dalvikvm﹕ VFY: unable to resolve interface method 14061: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
11-17 19:12:18.161 26694-26694/com.example.mishaal.mges_lab11 D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
11-17 19:12:18.161 26694-26694/com.example.mishaal.mges_lab11 I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
11-17 19:12:18.161 26694-26694/com.example.mishaal.mges_lab11 W/dalvikvm﹕ VFY: unable to resolve interface method 14065: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-17 19:12:18.161 26694-26694/com.example.mishaal.mges_lab11 D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
11-17 19:12:18.211 26694-26694/com.example.mishaal.mges_lab11 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-17 19:12:18.211 26694-26694/com.example.mishaal.mges_lab11 W/dalvikvm﹕ VFY: unable to resolve virtual method 412: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-17 19:12:18.211 26694-26694/com.example.mishaal.mges_lab11 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-17 19:12:18.211 26694-26694/com.example.mishaal.mges_lab11 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-17 19:12:18.211 26694-26694/com.example.mishaal.mges_lab11 W/dalvikvm﹕ VFY: unable to resolve virtual method 434: Landroid/content/res/TypedArray;.getType (I)I
11-17 19:12:18.211 26694-26694/com.example.mishaal.mges_lab11 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-17 19:12:18.271 26694-26694/com.example.mishaal.mges_lab11 D/AbsListView﹕ Get MotionRecognitionManager
11-17 19:12:18.321 26694-26694/com.example.mishaal.mges_lab11 D/AndroidRuntime﹕ Shutting down VM
11-17 19:12:18.321 26694-26694/com.example.mishaal.mges_lab11 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41953898)
11-17 19:12:18.381 26694-26694/com.example.mishaal.mges_lab11 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mishaal.mges_lab11/com.example.mishaal.mges_lab11.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.mishaal.mges_lab11.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.mishaal.mges_lab11.Fragment_display.onCreateView(Fragment_display.java:24)
at android.app.Fragment.performCreateView(Fragment.java:1699)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:879)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1155)
at android.app.Activity.onCreateView(Activity.java:4966)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.mishaal.mges_lab11.MainActivity.onCreate(MainActivity.java:41)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
            at dalvik.system.NativeStart.main(Native Method)
11-17 19:12:21.824 26694-26694/? I/Process﹕ Sending signal. PID: 26694 SIG: 9

谁能说出它为什么崩溃的.....我不知道,请帮助

最佳答案

在您的片段中尝试以下操作:

        String valueData= getArguments().getString("value");
View myInflatedView = inflater.inflate(R.layout.fragment, container,false);
textView = (TextView) myInflatedView.findViewById(R.id.textView1);
if (getArguments() != null) {
String valueData= getArguments().getString("value");
textView.setText(valueData);
}
return myInflatedView;

关于android - 应用程序甚至在启动前就崩溃了……我正在使用listview打开/调用 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com