我只想在操作栏选项卡中使用listfragment创建一个带有图像的Listview ..这里是错误日志
12-31 11:25:13.131: D/AndroidRuntime(4199): Shutting down VM
12-31 11:25:13.131: W/dalvikvm(4199): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-31 11:25:13.161: E/AndroidRuntime(4199): FATAL EXCEPTION: main
12-31 11:25:13.161: E/AndroidRuntime(4199): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sunil.actionbartab/com.sunil.actionbartab.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)`enter code here`
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.os.Looper.loop(Looper.java:137)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-31 11:25:13.161: E/AndroidRuntime(4199): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 11:25:13.161: E/AndroidRuntime(4199): at java.lang.reflect.Method.invoke(Method.java:511)
12-31 11:25:13.161: E/AndroidRuntime(4199): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 11:25:13.161: E/AndroidRuntime(4199): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 11:25:13.161: E/AndroidRuntime(4199): at dalvik.system.NativeStart.main(Native Method)
12-31 11:25:13.161: E/AndroidRuntime(4199): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-31 11:25:13.161: E/AndroidRuntime(4199): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.Activity.setContentView(Activity.java:1881)
12-31 11:25:13.161: E/AndroidRuntime(4199): at com.sunil.actionbartab.MainActivity.onCreate(MainActivity.java:29)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.Activity.performCreate(Activity.java:5104)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-31 11:25:13.161: E/AndroidRuntime(4199): ... 11 more
12-31 11:25:13.161: E/AndroidRuntime(4199): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.sunil.actionbartb.AndroidList: make sure class name exists, is public, and has an empty constructor that is public
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
12-31 11:25:13.161: E/AndroidRuntime(4199): ... 21 more
12-31 11:25:13.161: E/AndroidRuntime(4199): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sunil.actionbartb.AndroidList" on path: /data/app/com.sunil.actionbartab-1.apk
12-31 11:25:13.161: E/AndroidRuntime(4199): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
12-31 11:25:13.161: E/AndroidRuntime(4199): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-31 11:25:13.161: E/AndroidRuntime(4199): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-31 11:25:13.161: E/AndroidRuntime(4199): at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
12-31 11:25:13.161: E/AndroidRuntime(4199): ... 24 more
活动主要
<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=".MainActivity" >
<fragment
android:id="@+id/country_fragment"
android:name="com.sunil.actionbartab.MainActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
package com.sunil.actionbartab;
import com.sunil.actionbartab.R;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText("English")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Marathi")
.setTabListener(this));
/*actionBar.addTab(actionBar.newTab().setText(R.string.title_section3)
.setTabListener(this));*/
}
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
/*
* case R.id.action_location_found: // location found
* LocationFound(); return true; case R.id.action_refresh: //
* refresh refreshMenuItem = item; // load the data from server new
* SyncData().execute(); return true; case R.id.action_help: // help
* action return true; case R.id.action_check_updates: // check for
* updates action return true;
*/
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setQueryRefinementEnabled(true);
/*return super.onCreateOptionsMenu(menu);*/
return true;
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
/**
* On first tab we will show our list
*/
if (tab.getPosition() == 0) {
MyFriendsListFragment simpleListFragment = new MyFriendsListFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.country_fragment, simpleListFragment).commit();
} else if (tab.getPosition() == 1) {
AndroidList androidlidt = new AndroidList();
getSupportFragmentManager().beginTransaction()
.replace(R.id.country_fragment, androidlidt).commit();
}
else {
AndroidVersionList androidversionlist = new AndroidVersionList();
getSupportFragmentManager().beginTransaction()
.replace(R.id.country_fragment, androidversionlist).commit();
/*
* Fragment fragment = new DummySectionFragment(); Bundle args = new
* Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
* tab.getPosition() + 1); fragment.setArguments(args);
* getSupportFragmentManager
* ().beginTransaction().replace(R.id.container, fragment).commit();
*/
}
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
}
AndroidList.Java
package com.sunil.actionbartab;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class AndroidList extends ListFragment {
String[] Forts = new String[] {
"Pune",
"Thane",
"Satara",
"Raigad",
"Nashik",
"Kolhapur",
};
int[] citylogo = new int[]{
R.drawable.pune,
R.drawable.thane,
R.drawable.raigad,
R.drawable.nashik,
R.drawable.kolhapur,};
String[] cityinfo = new String[]{
"Pune is known to have existed as a town since 847 AD. It was the first capital of the Maratha Empire under Chhatrapati Shivaji Raje Bhosale",
"Thane is a city in Maharashtra, India, at the head of the Thane Creek.Thane also known as 'the city of lakes' is the administrative headquarters of Thane district",
"It is located in the Konkan region. The district was renamed after Raigad, the fort that was the former capital of the Maratha leader Shivaji Maharaj",
"Nashik is a located on the banks of the Godavari River, the holy river which flows through old Nashik",
"Kolhapur is a sixth largest city of Maharashtra situated in the southwest part of the state of Maharashtra." +
"Before Independence, it was a 19 gun salute princely state ruled by the royal Bhosale Chhatrapatis of the Maratha Empire.",
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*return inflater.inflate(R.layout.list_fragment, container, false);*/
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Forts : " + Forts[i]);
hm.put("cur","Cityinfo : " + cityinfo[i]);
hm.put("citylogo", Integer.toString(citylogo[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.citylogo,R.id.fort,R.id.cityinfo};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.englishlist, from, to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
答案 0 :(得分:0)
首先,关闭xml中的RelativeLayout ...
<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=".MainActivity" >
<fragment
android:id="@+id/country_fragment"
android:name="com.sunil.actionbartab.MainActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
第二步,将onCreateView()
中的代码移至onViewCreated()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Forts : " + Forts[i]);
hm.put("cur","Cityinfo : " + cityinfo[i]);
hm.put("citylogo", Integer.toString(citylogo[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.citylogo,R.id.fort,R.id.cityinfo};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.englishlist, from, to);
setListAdapter(adapter);
}
答案 1 :(得分:0)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sunil.actionbartb.AndroidList" on path: /data/app/com.sunil.actionbartab-1.apk
某处似乎有拼写错误,actionbartb
而不是actionbartab
。如果您最近在代码中修复了此错误,请清理并重建该应用。