Callum
Callum

Reputation: 31

Unable to instantiate activity ComponentInfo, android using java

UPDATE

Like an idiot I did not pay attention to the logCat. The problem was coming from the page I was navigating to, not the intent itself, I was declaring some code before the setContentView code has a chance to run within onContentLoad().

Thanks for all the help!

I know this question has been asked before, but I have followed numerous posts about this topic; but can't seem to find the right answer. I am trying to navigate from my MainActivity page, to my ViewFeeds page; but it is throwing the above error.

The funny thing is, I have already got the code to navigate between pages, and it works. But when I come to duplicate this code for another class, it throws the above error.

MainActivity Code (The one I am navigating from):

 public void btnFeedsClick(View v)
{
        Button button = (Button) findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
        @Override

            public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ViewFeeds.class);                
            MainActivity.this.startActivity(intent);

            }

        });
}

The ViewFeeds class (The one I am navigating to):

  package com.example.rssapplication;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ViewFeeds extends Activity{
    //The array which holds all of the data...
    ArrayList<String> items = new ArrayList<String>();
    ListView lv = (ListView) findViewById(R.id.listView1);

    protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
            setContentView(R.layout.viewfeeds);

            ActionBar actionBar = getActionBar();
            actionBar.hide();
            items = new ArrayList<String>();
            String ret = "";
            try {

                InputStream inputStream = openFileInput("Teams.txt");

                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();
              //  Toast.makeText(MainActivity.this, receiveString, Toast.LENGTH_LONG).show();   

                try {
                    while ( (receiveString = bufferedReader.readLine()) != null ) {
                        items.add(receiveString);
                        stringBuilder.append(receiveString + "\r\n");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                inputStream.close();
                //ret holds each line of text from the file...
                ret = stringBuilder.toString();
                TextView txt = (TextView) findViewById(R.id.textView6);
                txt.setText(ret);

                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                        this, 
                        android.R.layout.simple_list_item_1,
                        items);

                lv.setAdapter(arrayAdapter); 


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



   }
}

The android Manifest (I have put a comment above the section in which I am declaring the reference):

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>    
 <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

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

        <activity android:name="com.example.rssapplication.SelectTeams" android:label="@string/app_name">  
         <intent-filter> 
           <action android:name="android.intent.action.MainActivity" />  
             <category android:name="android.intent.category.DEFAULT" />  
         </intent-filter>   
      </activity>




      <!-- This is where I am creating the intent to navigate from main, to viewfeeds -->

        <activity android:name="com.example.rssapplication.ViewFeeds" android:label="@string/app_name">  
         <intent-filter> 
           <action android:name="android.intent.action.MainActivity" />  
             <category android:name="android.intent.category.DEFAULT" />  
         </intent-filter>   
      </activity>





        <activity android:name="com.example.rssapplication.ShowArticle" android:label="@string/app_name"
            android:parentActivityName="com.example.rssapplication.MyAdapter"
            >
         <!--  <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.rssapplication.MyAdapter" />-->
        <!-- <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter> -->    
    </activity>


    </application>

</manifest>

This is he logcat:

    08-07 16:27:02.512: E/AndroidRuntime(2321): FATAL EXCEPTION: main
08-07 16:27:02.512: E/AndroidRuntime(2321): Process: com.example.rssapplication, PID: 2321
08-07 16:27:02.512: E/AndroidRuntime(2321): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.rssapplication/com.example.rssapplication.ViewFeeds}: java.lang.NullPointerException
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.os.Looper.loop(Looper.java:157)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.ActivityThread.main(ActivityThread.java:5356)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at java.lang.reflect.Method.invoke(Method.java:515)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at dalvik.system.NativeStart.main(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321): Caused by: java.lang.NullPointerException
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.Activity.findViewById(Activity.java:1965)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at com.example.rssapplication.ViewFeeds.<init>(ViewFeeds.java:20)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at java.lang.Class.newInstanceImpl(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at java.lang.Class.newInstance(Class.java:1208)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
08-07 16:27:02.512: E/AndroidRuntime(2321):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
08-07 16:27:02.512: E/AndroidRuntime(2321):     ... 11 more

Any help will be greatly appreciated!

Thanks,

Callum

Upvotes: 0

Views: 78

Answers (2)

Kaushik
Kaushik

Reputation: 6162

1st problem declare this inside onCreate(...) after setContentView(R.layout.ur_layout)

ListView lv = (ListView) findViewById(R.id.listView1);

2nd problem as your application's android:minSdkVersion="8" you should use

ActionBar actionBar = getSupportActionBar(); 

instead of Activity you have to extends ActionBarActivity of appcompat v7 support library ActionbarActivity

How to import appcompat into your project

Upvotes: 0

Xaver Kapeller
Xaver Kapeller

Reputation: 49807

The problem is this:

ListView lv = (ListView) findViewById(R.id.listView1);

You are calling findViewById() too early! You always have to be aware of the Activity Lifecycle! findViewById() can only be called after you set a layout with setContentView().

You should look for Views in onCreate() like this:

public class ViewFeeds extends Activity {
    private ArrayList<String> items = new ArrayList<String>();
    private ListView lv;

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

        lv = (ListView) findViewById(R.id.listView1);

        ...
    }

    ...
}

Upvotes: 1

Related Questions