2011-08-28 53 views
0

我的Android應用程序似乎無法啓動。我在主要活動中進行了一些代碼更改,嘗試啓動它,並在啓動時崩潰。然後我刪除了這些代碼更改,它仍然不會啓動。我找不到任何錯誤的代碼,這裏是主要活動:Android應用程序未啓動,意外停止

public class MainActivity extends TabActivity implements OnClickListener { 


Button doSomething; 
TabHost tabHost; 
ViewFlipper flipper; 
ListView listview; 
ListView listview2; 
HttpResponse re; 
String json; 
JSONObject j; 
ListAdapter adapter2; 



/*==================*/ 

ListView lazyList; 
LazyAdapter lazyAdapter; 
/*==================*/ 


private static final String LOG_TAG = "App"; 



@SuppressWarnings({ "unchecked", "rawtypes" }) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 




    super.onCreate(savedInstanceState); 




    doSomething = (Button) findViewById(R.id.btn_do_something); 
    doSomething.setOnClickListener(this); 

    flipper = (ViewFlipper) findViewById(R.id.layout_tab_one); 

    listview = (ListView) findViewById(R.id.listview); 

    listview2 = (ListView) findViewById(R.id.listview2); 


    /*==================================*/ 

    lazyList=(ListView)findViewById(R.id.lazylistview); 


    //@SuppressWarnings("unchecked") 
    ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetch()); 
    adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetchList1()); 



    listview.setAdapter(adapter); 
    listview2.setAdapter(adapter2); 

    flipper.setOnClickListener(this); 

    String tabname1 = getString(R.string.tabexample_tab1); 
    String tabname2 = getString(R.string.tabexample_tab2); 
    String tabname3 = getString(R.string.tabexample_tab3); 
    String tabname4 = getString(R.string.tabexample_tab4); 

    tabHost = getTabHost(); 
    tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.layout_tab_one).setIndicator(tabname1)); 
    tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.layout_tab_two).setIndicator(tabname2)); 
    tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.layout_tab_three).setIndicator(tabname3)); 
    tabHost.addTab(tabHost.newTabSpec("tab4").setContent(R.id.layout_tab_four).setIndicator(tabname4)); 

    tabHost.setCurrentTab(0); 




    listview.setOnItemClickListener(new OnItemClickListener(){ 

     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 



      Object item = listview.getItemAtPosition(position); 






      SavePreferences("item",item.toString()); 
      MainActivity.this.refreshList(); 







      flipper.showNext(); 


     }}); 

    listview2.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
      flipper.showPrevious(); 


     }}); 

    lazyList.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
      flipper.showPrevious(); 


     }}); 








} 





