2012-12-15 100 views
-1

在我的android應用程序中,我打算實現tabhost。 其實我試圖從androidhive的udapt教程 但是,我不斷得到致命的錯誤。 請幫助我解決這個錯誤tabhost classnotfoundexception

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.activity_main); 

     TabHost tabHost = getTabHost(); 


     TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC); 

     inboxSpec.setIndicator(INBOX_SPEC, getResources().getDrawable(R.drawable.icon_inbox)); 
     Intent inboxIntent = new Intent(this, InboxActivity.class); 

     inboxSpec.setContent(inboxIntent); 


     TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC); 
     outboxSpec.setIndicator(OUTBOX_SPEC, getResources().getDrawable(R.drawable.icon_outbox)); 
     Intent outboxIntent = new Intent(this, OutboxActivity.class); 
     outboxSpec.setContent(outboxIntent); 


     TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC); 
     profileSpec.setIndicator(PROFILE_SPEC, getResources().getDrawable(R.drawable.icon_profile)); 
     Intent profileIntent = new Intent(this, Testactivity.class); 
     profileSpec.setContent(profileIntent); 



     tabHost.addTab(inboxSpec); 
     tabHost.addTab(outboxSpec); 
     tabHost.addTab(profileSpec); 

    } 
} 

佈局

<?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> 

適配器

public class CandidateAdapter extends BaseAdapter{ 
    Context ctx; 
    LayoutInflater lInflater; 
    ArrayList<Candidate> objects; 
    CandidateAdapter(Context context, ArrayList<Candidate> candidates) { 
      ctx = context; 
      objects = candidates; 
      lInflater = (LayoutInflater) ctx 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 
    public int getCount() { 
     return objects.size(); 
     } 
    public Object getItem(int position) { 
     return objects.get(position); 
     } 
    public long getItemId(int position) { 
     return position; 
     } 
    Candidate getCandidate(int position) { 
     return ((Candidate) getItem(position)); 
     } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // используем созданные, но не используемые view 
     View view = convertView; 
     if (view == null) { 
      view = lInflater.inflate(R.layout.inbox_list_item, parent, false); 
     } 
     Candidate p = getCandidate(position); 
     ((TextView) view.findViewById(R.id.from)).setText(p.get_name()); 
     ((TextView) view.findViewById(R.id.subject)).setText(p.get_office()); 
     return view; 
    } 



} 

活動

public class InboxActivity extends ListActivity { 

    List<Candidate> candidates; 
    private DatabaseHandler db; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.inbox_list); 
     db = new DatabaseHandler(this); 
     candidates = db.getAllCandidates(1); 
     CandidateAdapter adapter = new CandidateAdapter(this,(ArrayList) candidates); 
     ListView lvMain = (ListView) findViewById(R.id.lvMain); 
     lvMain.setAdapter(adapter); 

    } 



} 

佈局活性

<?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="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/lvMain" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

列表項目佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <!-- From Label --> 
    <TextView 
     android:id="@+id/from" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="8dip" 
     android:paddingLeft="8dip" 
     android:paddingBottom="4dip" 
     android:textSize="20dip" 
     android:textStyle="bold" /> 

    <!-- Mail Subject --> 
    <TextView android:id="@+id/subject" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:paddingLeft="8dip" 
     android:paddingBottom="6dip" 
     android:textSize="15dip" 
     android:layout_below="@id/from"/> 

    <!-- Mail date --> 
    <TextView android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:padding="8dip"/> 

</RelativeLayout> 

等活動實際上是相同的 唯一字段:

的setContentView(R.layout.inbox_list);候選人= db.getAllCandidates(1);

不同

logcat的輸出

12-15 23:27:47.529: W/dalvikvm(397): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
12-15 23:27:47.568: E/AndroidRuntime(397): FATAL EXCEPTION: main 
12-15 23:27:47.568: E/AndroidRuntime(397): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.s1042512.newvoter/com.s1042512.newvoter.AndroidTabAndListView}: java.lang.ClassNotFoundException: com.s1042512.newvoter.AndroidTabAndListView in loader dalvik.system.PathClassLoader[/data/app/com.s1042512.newvoter-2.apk] 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.os.Looper.loop(Looper.java:123) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-15 23:27:47.568: E/AndroidRuntime(397): at java.lang.reflect.Method.invokeNative(Native Method) 
12-15 23:27:47.568: E/AndroidRuntime(397): at java.lang.reflect.Method.invoke(Method.java:521) 
12-15 23:27:47.568: E/AndroidRuntime(397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-15 23:27:47.568: E/AndroidRuntime(397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-15 23:27:47.568: E/AndroidRuntime(397): at dalvik.system.NativeStart.main(Native Method) 
12-15 23:27:47.568: E/AndroidRuntime(397): Caused by: java.lang.ClassNotFoundException: com.s1042512.newvoter.AndroidTabAndListView in loader dalvik.system.PathClassLoader[/data/app/com.s1042512.newvoter-2.apk] 
12-15 23:27:47.568: E/AndroidRuntime(397): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 
12-15 23:27:47.568: E/AndroidRuntime(397): at java.lang.ClassLoader.loadClass(ClassLoader.java:573) 
12-15 23:27:47.568: E/AndroidRuntime(397): at java.lang.ClassLoader.loadClass(ClassLoader.java:532) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
12-15 23:27:47.568: E/AndroidRuntime(397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 
12-15 23:27:47.568: E/AndroidRuntime(397): ... 11 more 
12-15 23:27:55.818: I/Process(397): Sending signal. PID: 397 SIG: 9 
+0

你的包名是什麼? –

+0

selp.s1042512.newvoter;但如果你爲教程編寫了一些代碼,我正在修改一個 – Yarh

+0

那麼Android正在尋找** com **。s1042512.newvoter。檢查你的AndroidManifest,看看'selp.s1042512.newvoter'是否存在*無處不在* –

回答

0

你有一個問題,你的包名,確保所有的課程和您的Android清單參考相同的包名。