2013-04-09 141 views
0

我有一個名爲main_activity.xml的活動和兩個名爲Main.java的java類& Menu.java。在Menu.java類中,我擴展了ListActivity類。當我運行我的應用程序時,它彈出強制關閉。 Plz的幫助。需要幫助以擴展ListActivity類運行我的Android應用程序

下面是Menu.java的代碼:

公共類菜單擴展ListActivity {

String classes[]={"Main","Second","Third","China"}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes)); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 

    String selectedItem= classes[position]; 
    try{ 
     Class myClass = Class.forName("com.example.myapp."+selectedItem); 
     Intent myIntent=new Intent(Menu.this, myClass); 
     startActivity(myIntent); 
    } 
    catch(ClassNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
} 

}

Main.java類代碼:

公衆班級主要延伸活動{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Intent intent=new Intent("com.example.myapp.Menu"); 
    startActivity(intent); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

Mainfest文件代碼:

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

<application 
    <activity 
     android:name="com.example.myapp.Main" 
     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="com.example.myapp.Menu" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MENU" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 


</application> 

錯誤管理單元排序:

enter image description here

回答

0

未定義活動來處理意圖。這可能是因爲你沒有在你的onCreate中正確地啓動你的意圖。嘗試這樣的事情

Intent intent = new Intent(Main.this,Menu.class) 
Main.this.startActivity(intent); 

編輯:當然我也會必然要求有你的清單中聲明你的活動?

0

一切都很好,在我們的代碼。當你打電話給其他班時,你沒有明確指出。 任何方式使用下面的示例代碼,它工作正常..

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MyListActivity extends ListActivity { 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
     "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
     "Linux", "OS/2" }; 
    // Use your own layout 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
     R.layout.rowlayout, R.id.label, values); 
    setListAdapter(adapter); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    String item = (String) getListAdapter().getItem(position); 
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 
    } 
} 

If u want to show it in your own layout use below rowlayout.xml file 

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

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="22px" 
     android:layout_height="22px" 
     android:layout_marginLeft="4px" 
     android:layout_marginRight="10px" 
     android:layout_marginTop="4px" 
     android:src="@drawable/ic_launcher" > 
    </ImageView> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+id/label" 
     android:textSize="20px" > 
    </TextView> 

</LinearLayout> 

現在ü可以使用呼叫按鈕該類onclicklistner與類名。

我希望它能幫助:)

0

改變這樣

Intent intent=new Intent(Main.this, Menu.class); 
startActivity(intent); 

更改艙單這樣

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

<application 
<activity 
    android:name="com.example.myapp.Main" 
    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="com.example.myapp.Menu" > 
</activity> 



</application>