public ArrayList<String> fetch() 
{ 
    ArrayList<String> listItems = new ArrayList<String>(); 

    try { 
     URL twitter = new URL(
       "JSON.php"); 
     URLConnection tc = twitter.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 




     String line; 
     while ((line = in.readLine()) != null) { 







     //make sure youe String line is completely filled after that.. 
     if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
     { 

     JSONArray jArray = new JSONArray(line); 
     Log.v(LOG_TAG,"jarray value: " + jArray); 
     for (int i = 0;i < jArray.length(); i++) 
     { 

     //SONObject jobj = jArray.getJSONObject(i); 
      String country = jArray.getString(i); 
      listItems.add(country); 
     // also make sure you get the value from the jsonObject using some key 
     // like, jobj.getString("country"); 


     //istItems.add(jobj.getString("")); 

     } 
      } 

     } 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return listItems; 
} 


public ArrayList<String> fetchList1() 
{ 
    ArrayList<String> listItems = new ArrayList<String>(); 

    try { 

     SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); 
     String selected = sharedPreferences.getString("item", ""); 



     URL twitter = new URL(
       "JSON2.php?item=" + selected); 

     URLConnection tc = twitter.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 




     String line; 
     while ((line = in.readLine()) != null) { 







     //make sure youe String line is completely filled after that.. 
     if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
     { 

     JSONArray jArray = new JSONArray(line); 
    // Log.v(LOG_TAG,"jarray value: " + jArray); 
     for (int i = 0;i < jArray.length(); i++) 
     { 

     //SONObject jobj = jArray.getJSONObject(i); 
      String country = jArray.getString(i); 
      listItems.add(country); 
     // also make sure you get the value from the jsonObject using some key 
     // like, jobj.getString("country"); 


     //istItems.add(jobj.getString("")); 

     } 
      } 

     } 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return listItems; 
} 


public ArrayList<String> fetchImages() 
{ 

    ArrayList<String> listItems = new ArrayList<String>(); 
    try { 

     SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); 
     String selected = sharedPreferences.getString("item", ""); 



     URL twitter = new URL(
       "ImageJSON.php?item="+ selected); 
     Log.v(LOG_TAG,"URL**:" + twitter.toString()); 
     URLConnection tc = twitter.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 




     String line; 
     while ((line = in.readLine()) != null) { 







     //make sure youe String line is completely filled after that.. 
     if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
     { 

     JSONArray jArray = new JSONArray(line); 
    // Log.v(LOG_TAG,"jarray value: " + jArray); 
     for (int i = 0;i < jArray.length(); i++) 
     { 

     //SONObject jobj = jArray.getJSONObject(i); 
      String country = jArray.getString(i); 
      listItems.add(country); 
     // also make sure you get the value from the jsonObject using some key 
     // like, jobj.getString("country"); 


     //istItems.add(jobj.getString("")); 

     } 
      } 

     } 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return listItems; 
} 


public ArrayList<String> fetchList2() 
{ 

    ArrayList<String> listItems = new ArrayList<String>(); 
    try { 

     SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); 
     String selected = sharedPreferences.getString("item", ""); 



     URL twitter = new URL(
       "JSON3.php?item="+ selected); 

     URLConnection tc = twitter.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 




     String line; 
     while ((line = in.readLine()) != null) { 







     //make sure youe String line is completely filled after that.. 
     if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
     { 

     JSONArray jArray = new JSONArray(line); 
    // Log.v(LOG_TAG,"jarray value: " + jArray); 
     for (int i = 0;i < jArray.length(); i++) 
     { 

     //SONObject jobj = jArray.getJSONObject(i); 
      String country = jArray.getString(i); 
      listItems.add(country); 
     // also make sure you get the value from the jsonObject using some key 
     // like, jobj.getString("country"); 


     //istItems.add(jobj.getString("")); 

     } 
      } 

     } 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return listItems; 
} 







public void refreshList() { 

    /*adapter2 = null; 
    adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetchList1()); 
    listview2.setAdapter(adapter2);*/ 
    lazyAdapter = null; 
    String[] imagesList = new String[this.fetchList1().size()]; 
     imagesList = this.fetchImages().toArray(imagesList); 
     ArrayList<String> v = this.fetchList1(); 
     ArrayList<String> t = this.fetchList2(); 
     lazyAdapter=new LazyAdapter(this,imagesList,v,t); 
     lazyList.setAdapter(lazyAdapter); 
    //adapter2.notifyDatasetChanged(); 

} 


    private void SavePreferences(String key, String value){ 
     SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString(key, value); 
     editor.commit(); 
     } 





@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

} 

public void onBackPressed() 
{  
    flipper.showPrevious(); 

    } 






} 

這是清單;

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    android:versionCode="1" 
    android:versionName="1.0" package="com.myApp.android"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".MainActivity" 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=".MainActivity" /> 
     <activity android:name=".LazyAdapter" /> 
     <activity android:name=".ImageLoader" /> 
    </application> 
    <uses-sdk android:minSdkVersion="3"/> 
    <uses-permission 
     android:name="android.permission.INTERNET" /> 
</manifest> 

登錄貓輸出:

08-28 11:16:32.442: ERROR/AndroidRuntime(517): FATAL EXCEPTION: main 
08-28 11:16:32.442: ERROR/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myApp.android/com.myApp.android.MainActivity}: java.lang.NullPointerException 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.app.ActivityThread.access$1500(ActivityThread.java:122) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.os.Looper.loop(Looper.java:132) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.app.ActivityThread.main(ActivityThread.java:4025) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at java.lang.reflect.Method.invoke(Method.java:491) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at dalvik.system.NativeStart.main(Native Method) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517): Caused by: java.lang.NullPointerException 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at com.android.MainActivity.onCreate(MainActivity.java:84) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 
08-28 11:16:32.442: ERROR/AndroidRuntime(517):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712) 
+0

哪一行是「MainActivity.java 84」? – manelizzard

+0

doSomething.setOnClickListener(this); – EI756

回答

1

那是因爲你有一個空指針。該行doSomething = (Button) findViewById(R.id.btn_do_something);返回null。檢查Button ID是否真的是「btn_do_something」或者是否有多個相同的ID分配給不同的按鈕

+0

這樣做的伎倆,出於某種原因setContentView不叫 – EI756