2012-10-18 97 views
2

好的,我正在開發一個Android應用程序,可以向您展示一些使用RSS的電視節目,新聞,Wather Forecast和星座運勢等內容。該應用程序的工作原理應該是在Android 2.1或2.3.X上,但是當我在4.0或4.1上啓動它時,在我從main中選擇任何選項後它會停止工作。Android應用程序無法在Android 4.0上啓動

應用程序的結構是:

- 主要

-activity具體的事情(像剛纔新聞),有一個觀點

由以前的類

稱爲

-XML解析器類

-Signature View的活動(如同整條新聞中的一條信息)

這裏我提供了Manifest,L OG和幾類:

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

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="14" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".Vodic" 
     android:label="@string/title_activity_pocetna" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".Pomoc" 
     android:label="@string/m_tel_vodi_za_odlu_ne" 
     ></activity> 
    <activity 
     android:name=".Pomocna" 
     android:label="@string/m_tel_vodi_za_odlu_ne" 
     ></activity> 
    <activity 
     android:name=".SingleMenuItemActivity" 
     android:label="@string/m_tel_vodi_za_odlu_ne" 
     ></activity> 
    . 
    . 
    . 

</application> 

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

<!-- Needed to check when the network connection changes --> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 

</manifest> 

錯誤日誌:

10-18 20:35:05.457: E/Trace(730): error opening trace file: No such file or directory  (2) 
10-18 20:35:06.197: I/Choreographer(730): Skipped 30 frames! The application may be doing too much work on its main thread. 
10-18 20:35:06.257: D/gralloc_goldfish(730): Emulator without GPU emulation detected. 
10-18 20:35:11.316: D/AndroidRuntime(730): Shutting down VM 
10-18 20:35:11.316: W/dalvikvm(730): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
10-18 20:35:11.347: E/AndroidRuntime(730): FATAL EXCEPTION: main 
10-18 20:35:11.347: E/AndroidRuntime(730): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mtel.vodic/com.mtel.vodic.Vijesti}: java.lang.NullPointerException 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.os.Looper.loop(Looper.java:137) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.ActivityThread.main(ActivityThread.java:4745) 
10-18 20:35:11.347: E/AndroidRuntime(730): at java.lang.reflect.Method.invokeNative(Native Method) 
10-18 20:35:11.347: E/AndroidRuntime(730): at java.lang.reflect.Method.invoke(Method.java:511) 
10-18 20:35:11.347: E/AndroidRuntime(730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-18 20:35:11.347: E/AndroidRuntime(730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-18 20:35:11.347: E/AndroidRuntime(730): at dalvik.system.NativeStart.main(Native Method) 
10-18 20:35:11.347: E/AndroidRuntime(730): Caused by: java.lang.NullPointerException 
10-18 20:35:11.347: E/AndroidRuntime(730): at com.mtel.vodic.Vijesti.onCreate(Vijesti.java:61) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.Activity.performCreate(Activity.java:5008) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
10-18 20:35:11.347: E/AndroidRuntime(730): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
10-18 20:35:11.347: E/AndroidRuntime(730): ... 11 more 
10-18 20:35:13.926: I/Process(730): Sending signal. PID: 730 SIG: 9 

主營:

public class Vodic extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pocetna); 
    Button tv = (Button)findViewById(R.id.tv); 
    Button vijesti = (Button)findViewById(R.id.vijesti); 
    Button horoskop = (Button)findViewById(R.id.horoskop); 
    Button vremenska_prognoza = (Button)findViewById(R.id.vremenska_prognoza); 
    Button o_aplikaciji = (Button)findViewById(R.id.o_aplikaciji); 
    Button pomoc = (Button)findViewById(R.id.pomoc); 

    . 
    . 
    . 

    vijesti.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      Intent xw = new Intent(getApplicationContext(), Vijesti.class); 
      //xw.putExtra("A", "http://bih-x.info/feed/"); 
      xw.putExtra("A", "http://klix.ba/rss/naslovnica"); 
      if(!isOnline()) { 
       Toast.makeText(getApplicationContext(), "Niste povezani sa internetom!", Toast.LENGTH_SHORT).show(); 
      } else { 
       startActivity(xw); 
      } 
     } 
    }); 

    . 
    . 
    . 

public boolean isOnline() { 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if(netInfo != null && netInfo.isConnected()) { 
     return true; 
    } 
    return false; 
} 
} 

