0

I just started learning android from past week and i m enjoying it very well too. But i m facing a problem from the past 2 days and i couldn't find any solutions. Actually i don't even know what is the problem. so i m posting here the codes and everything. I wish u guys help me out.

Here is the Activity.

package com.example.androidaspect;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tv = (TextView)findViewById(R.id.tvRes);
    HttpClient httpclient;
    HttpGet request;
    HttpResponse response;
    String url; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        url = "https://www.google.co.in/?gfe_rd=cr&ei=o7s5U8fxBIrV8gfbzIHYCw";

        try {
            httpclient = new DefaultHttpClient();
            request = new HttpGet(url);
            response = httpclient.execute(request);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while((line=rd.readLine())!=null){
                tv.append(line);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Here is the xml file.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvRes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

Here is the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidaspect"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidaspect.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>
    </application>

</manifest>

Here is the log file

04-01 01:52:51.291: D/AndroidRuntime(457): Shutting down VM
04-01 01:52:51.291: W/dalvikvm(457): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-01 01:52:51.402: E/AndroidRuntime(457): FATAL EXCEPTION: main
04-01 01:52:51.402: E/AndroidRuntime(457): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidaspect/com.example.androidaspect.MainActivity}: java.lang.NullPointerException
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.os.Looper.loop(Looper.java:123)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 01:52:51.402: E/AndroidRuntime(457):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 01:52:51.402: E/AndroidRuntime(457):  at java.lang.reflect.Method.invoke(Method.java:521)
04-01 01:52:51.402: E/AndroidRuntime(457):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 01:52:51.402: E/AndroidRuntime(457):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 01:52:51.402: E/AndroidRuntime(457):  at dalvik.system.NativeStart.main(Native Method)
04-01 01:52:51.402: E/AndroidRuntime(457): Caused by: java.lang.NullPointerException
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.Activity.findViewById(Activity.java:1637)
04-01 01:52:51.402: E/AndroidRuntime(457):  at com.example.androidaspect.MainActivity.<init>(MainActivity.java:17)
04-01 01:52:51.402: E/AndroidRuntime(457):  at java.lang.Class.newInstanceImpl(Native Method)
04-01 01:52:51.402: E/AndroidRuntime(457):  at java.lang.Class.newInstance(Class.java:1429)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-01 01:52:51.402: E/AndroidRuntime(457):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-01 01:52:51.402: E/AndroidRuntime(457):  ... 11 more
Tyagi
  • 13
  • 4

1 Answers1

0

You should post your logcat output any time the app crashes. It makes finding the error a lot easier. Anyhow, I see two major things wrong right away. You can't initialize your Views until after you have inflated your layout which means it should go after setContentView().

public class MainActivity extends Activity {

TextView tv; // you can declare here


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.tvRes);  // but initialize here

The way you have it, you will get a NPE when you try to call a method on your View.

The other big problem I see is that you need to do your network stuff on a background Thread. AsyncTask can be very handy for this.

Example of setting up an AsyncTask

AsyncTask Docs

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I have added the log file too is it is of any use. – Tyagi Mar 31 '14 at 20:24
  • Yes, it is because of where you call `findViewById()`. See my answer. Also, do something about that networking on the main thread as I also pointed out in my answer. – codeMagic Mar 31 '14 at 20:26
  • Thank you for your time. and about your suggestion, are u saying that i should add the buffered reader thing to the AsyncTask too? – Tyagi Mar 31 '14 at 20:43
  • Yes, that can all go into `doInBackground()` then you call `onProgressUpdate()` with `publishProgress()` to update your `TextView`. Read over the docs so that you understand how `AsyncTask` works, see the example I posted, then post a question if you still aren't sure how it works. It may seem a little intimidating at first but it's a piece of cake once you understand it. – codeMagic Mar 31 '14 at 20:47