gpt4 book ai didi

java - Android:RuntimeException:无法启动 Activity ComponentInfo

转载 作者:行者123 更新时间:2023-12-01 13:49:41 25 4
gpt4 key购买 nike

我是 Android 新手,我正在尝试适应此 Activity :ANDROID: EXPANDABLE LIST VIEW EXAMPLE到 fragment ,以便我可以在主从流程上使用它,并且我不断收到此消息:

FATAL EXCEPTION: main
E/AndroidRuntime(2391): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.expandablelistdemo/com.example.expandablelistdemo.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment

这是我的代码:

list

    <uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.expandablelistdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

MainActivity.java(默认 Activity )

package com.example.expandablelistdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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;
}

}

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=".MainActivity" >


<fragment
android:id="@+id/item_list"
android:name="com.example.expandablelistdemo.MainFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />


</RelativeLayout>

MainFragment.java

package com.example.expandablelistdemo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainFragment extends Fragment {

List<String> groupList;
List<String> childList;
Map<String, List<String>> laptopCollection;
ExpandableListView expListView;

private LinearLayout ll;
private FragmentActivity fa;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

fa = (FragmentActivity) super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.fragment_main, container, false);

return ll;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.getActivity().setContentView(R.layout.fragment_main);

createGroupList();

createCollection();

expListView = (ExpandableListView) super.getActivity().findViewById(R.id.laptop_list);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
super.getActivity(), groupList, laptopCollection);
expListView.setAdapter(expListAdapter);

expListView.setOnChildClickListener(new OnChildClickListener() {

public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
final String selected = (String) expListAdapter.getChild(
groupPosition, childPosition);
// Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG).show();

return true;
}
});
}

private void createGroupList() {
groupList = new ArrayList<String>();
groupList.add("HP");
groupList.add("Dell");
groupList.add("Lenovo");
groupList.add("Sony");
groupList.add("HCL");
groupList.add("Samsung");
}

private void createCollection() {
// preparing laptops collection(child)
String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540",
"HP Envy 4-1025TX" };
String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series",
"ThinkPad X Series", "Ideapad Z Series" };
String[] sonyModels = { "VAIO E Series", "VAIO Z Series",
"VAIO S Series", "VAIO YB Series" };
String[] dellModels = { "Inspiron", "Vostro", "XPS" };
String[] samsungModels = { "NP Series", "Series 5", "SF Series" };

laptopCollection = new LinkedHashMap<String, List<String>>();

for (String laptop : groupList) {
if (laptop.equals("HP")) {
loadChild(hpModels);
} else if (laptop.equals("Dell"))
loadChild(dellModels);
else if (laptop.equals("Sony"))
loadChild(sonyModels);
else if (laptop.equals("HCL"))
loadChild(hclModels);
else if (laptop.equals("Samsung"))
loadChild(samsungModels);
else
loadChild(lenovoModels);

laptopCollection.put(laptop, childList);
}
}

private void loadChild(String[] laptopModels) {
childList = new ArrayList<String>();
for (String model : laptopModels)
childList.add(model);
}

// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.getActivity().getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

fragment_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"
tools:context=".MainFragment" >

<ExpandableListView
android:id="@+id/laptop_list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ExpandableListView>

</RelativeLayout>

列表中还使用了 1 个 java 类和 2 个布局,但我确信这些不是问题......我只能找出问题出在哪里

我最好的猜测是fa = (FragmentActivity) super.getActivity();,这个 Actor 看起来很奇怪......或者也许只是我

任何帮助将不胜感激!

最佳答案

尝试使用 FragmentActivity 扩展 MainActivity,而不仅仅是 Activity。即:

public class MainActivity extends FragmentActivity

关于java - Android:RuntimeException:无法启动 Activity ComponentInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20061125/

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