0

我希望 listview1 打开我的包中定义的一些活动,并且在这些活动上我重新定义了一个新的 listview2 活动。

我试图编写相同的代码。请查看代码以了解问题。

public class ABC extends ListActivity{

String classNames[] = {"A","B","C"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,classNames));
}

protected void onListItemClick(ListView lv, View v, int position, long id){
    super.onListItemClick(lv, v, position, id);
    String openClass = classNames[position];
    try{
        Class selected = Class.forName("com.lab.example."+openClass);
        Intent selectedIntent = new Intent(this, selected);
        startActivity(selectedIntent);

    }
    catch(ClassNotFoundException e){
        e.printStackTrace();
    }

}

上面的源代码a,b,c类可以在listview中看到。但是当我点击 A 时,什么也没有发生。

我想知道 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,classNames)); }

我如何在上面添加自定义布局

而对于 A 类:-

public class A extends ListActivity{

String classNames[] = {"x1","x2","x3"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, classNames));
}

protected void onListItemClick(ListView lv, View v, int position, long id){
    super.onListItemClick(lv, v, position, id);
    String openClass = classNames[position];
    try{
        Class selected = Class.forName("com.lab.example."+openClass);
        Intent selectedIntent = new Intent(this, selected);
        startActivity(selectedIntent);

    }
    catch(ClassNotFoundException e){
        e.printStackTrace();
    }

}

on the above source code x1,x2,x3 class activites are working for A only

而X1:-

public class X1 extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Listview Data
    String products[] = getResources().getStringArray(R.array.X1);
    lv = (ListView) findViewById(R.id.list_view11);
    inputSearch = (EditText) findViewById(R.id.inputSearch11);
    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list, R.id.route, products);
    lv.setAdapter(adapter);      
    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            X1.this.adapter.getFilter().filter(cs);
        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

}

}

显现:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lab.example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.lab.example.MainActivitypage"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Menu"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.lab.example.Menu" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>



   <activity android:name="com.lab.example.ABC" />
   <activity android:name="com.lab.example.A" />
   <activity android:name="com.lab.example.B" />
   <activity android:name="com.lab.example.C" />
   <activity android:name="com.lab.example.X1" />
   <activity android:name="com.lab.example.X2" />
   <activity android:name="com.lab.example.X3" />

日志 cat 中显示的错误:-

日志中充满了这个错误,因为我是 android 新手,所以无法理解这一点:-

03-14 23:16:55.970: E/PGA(3459): PgaSocketWriteAllHdipc: hd_ipc_send() failed
03-14 22:32:23.810: E/InputDispatcher(1304): channel 'b466cb20      com.lab.example/com.lab.example.ABC (server)' ~ Channel is unrecoverably broken and will be disposed! 
03-14 22:34:45.480: E/ALSALib(1293): external/alsa-lib/src/confmisc.c:136:    (snd_config_get_bool) Invalid type for nonblock

我想当我单击 A->X1 开始活动时。如果我不清楚,请在下面的评论中提及。任何形式的建议表示赞赏。

4

2 回答 2

1

classNames数组更改A ListActivity为:

String classNames[] = {"X1","X2","X3"};

因为您使用 X1、X2 和 X3 类名在 AndroidManifest.xml 中声明活动

于 2013-03-14T17:21:35.323 回答
0

尝试这个:

Intent intent = new Intent();
intent.setClassName("com.lab.example", "com.lab.example." + openClass);
startActivity(intent);
于 2013-03-14T19:29:05.983 回答