活動:

public class Vijesti extends ListActivity { 

static String url =null; 

// XML node keys 
static final String KEY_ITEM = "item"; // parent node 
static final String KEY_TITLE = "title"; 
static final String KEY_DATE = "pubDate"; 
static final String KEY_DESC = "encoded"; 
static final String UVOD = "uvod"; 
static final String CLANAK = "clanak"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.vijesti_m); 
    Intent in = getIntent(); 

    // Get XML values from previous intent 
    url = in.getStringExtra("A"); 
    final ArrayList<HashMap<String,String>> menuItems = new ArrayList<HashMap<String,String>>(); 
    ArrayList<String> xqw = new ArrayList<String>(); 

    ParserVijesti parser=null; 
    Document doc=null; 
    try { 
     parser = new ParserVijesti(); 
     String xml = parser.getXmlFromUrl(url); //get XML 
     doc = parser.getDomElement(xml); 
    } catch (Exception e1) { 
     finish(); 
    } 

    NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
    //loop 
    for (int i=0; i< nl.getLength(); i++){ 
     HashMap<String, String> map = new HashMap<String, String>(); 
     HashMap<String, String> mapq = new HashMap<String, String>(); 

     Element e = (Element) nl.item(i); 

     //add to map 
     map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); 
     map.put(KEY_DATE, parser.getValue(e, KEY_DATE)); 
     map.put(UVOD, parser.getValue(e,UVOD)); 
     map.put(CLANAK, parser.getValue(e,CLANAK)); 


     menuItems.add(map); 

     xqw.add(parser.getValue(e,KEY_TITLE)); 
    } 

    for(int gf=0; gf<xqw.size(); gf++){ 
     Log.w("ISPISI: ", xqw.get(gf)); 
    } 
    ArrayAdapter adapterx = new ArrayAdapter(this, R.layout.vijesti_m,R.id.tetkica, xqw); 


    setListAdapter(adapterx); 

    //singleView 
    ListView lv = getListView(); 

    lv.setOnItemClickListener(new OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 

      int hg = position; 
      HashMap<String, String> kaktus = menuItems.get(hg); 
      String uvod1 = kaktus.get(UVOD); 
      String clanak1 = kaktus.get(CLANAK); 
      String dat1 = kaktus.get(KEY_DATE); 
      String tit1 = kaktus.get(KEY_TITLE); 



      //intent 
      Intent inx = new Intent(getApplicationContext(), VijestiSingle.class); 
      inx.putExtra(KEY_TITLE, tit1); 
      inx.putExtra(KEY_DATE, dat1); 
      inx.putExtra(UVOD, uvod1); 
      inx.putExtra(CLANAK, clanak1); 
      startActivity(inx); 
     } 
    }); 


} 

} 

XML解析類:

public class ParserVijesti { 




// constructor 
public ParserVijesti() { 

} 

/** 
* Getting XML from URL making HTTP request 
* @param url string 
* */ 
public String getXmlFromUrl(String url) { 
    String xml = null; 

    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity, "UTF-8"); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // return XML 
    return xml; 
} 
/** 
* Getting XML DOM element 
* @param XML string 
* */ 

public Document getDomElement(String xml){ 
    Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    dbf.setCoalescing(true); 
    dbf.setNamespaceAware(true); 
    try { 
     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
     is.setByteStream(new ByteArrayInputStream(xml.getBytes("UTF-8"))); 
      doc = db.parse(is); 

     } catch (ParserConfigurationException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 

     return doc; 
} 

/** Getting node value 
    * @param elem element 
    */ 
public final String getElementValue(Node elem) { 
    Node child; 
    if(elem != null){ 
     if (elem.hasChildNodes()){ 
      for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
       if(child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE){ 
        return child.getNodeValue(); 
       } 
      } 
     } 
    } 
    return ""; 
} 

public final String getElementValue2(Node elem) { 
    Node child; 
    if(elem != null){ 
     if (elem.hasChildNodes()){ 
      for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
       if(child.getNodeType() == Node.CDATA_SECTION_NODE){ 
        return child.getNodeValue(); 
       } 
      } 
     } 
    } 
    return "SRANJE"; 
} 

/** 
    * Getting node value 
    * @param Element node 
    * @param key string 
    * */ 



