从Android市场首次安装Android应用程序的问题

时间:2011-04-24 22:34:58

标签: android android-manifest google-play

我正在访问互联网以获取信息。该应用程序在我访问互联网时强行关闭。如果应用程序被删除并重新安装,则在我访问互联网时不会强行关闭。这是在第一次安装应用程序时发生的。请帮我解决这个问题。

这是堆栈跟踪 -

java.lang.RuntimeException: Unable to start activity ComponentInfo{ListOfDealSites}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.io.DataInputStream.readLine(DataInputStream.java:309)
at ListOfDealSites.onCreate(ListOfDealSites.java:53)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

它还说源方法--DataInputStream.readLine()

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dealsites);

    InputStream in = null;
    try {
        // Connect to the server to get the list of deal sites
        in = OpenHttpConnection("Link to deal server");
        } 
    catch (IOException e1) {

        // TODO Auto-generated catch block          
        e1.printStackTrace();
    }

    try {

              DataInputStream dataIO = new DataInputStream(in);
        String strLine = null;
        int i = 0;

        while((strLine = dataIO.readLine())!= null) // Line 53------
        {
            count = i;
            NEWDEALSITES[i++] = strLine;

        }

        dataIO.close();
        in.close();
   } 
   catch (IOException e){ 

    // TODO Auto-generated catch block
    e.printStackTrace();
   }

   count_deals = count/2;
   newdeals = new String[count_deals+1];
   newsites = new String[count_deals+1];
   int j, k;
   for(j = 0, k = 0; k < count; j++)
   {
       newdeals[j] = NEWDEALSITES[k++];
       newsites[j] = NEWDEALSITES[k++];

   }

   listOfDealSites = (ListView)findViewById(R.id.ListView01);
   listOfDealSites.setAdapter(new ArrayAdapter<String>(this,R.layout.row,newdeals));



}

//Function to connect to the server.
private InputStream OpenHttpConnection(String urlString) 
throws IOException
{
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))                     
        throw new IOException("Not an HTTP connection");

    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 

        response = httpConn.getResponseCode();                 
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                                 
        }                     
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");            
    }
    return in;     
}

}

2 个答案:

答案 0 :(得分:1)

出于某种原因,OpenHttpConnection返回null,这基本上意味着你在这里没有得到OK回复......

response = httpConn.getResponseCode();                 
if (response == HttpURLConnection.HTTP_OK) {
    in = httpConn.getInputStream();                                 
}

这意味着当您尝试读取第53行的数据时,dataIO将无效。

从DataInputStream构造函数docs ...

  

public DataInputStream(InputStream   in)自:API Level 1

     

构造一个新的   在InputStream中的DataInputStream。

     

然后过滤所有读数   这个流。注意读取的数据   这个流不是人类可读的   格式,很可能是由   一个DataOutputStream。

     

警告:通过   null source创建无效   DataInputStream类。所有操作都在   这样的流会失败。

不确定导致此问题的原因或修复方法,但在尝试使用'in'InputStream创建DataInputStream之前,您肯定需要检查从OpenHttpConnection返回的null结果。

答案 1 :(得分:0)

问题是由于没有互联网连接造成的。我发现了异常,我发出了一条消息来检查连接。

谢谢大家的帮助。