2014-06-25 73 views
0

可能的錯誤是什麼?活動不會啓動,每次運行應用程序時強制關閉。我是否需要在嘗試和捕捉塊中進行一些更改或者是否是其他內容....?列表活動未啓動android

import org.example.emergencyhandler.R; 

import android.R.string; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.content.res.Resources.NotFoundException; 
import android.os.Bundle; 
public class Main extends ListActivity { 
String arr[] = { "Medical Emergency", "example1", "example2", "example3", 
     "example4", "example5", "example6", "example7", "example8", 
     "example9" }; 

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // ListView listview=(ListView)findViewById(R.id.lv); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.this, android.R.layout.simple_list_item_2, arr); 
    setListAdapter(adapter); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    super.onListItemClick(l, v, position, id); 

    String toast=arr[position]; 
    try { 
     Toast.makeText(getApplicationContext(), toast + "clicked", Toast.LENGTH_SHORT) 

     .show(); 

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

logcat的上述類 的: :

1.06-25 15:33:36.992: D/AndroidRuntime(24847): Shutting down VM 
    2.06-25 15:33:36.992: W/dalvikvm(24847): threadid=1: thread exiting with uncaught exception<br/> (group=0x40018578) 
    3.06-25 15:33:37.046: E/AndroidRuntime(24847): FATAL EXCEPTION: main 
    4.06-25 15:33:37.046: E/AndroidRuntime(24847): java.lang.RuntimeException: Unable to instantiate<br/> activity<br/> ComponentInfo{org.example.emergencyhandler/org.example.emergencyhandler.Main}:<br/> java.lang.ClassNotFoundException: org.example.emergencyhandler.Main in loader <br/>dalvik.system.PathClassLoader[/data/app/org.example.emergencyhandler-1.apk] 
    5.06-25 15:33:37.046: E/AndroidRuntime(24847): at<br/> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573) 
    6.06-25 15:33:37.046: E/AndroidRuntime(24847): at<br/> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
    7.06-25 15:33:37.046: E/AndroidRuntime(24847): at <br/>android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    8.06-25 15:33:37.046: E/AndroidRuntime(24847): at<br/> android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
    9.06-25 15:33:37.046: E/AndroidRuntime(24847): at <br/>android.os.Handler.dispatchMessage(Handler.java:99) 
    10.06-25 15:33:37.046: E/AndroidRuntime(24847): at android.os.Looper.loop(Looper.java:130)<br/> 
    11.06-25 15:33:37.046: E/AndroidRuntime(24847): at android.app.ActivityThread.main(ActivityThread.java:3687) 
    12.06-25 15:33:37.046: E/AndroidRuntime(24847): at java.lang.reflect.Method.invokeNative(Native Method) 
    13.06-25 15:33:37.046: E/AndroidRuntime(24847): at java.lang.reflect.Method.invoke(Method.java:507) 
    14.06-25 15:33:37.046: E/AndroidRuntime(24847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
    15.06-25 15:33:37.046: E/AndroidRuntime(24847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
    16.06-25 15:33:37.046: E/AndroidRuntime(24847): at dalvik.system.NativeStart.main(Native Method) 
    17.06-25 15:33:37.046: E/AndroidRuntime(24847): Caused by: java.lang.ClassNotFoundException:<br/> org.example.emergencyhandler.Main in loader dalvik.system.PathClassLoader[/data/app/org.example.emergencyhandler-1.apk]<br/> 
    18.06-25 15:33:37.046: E/AndroidRuntime(24847): at <br/>dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
    19.06-25 15:33:37.046: E/AndroidRuntime(24847): at <br/>java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
    20.06-25 15:33:37.046: E/AndroidRuntime(24847): at<br/> java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
    21.06-25 15:33:37.046: E/AndroidRuntime(24847): at <br/>android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
    22.06-25 15:33:37.046: E/AndroidRuntime(24847): at<br/> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565) 
    23.06-25 15:33:37.046: E/AndroidRuntime(24847): ... 11 more 

上述申請的清單文件: :

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="org.example.emergencyhandler.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> 
</application> 

+1

哪裏是logcat的錯誤?上課在哪裏? – Hariharan

+0

可以顯示logcat輸出嗎? – Opiatefuchs

+0

嘗試我的下面的代碼工作 – Mourice

回答

0

反應以在選擇列表se T時的OnItemClickListener您的ListView

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
     int position, long id) { 
     Toast.makeText(getApplicationContext(), 
     "Click ListItem Number " + position, Toast.LENGTH_LONG) 
     .show(); 
     } 
     }); 
+0

從標題本身OP說'ListActivity'。 – Hariharan

0

試試這個代碼

public class MainActivity extends ListActivity { 

String[] arr = new String[] { "Medical Emergency", "example1", "example2", "example3", 
     "example4", "example5", "example6", "example7", "example8", 
     "example9" }; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arr); 
    setListAdapter(adapter); 
} 


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

} 
+0

此代碼不能正常工作 – user3503299

+0

我已經測試過上面的代碼正在工作,併爲您的代碼工作評論此行「setContentView(R.layout.activity_main);」並用「android.R.layout.simple_list_item_1」替換「android.R.layout.simple_list_item_2」 – Mourice

+0

仍然無法正常工作.....是否有清單文件中的任何問題??? – user3503299