【问题标题】:Problem with first installation of android application from android market从 android market 首次安装 android 应用程序的问题
【发布时间】:2011-04-24 22:34:57
【问题描述】:

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

这是堆栈跟踪 -

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;     
}

}

【问题讨论】:

  • @user588132:发布 ListOfDealSites.onCreate() 的代码并告诉我们第 53 行。
  • 变量 'in' 可能为空。您捕获了异常但继续使用 var,
  • 但问题只发生在第一次安装时。它是否必须进行任何设置。为什么不是一直发生?

标签: android android-manifest google-play


【解决方案1】:

由于某种原因,OpenHttpConnection 返回 null,这基本上意味着您在这里没有得到 OK 响应...

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

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

来自 DataInputStream 构造函数文档...

公共数据输入流(输入流 in) 自:API 级别 1

构造一个新的 DataInputStream 在 InputStream 中。

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

警告:传递一个 空源创建一个无效的 数据输入流。上的所有操作 这样的流会失败。

不确定是什么原因造成的或如何修复它,但您肯定需要在尝试使用“in”InputStream 来创建 DataInputStream 之前检查从 OpenHttpConnection 返回的空结果。

【讨论】:

  • 为什么只发生第一次?如何优雅地关闭应用程序并要求用户重试?
  • @user588132:我不知道——我也很困惑。所以你是说完全卸载/重新安装可以解决问题?在旁注中,尝试记录 OpenHttpConnection 方法中的“响应”。如果不是 HTTP.OK,那么了解它是什么可能会很有趣。
  • 是的,就是这样...我不知道如何重新创建错误。是否与我需要添加的某些设置有关。
【解决方案2】:

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

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多