0

I am a beginner with android, so bear my lack of knowledge I am working on a project, which has been working fine, when after few small changes it started giving me a RuntimeException unable to instantiate activity ComponentInfo{com.id11298775.exercise5/com.id11298775.excercise5.MainActivity}: java.lang.NullPointerException. I have reverted the small changes, which were in an adapter but still I cannot run the app. Here is my catLog: enter image description here

I have tried to figure out what is wrong with my main activity for quite a long time but to me everything seems fine, so I guess I am missing something This is my MainActivity class:

package com.id11298775.exercise5;

// import statements are here



public class MainActivity extends Activity {

private final String URI = getString(R.string.URI);

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

    // get the subject name
    EditText subjectName = (EditText) findViewById(R.id.activity_main_subjectname_et);
    // associate editText with the context menu
    subjectName.setOnCreateContextMenuListener(this);

}

/**
 * Inflate the menu this adds items to the action bar
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * code that executes according to the menu item that is clicked
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // click help
    case R.id.help:
        help();
        return true;
        // click exit
    case R.id.exit:
        finish();

    default:
        return super.onOptionsItemSelected(item);
    }
}

/**
 * when help is clicked this method will display a page related to the
 * subject number that is entered If no subject name is entered display the
 * general page of subjects of the handbook
 */
public void help() {
    EditText subjectNumber = (EditText) findViewById(R.id.activity_main_subjectnumber_et);
    String number = subjectNumber.getText().toString();
    String url = URI + number;

    Uri uriUrl = Uri.parse(url);
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);
}

/**
 * Inflate the context menu
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
    super.onCreateContextMenu(menu, v, menuInfo);
}

/**
 * code that executes according to the context menu item that is selected
 */
@Override
public boolean onContextItemSelected(MenuItem item) {
    EditText nameEd = (EditText) findViewById(R.id.activity_main_subjectname_et);
    switch (item.getItemId()) {
    // capitalize is selected
    case R.id.contextmenu_capitalize:
        String subjectName = nameEd.getText().toString();
        String upperCase = subjectName.toUpperCase();
        nameEd.setText(upperCase);
        return true;
        // clear is selected
    case R.id.contextmenu_clear:
        nameEd.setText("");
        return true;

    default:
        return super.onContextItemSelected(item);
    }
}

/**
 * methdo called when showList button is clicked
 * @param v
 */
public void showListButtonClicked(View v) {

    Intent intent = new Intent(MainActivity.this, SubjectList.class);
    startActivity(intent);
}

}

and this is my manifest:

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.id11298775.exercise5.MainActivity"
        android:label="@string/app_name" 
         android:theme="@android:style/Theme.Holo.Light">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.id11298775.exercise5.SubjectList"></activity>
</application>

</manifest>

Anybody is keen to help me please? Thank you for sharing your knowledge..

mikey
  • 1,339
  • 5
  • 22
  • 43
  • I had an adapter, and I wanted to access the strings.xml file within adapter. So I found that I needed to pass the context as a parameter in the adapter, I have done that. Then apps started breaking so I reverted the changes. Still the app doesn't run from the very beginning. I use the adapter only after a button form `Main_Activity` is pressed. – mikey Mar 30 '14 at 11:42

1 Answers1

2

Most likely this is your problem:

private final String URI = getString(R.string.URI);

Leave the declaration, but put method invocations inside onCreate() method:

private String uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    uri = getString(R.string.URI);
}
agamov
  • 4,407
  • 1
  • 27
  • 31
  • You are right! It is working! Any idea why for a while it worked, I had `URI` variable declared over there for 4/5 days and the it stopped? – mikey Mar 30 '14 at 11:44
  • 1
    @mikey It shouldn't have worked, no idea :) please accept the answer. – agamov Mar 30 '14 at 11:45
  • oh all right, well thanx anyway I'll accept the answer as soon as I can do it – mikey Mar 30 '14 at 11:46
  • So the error is caused by getString() method, in other words I cannot call it before I created the MainActivity, is it correct? – mikey Mar 30 '14 at 12:22
  • 1
    my guess is that the `Context`, i.e. your `Activity` is not yet initialized because `onCreate()` method is not yet called when you init your String. Check also this question: http://stackoverflow.com/questions/4253328/getstring-outside-of-a-context-or-activity – agamov Mar 30 '14 at 12:39