public String getValue(Element item, String str) {  
     NodeList n = item.getElementsByTagName(str);     
     return this.getElementValue(n.item(0)); 
    } 


public String getValue3(Element item, String str){ 
    NodeList n = item.getElementsByTagNameNS("http://purl.org/rss/1.0/modules/content/", str); 
    String ses = this.getElementValue2(n.item(0)); 

    //String mim =ses.replaceAll("(?s)\\<.*?\\>", " \n"); 
    String html = ses; 
    Spanned strxa = Html.fromHtml(html); 
    String fffx=strxa.toString(); 

    //return this.getElementValue2(n.item(0)); 
    //return ses; 
    //return Promjena(ses); 
    return fffx; 
} 


} 

總結:應用程序可以在2.1或2.3.x上正常運行,但在Android 4.0或4.1的主要活動中選擇任何內容後,應用程序將停止運行。我不知道這是怎麼回事。任何人都可以幫忙嗎?

+2

這是'Vijesti.java'中的第61行嗎?你在那裏得到一個空指針。 – Geobits

+1

維謝斯蒂的61線有什麼? '引發:java.lang.NullPointerException在com.mtel.vodic.Vijesti.onCreate(Vijesti.java:61)' – dmon

+0

爲什麼你的「公共類Vijesti擴展ListActivity」沒有聲明清單文件?它被宣佈爲 –

回答

0

這不會解決你原來的問題,而是:

我覺得這是您有一個問題:你是用高於你已經測試並確認您的應用程序針對工作targetSdkVersion發佈應用程序。

你有targetSdkVersion設置爲14,但是如果你這樣說的:

Android Min SDK Version vs. Target SDK Version

你就會明白,設置targetSdkVersion到14意味着你告訴Android的「我已經測試我的應用程序的API 14並確認它正在工作,請禁用所有兼容性設置,版本低於API 14「。

但事實並非如此,事實證明相反,該應用程序不適用於4.0!

如果設置targetSdkVersion至14這將使它正常使用,確保所有應用對老年人的API工作(FI GPU渲染,JNI改變)

所以現在你應該剛剛成立的minSdkVersion和Android 4.0禁用兼容性設置當您發佈您的應用時,請勿使用targetSdkVersion,以便在4.0中保持兼容性行爲的可用性。

只有在修復了原始錯誤並針對API 14測試了應用程序之後,纔將targetSdkVersion升至14。

+0

這真的很有效!我剛剛刪除了targetSdkVersion ...謝謝你 – Igx33

+0

從長遠來看,這是一個可怕的想法。開發/維護應用程序時,您應始終針對Android的最新版本(目前爲16)。如果您忽略Google推薦的這種良好做法,它遲早會讓您受到嘲笑。 – Teovald

+0

@Teovald:是的,你說的沒錯,當然你應該確保你的應用程序可以在沒有兼容性設置的情況下使用最新的SDK。但是,除非你這樣做,否則不要提高targetSdk版本,高於你在發佈應用程序時測試的版本,否則你會讓你的用戶進行兼容性測試。 –

1

根據日誌,你得到一個空指針Vijesti.java:61。你說這條線是:

NodeList nl = doc.getElementsByTagName(KEY_ITEM); 

這意味着doc必須爲空。既然你用getDomElement()來指定它,這意味着你的問題在於這個方法。我看到它可能爲空的唯一方法(因爲DocumentBuilder.parse()不能返回空值)是如果你正在擊中你的catch陳述之一。

您是否在logcat中獲得了catch異常消息?您顯示的部分僅列出了由空指針產生的實際錯誤。你的消息可能會在那之前。檢查一下,你就可以知道你得到了哪個異常,並相應地處理它。

+0

啊哈!你明白了。第61行之前的唯一catch語句是在失敗時調用finish()的語句。 OP可能認爲finish()就像exit()並且會導致執行立即終止。實際上,finish()仍然會返回給調用者(系統將很快終止該應用程序,但不會立即結束)。該代碼還應該執行Log.e()並返回;調用finish()後。 –

+0

你確定這是怎麼回事?這是可能的,但似乎更可能的是'getDomElement()'中的'catch'塊之一正在觸發,因爲所有'return null'(被賦值給'doc')並且*不*結束,很好地解釋了一個空指針。 – Geobits

+0

我同意。 OP應該在這裏檢查null。 –

相關問題