Android应用无法在Android 4.0上启动

时间:2012-10-18 20:57:58

标签: android android-4.0-ice-cream-sandwich

好的,我正在开发一款Android应用程序,它可以向您展示一些内容,如电视时间表,新闻,天气预报和使用RSS的星座运势。该应用程序可以在Android 2.1或2.3.X上运行,但是当我在4.0或4.1上启动时,它会在我从main选择任何选项后停止工作。

app的结构是:

- 主要

- 具有视图的特定事物(如新闻)的活动

-XML由前一个类

调用的Parser类

- Signle View的活动(就像一大堆新闻中的一条信息)及其视图

这里我提供了Manifest,Log和几个类:

<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 个答案:

答案 0 :(得分:2)

根据日志,你在Vijesti.java:61上得到一个空指针。你说这行是:

NodeList nl = doc.getElementsByTagName(KEY_ITEM);

这意味着doc必须为null。由于您使用getDomElement()进行分配,这意味着您的问题在于该方法。我看到它可能为null的唯一方法(因为DocumentBuilder.parse()不能返回null)是你正在点击你的catch语句之一。

您是否在logcat中收到任何catch异常消息?您显示的部分仅列出空指针产生的实际错误。你的消息可能会在此之前。检查一下,你就能分辨出你得到的异常,并据此处理。

答案 1 :(得分:0)

这不会解决您的原始问题,但是:

我认为这是您遇到的另一个问题:您发布的应用程序的targetSdkVersion高于您测试的版本,并确认您的应用程序可以使用。

您已将targetSdkVersion设置为14.但如果您阅读此内容:

Android Min SDK Version vs. Target SDK Version

你会理解将targetSdkVersion设置为14意味着你正在告诉Android“我已经在API 14上测试了我的应用程序并确认它正在运行,请禁用低于API 14的版本的所有兼容性设置”。

但事实并非如此,你实际上确认了相反,该应用程序不适用于4.0!

如果将targetSdkVersion设置为14,它将使Android 4.0禁用其通常用于确保旧API的应用程序工作的兼容性设置(f.i. GPU渲染,JNI更改)

所以现在你应该在发布应用时设置minSdkVersion而不是targetSdkVersion,以保持4.0中的兼容性行为。

只有在您修复原始错误并针对API 14测试您的应用程序之后,才将targetSdkVersion提升为14。