0

我正在尝试将 Tab-Host 融入 Android

  • 我在 logcat 中发布时遇到错误
  • 关于如何克服这个问题的任何想法
  • JSON响应和其他东西都很好,因为它在没有tabhost的情况下运行良好,但是因为我已经整合了下面的tabhost,所以我遇到了错误

AndroidTabAndListView.java

public class AndroidTabAndListView extends TabActivity {

    // TabSpec Names
        private static final String INBOX_SPEC = "Inbox";
        private static final String OUTBOX_SPEC = "Outbox";
        private static final String PROFILE_SPEC = "Profile";

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

            TabHost tabHost = getTabHost();

            // Inbox Tab
            TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
            Intent inboxIntent = new Intent(this, MainActivity.class);
            // Tab Content
            inboxSpec.setContent(inboxIntent);

            // Outbox Tab
            TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
            Intent PriceIntent = new Intent(this, PriceDescriptionActivity.class);
            PriceSpec.setContent(PriceIntent);

            // Profile Tab
            TabSpec DistanceSpec = tabHost.newTabSpec(PROFILE_SPEC);
            Intent DistanceIntent = new Intent(this, DistanceDiscriptionActivity.class);
            DistanceSpec.setContent(DistanceIntent);

            // Adding all TabSpec to TabHost
            tabHost.addTab(inboxSpec); 
            tabHost.addTab(PriceSpec); 
            tabHost.addTab(DistanceSpec); 
        }

}

MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://54.218.73.244:7004/";
    TextView timedisplay;
    String item;
    private HashMap<Integer, String> TimeMap = new HashMap<Integer, String>();
    ListView yourListView;
    List<Item> yourData = new ArrayList<Item>();
    MyAdapter customAdapter;
    ProgressDialog progressDialog;
    String restaurant_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timedisplay = (TextView) findViewById(R.id.RestaurantTimeID);

        yourListView = (ListView) findViewById(R.id.listViewID);
        // Instantiating ProgressDialog with onCreate method


        progressDialog = new ProgressDialog(MainActivity.this);
        new ParsingAsync().execute();


    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(MainActivity.this, "",
                    "Please Wait", true, false);

        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            String _response = null;
            String _response1 = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                httpclient.getParams().setParameter(
                        CoreProtocolPNames.PROTOCOL_VERSION,
                        HttpVersion.HTTP_1_1);
                HttpGet request = new HttpGet(url);
                HttpResponse response = httpclient.execute(request);
                HttpEntity resEntity = response.getEntity();
                _response = EntityUtils.toString(resEntity);
                JSONObject jsonObject = new JSONObject(_response);
                JSONArray first_array = jsonObject.getJSONArray("restaurants");
                JSONArray second_array = jsonObject
                        .getJSONArray("RestaurantTimings");
                for (int i = 0; i < first_array.length(); i++) {
                    JSONObject c = second_array.getJSONObject(i);
                    Item item = new Item();

                    // Storing each json item in variable
                    int id = c.getInt("_id");
                    String TIME = c.getString("RestaurantTime");
                    item.setTime(TIME);

                    c = first_array.getJSONObject(i);

                    String NAME = c.getString("restaurantNAME");
                    item.setName(NAME);

                    yourData.add(item);
                }

                HttpClient httpclient1 = new DefaultHttpClient();
                httpclient.getParams().setParameter(
                        CoreProtocolPNames.PROTOCOL_VERSION,
                        HttpVersion.HTTP_1_1);


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();

            customAdapter = new MyAdapter(MainActivity.this,
                    R.layout.itemlistrow, yourData);
            yourListView.setAdapter(customAdapter);
            yourListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    item = yourData.get(position).getName();

                    // String sendingurl="url1?param1=value1";

                    Intent i = new Intent(MainActivity.this,
                            RestaurantDesc.class);
                    i.putExtra("REST", item.toString());
                    // i.putExtra("key", yourData.get(position).getUrl());
                    //i.putExtra("CC_RES", item.toString());
                    startActivity(i);

                }
            });
        }

    }

}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>

日志猫::

09-18 15:37:50.366: D/AndroidRuntime(553): Shutting down VM
09-18 15:37:50.366: W/dalvikvm(553): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-18 15:37:50.385: E/AndroidRuntime(553): FATAL EXCEPTION: main
09-18 15:37:50.385: E/AndroidRuntime(553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.findmybuffet/com.project.findmybuffet.AndroidTabAndListView}: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.os.Looper.loop(Looper.java:123)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-18 15:37:50.385: E/AndroidRuntime(553):  at java.lang.reflect.Method.invokeNative(Native Method)
09-18 15:37:50.385: E/AndroidRuntime(553):  at java.lang.reflect.Method.invoke(Method.java:507)
09-18 15:37:50.385: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-18 15:37:50.385: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-18 15:37:50.385: E/AndroidRuntime(553):  at dalvik.system.NativeStart.main(Native Method)
09-18 15:37:50.385: E/AndroidRuntime(553): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.widget.TabHost.addTab(TabHost.java:198)
09-18 15:37:50.385: E/AndroidRuntime(553):  at com.project.findmybuffet.AndroidTabAndListView.onCreate(AndroidTabAndListView.java:40)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-18 15:37:50.385: E/AndroidRuntime(553):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-18 15:37:50.385: E/AndroidRuntime(553):  ... 11 more

从错误中说“您必须指定一种制作标签指示器的方法”....但是如何解决它

4

2 回答 2

0

指示器告诉 tabspec 要显示什么。

例如,您可以将布局膨胀到其中...

DistanceSpec.setIndicator(inflater.inflate(layout.distance_tab, null));

并让 distance_tab.xml 有类似....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right|center_vertical"
android:background="#00ffffff">

<LinearLayout
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:id="@+id/layout_distance_tab"
    android:background="@drawable/tab_drawable"
    android:layout_height="60dip">

    <ImageView
        android:id="@+id/image_tab_icon"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="1dip"
        android:layout_height="30dip"
        android:layout_width="45dip"
        android:background="@drawable/tab_distance_drawable"></ImageView>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="10.5dip"
        android:textColor="@drawable/text_tab_indicator"
        android:text="Distance"
        android:layout_marginTop="2dip"></TextView>

</LinearLayout>
</LinearLayout>
于 2013-09-18T10:45:47.420 回答
0

您需要为每个选项卡设置indicator类似inboxSpec.setIndicator(INBOX_SPEC);如下:

       TabHost tabHost = getTabHost();
        // Inbox Tab
        TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        Intent inboxIntent = new Intent(this, MainActivity.class);
        inboxSpec.setIndicator(INBOX_SPEC);       <----------- Set the indicator 
        // Tab Content
        inboxSpec.setContent(inboxIntent);

        // Outbox Tab
        TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
        Intent PriceIntent = new Intent(this, PriceDescriptionActivity.class);
        PriceSpec .setIndicator(OUTBOX_SPEC);   
        PriceSpec.setContent(PriceIntent);

        // Profile Tab
        TabSpec DistanceSpec = tabHost.newTabSpec(PROFILE_SPEC);
        Intent DistanceIntent = new Intent(this, DistanceDiscriptionActivity.class);
        DistanceSpec .setIndicator(PROFILE_SPEC);  
        DistanceSpec.setContent(DistanceIntent);
于 2013-09-18T10:30:21.053 回答