-1

在我的 android 应用程序中,我正在尝试实现 tabhost。实际上我正在尝试来自 androidhive 的 udapt 教程但是我不断收到致命错误。帮我解决这个错误

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

是不同的

日志输出

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
4

1 回答 1

0

您的包名有问题,请确保您的所有类和您的 Android Manifest 引用相同的包名。

于 2012-12-16T00:10:13.703 回答