0

可能重复:
如何将现有数据库与 Android 应用程序一起使用

我必须通过在 android 中使用 arrayadapter 将项目添加到列表视图中,所以完成了编码,我的 xml 布局文件名是“jobs”,但我收到错误,因为“jobs 无法解析或不是一个字段”,我已经包含了这个清单中的活动但仍然是相同的错误

public class Jobs extends ListActivity {
private TextView selection;

private static final String[] items={"Computer Hardware", "Featured", "Information Technology", "Software", "Technical"

};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jobs);
    ListView list = (ListView)findViewById(R.id.list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.jobs, items);
        setListAdapter(adapter);    


 }
public void onListItemClick(ListView parent, View v, int position, long id)
{
    selection.setText(items[position]);
}
 }

这是xml布局文件

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView 
    android:id="@+id/selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

清单.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rebuix.com"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/rebuix"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:background="#3D0909">
    <activity
        android:name=".LoadingScreen"
        android:label="@string/title_activity_loading_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Rebuix"
        android:label="@string/title_activity_rebuix" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.rebuix.com.Rebuix" />
    </activity>
    <activity
        android:name=".com.rebuix.com.Jobs"
        android:label="@string/app_name" >

    </activity>
    <activity
        android:name=".Login"
        android:label="@string/title_activity_login" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.rebuix.com.Rebuix" />
    </activity>
    <activity
        android:name=".About"
        android:label="@string/title_activity_about" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="android.support.v4.app.FragmentActivity" />
    </activity>
    <activity
        android:name=".Home"
        android:label="@string/title_activity_home" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="android.support.v4.app.FragmentActivity" />
    </activity>
    <activity
        android:name=".Changepassword"
        android:label="@string/title_activity_changepassword" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="android.support.v4.app.FragmentActivity" />
    </activity>
   </application>

   </manifest>

日志输出

01-31 12:31:53.133: I/Tutorial(531): Starting task with url: www.google.co.uk
01-31 12:32:03.972: D/dalvikvm(531): GC_EXTERNAL_ALLOC freed 64K, 52% free  2595K/5379K, external 1645K/2137K, paused 52ms
01-31 12:32:04.102: D/dalvikvm(531): GC_EXTERNAL_ALLOC freed 2K, 52% free 2594K/5379K, external 2687K/3355K, paused 45ms
01-31 12:32:08.083: D/AndroidRuntime(531): Shutting down VM
01-31 12:32:08.083: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-31 12:32:08.103: E/AndroidRuntime(531): FATAL EXCEPTION: main
01-31 12:32:08.103: E/AndroidRuntime(531): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.exm.com/com.exm.com.Jobs}; have you declared this activity in your AndroidManifest.xml?
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.app.Activity.startActivityForResult(Activity.java:2827)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.app.Activity.startActivity(Activity.java:2933)
01-31 12:32:08.103: E/AndroidRuntime(531):  at com.rebuix.com.Rebuix$4.onClick(Rebuix.java:69)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.view.View.performClick(View.java:2485)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.view.View$PerformClick.run(View.java:9080)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.os.Handler.handleCallback(Handler.java:587)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-31 12:32:08.103: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:123)
 01-31 12:32:08.103: E/AndroidRuntime(531):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 01-31 12:32:08.103: E/AndroidRuntime(531):     at  java.lang.reflect.Method.invokeNative(Native Method)
01-31 12:32:08.103: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:507)
01-31 12:32:08.103: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 01-31 12:32:08.103: E/AndroidRuntime(531):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  01-31 12:32:08.103: E/AndroidRuntime(531):    at dalvik.system.NativeStart.main(Native Method)
4

8 回答 8

3

因为您正在扩展ListActivity然后声明 ListView 与 xml 中的一样:

android:id="@android:id/list"

并获取 ListView 参考:

ListView list = (ListView)findViewById(android.R.id.list);

或者

   ListView list = this.getListView();
于 2013-01-31T06:28:07.590 回答
1

你的问题是:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.jobs, items);
        setListAdapter(adapter);    

删除安卓:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.jobs, items);
        setListAdapter(adapter);    
于 2013-01-31T06:28:54.433 回答
1

尝试这个

public class Jobs extends ListActivity {



 private TextView selection;

private static final String[] items = { "Computer Hardware", "Featured", "Information Technology",
        "Software", "Technical"

};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jobs);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    setListAdapter(adapter);

}

public void onListItemClick(ListView parent, View v, int position, long id) {
    selection.setText(items[position]);
}

}

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView 
    android:id="@+id/selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

解释 :

1 ) 所以你在自定义视图中的列表应该有 id android:id="@android:id/list"。如果您想将它们添加到ListActivity中。

2 ) 您没有将项目映射到列表视图,而是映射到列表项。因此,您必须在添加新的 ArrayAdapter(this, android.R.layout.simple_list_item_1, items);时使用自定义列表项或使用 android 默认值 。

于 2013-01-31T06:39:07.143 回答
0

代替

android.R.layout.jobs

利用

R.layout.jobs
于 2013-01-31T06:30:20.687 回答
0

看起来您使用了错误的 R 类。您将作业定义称为android.R.layout.jobs(这是 Android 的 R 类,可能不包含作业定义)。这应该类似于<your package>/R.layout.jobs.

于 2013-01-31T06:31:57.253 回答
0

可能你的问题是:android.R.layout.jobs

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.jobs, items);
setListAdapter(adapter);

试试看 :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
于 2013-01-31T06:35:19.943 回答
0

当您通过 ListActivity 扩展您的活动时,您必须将 listview id 声明为@android:id/list

android:id="@android:id/list"

在您的活动中,无需声明 listview。否则,如果您想声明,请执行以下操作

ListView list = (ListView)findViewById(android.R.id.list);
于 2013-01-31T06:35:29.863 回答
0

注意到错误并不难,当您遇到错误时请注意堆栈跟踪。

01-31 12:32:08.103: E/AndroidRuntime(531): android.content.ActivityNotFoundException: 找不到明确的活动类 {com.exm.com/com.exm.com.Jobs}; 您是否在 AndroidManifest.xml 中声明了此活动?

于 2013-01-31T07:16:44.650 回答