2015-01-20 7 views
0

Я делаю приложение, которое может добавлять контакты в список контактов вашего телефона, наблюдая за учебниками, но когда я добавляю вкладки в свое приложение и запускаю его, он выходит из строя, и сообщение было показано, что мое приложение было остановлено, к сожалению,Android приложение сбой после добавления TabHost

Это мой основной XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="wrap_content" 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=".Contact_main" 
android:orientation="vertical" 
android:id="@+id/background"> 


<TabHost 
    android:layout_width="match_parent" 
    android:layout_height="400dp" 
    android:id="@+id/tabhost" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false"></TabWidget> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:text="Contact Creators" 
        android:id="@+id/lblcreatortitle" 
        android:layout_gravity="center" 
        android:layout_marginTop="10dp" 
        android:textSize="30sp" /> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPersonName" 
        android:ems="10" 
        android:id="@+id/textname" 
        android:layout_marginTop="15dp" 
        android:hint="Contact name" /> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="phone" 
        android:ems="10" 
        android:id="@+id/textphone" 
        android:layout_gravity="center" 
        android:hint="Phone no" 
        android:focusableInTouchMode="true" 
        android:layout_marginTop="15dp" 
        android:editable="false" /> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textEmailAddress" 
        android:ems="10" 
        android:id="@+id/textemail" 
        android:layout_gravity="center_horizontal" 
        android:hint="Email" 
        android:focusableInTouchMode="true" 
        android:layout_marginTop="15dp" /> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:ems="10" 
        android:id="@+id/textaddress" 
        android:layout_gravity="center_horizontal" 
        android:hint="Address" 
        android:focusableInTouchMode="true" 
        android:layout_marginTop="15dp" 
        android:inputType="textPostalAddress" /> 

       <Button 
        android:layout_width="200dp" 
        android:layout_height="wrap_content" 
        android:text="Add Contact" 
        android:id="@+id/buttonadd" 
        android:layout_gravity="center" 
        android:layout_marginTop="15dp" 
        android:enabled="false" /> 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"></LinearLayout> 

     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

и это мой mainactivity

package com.example.sayedshazeb.contact; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TabHost; 


public class Contact_main extends ActionBarActivity { 

EditText nametext,phonetext,emailtext,adresstext; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_contact_main); 

    nametext = (EditText) findViewById(R.id.textname); 
    phonetext = (EditText) findViewById(R.id.textphone); 
    emailtext = (EditText) findViewById(R.id.textemail); 
    adresstext = (EditText) findViewById(R.id.textaddress); 
    TabHost tabHost = (TabHost)findViewById (R.id.tabhost); 
    tabHost.setup(); 
    TabHost.TabSpec spec1 = tabHost.newTabSpec("Tab1"); 
    spec1.setContent(R.id.tab1); 
    spec1.setIndicator("Contact"); 

    TabHost.TabSpec spec2 = tabHost.newTabSpec("Tab2"); 
    spec2.setContent(R.id.tab2); 
    spec2.setIndicator("List"); 

    tabHost.addTab(spec1); 
    tabHost.addTab(spec2); 







    final Button addbtn = (Button) findViewById(R.id.buttonadd); 

    nametext.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      addbtn.setEnabled(!nametext.getText().toString().trim().isEmpty()); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
} 

} 

вот мой лог-

01-21 11:23:02.784 3064-3064/com.example.sayedshazeb.contact E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.sayedshazeb.contact, PID: 3064 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sayedshazeb.contact/com.example.sayedshazeb.contact.Contact_main}: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator. 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:212) 
      at android.app.ActivityThread.main(ActivityThread.java:5137) 
      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:902) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator. 
      at android.widget.TabHost.addTab(TabHost.java:221) 
      at com.example.sayedshazeb.contact.Contact_main.onCreate(Contact_main.java:36) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:212) 
            at android.app.ActivityThread.main(ActivityThread.java:5137) 
            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:902) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718) 
            at dalvik.system.NativeStart.main(Native Method) 

Я не знаю, что я делаю неправильно? почему мое приложение рушится после добавления вкладок?

+0

Исследовать LogCat: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare

ответ

0

Похоже, вы используете android.R.id.tabhost вместо R.id.tabhost, когда вы связываете свой TabHost с вашим xml. Без какого-то выхода logcat это лучшее мое предположение.

Good Luck

+0

не кажется, что быть причиной проблема. Спасибо за вашу помощь. –

Смежные вопросы