Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Can someone please help me figure out the reason behind this nullPointerException.
- >This is my error log:
- 8-13 23:27:33.924 4817-4817/? E/Zygote﹕ MountEmulatedStorage()
- 08-13 23:27:33.924 4817-4817/? E/Zygote﹕ v2
- 08-13 23:27:33.934 4817-4817/? E/SELinux﹕ [DEBUG] get_category: variable
- seinfo: default sensitivity: NULL, cateogry: NULL
- 08-13 23:27:34.024 4817-4817/com.kishore_kumar.call E/AndroidRuntime﹕
- FATAL EXCEPTION: main
- Process: com.kishore_kumar.call, PID: 4817
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
- {com.kishore_kumar.call/com.kishore_kumar.call.MainActivity}:
- java.lang.NullPointerException: Attempt to invoke virtual
- method 'android.view.View android.view.Window.findViewById(int)' on a null
- object reference
- at android.app.ActivityThread.performLaunchActivity
- (ActivityThread.java:2515)
- at android.app.ActivityThread.handleLaunchActivity
- (ActivityThread.java:2723)
- at android.app.ActivityThread.access$900(ActivityThread.java:172)
- at android.app.ActivityThread$H.handleMessage
- (ActivityThread.java:1422)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:145)
- at android.app.ActivityThread.main(ActivityThread.java:5832)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
- (ZygoteInit.java:1399)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual
- method 'android.view.View android.view.Window.findViewById(int)' on a null
- object reference
- at android.app.Activity.findViewById(Activity.java:2168)
- at com.kishore_kumar.call.MainActivity.<init>(MainActivity.java:21)
- at java.lang.reflect.Constructor.newInstance(Native Method)
- at java.lang.Class.newInstance(Class.java:1650)
- at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
- at android.app.ActivityThread.performLaunchActivity
- (ActivityThread.java:2505)
- at android.app.ActivityThread.handleLaunchActivity
- (ActivityThread.java:2723)
- at android.app.ActivityThread.access$900(ActivityThread.java:172)
- at android.app.ActivityThread$H.handleMessage
- (ActivityThread.java:1422)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:145)
- at android.app.ActivityThread.main(ActivityThread.java:5832)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
- (ZygoteInit.java:1399)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
- >----------
- My 'MainActivity' File:
- >------------------------------------
- package com.kishore_kumar.call;
- import android.app.Activity;
- import android.app.SearchManager;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.widget.SearchView;
- import android.widget.*;
- /**
- * Simple one-activity app that takes a search term via the Action Bar
- * and uses it as a query to search the contacts database via the Contactables
- * table.
- */
- public class MainActivity extends Activity {
- public static final int CONTACT_QUERY_LOADER = 0;
- public static final String QUERY_KEY = "query";
- public EditText cn = (EditText) findViewById(R.id.mainEditText);
- String query = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- startQuery();
- }
- private void startQuery()
- {
- query = cn.getText().toString();
- // We need to create a bundle containing the query string to
- send along to the
- // LoaderManager, which will be handling querying the database
- and returning results.
- if(query!="")
- {
- Bundle bundle = new Bundle();
- bundle.putString(QUERY_KEY, query);
- ContactablesLoaderCallbacks loaderCallbacks = new
- ContactablesLoaderCallbacks(this);
- // Start the loader with the new query, and an object that will
- handle all callbacks.
- getLoaderManager().restartLoader(CONTACT_QUERY_LOADER, bundle,
- loaderCallbacks);
- }
- }
- }
- >--------
- And this is my Loader Object Class File:
- >--------
- package com.kishore_kumar.call;
- import android.app.Activity;
- import android.app.LoaderManager;
- import android.content.Context;
- import android.content.CursorLoader;
- import android.content.Loader;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.ContactsContract.CommonDataKinds;
- import android.util.Log;
- import android.widget.TextView;
- /**
- * Helper class to handle all the callbacks that occur when interacting with
- loaders. Most of the
- * interesting code in this sample app will be in this file.
- */
- public class ContactablesLoaderCallbacks implements
- LoaderManager.LoaderCallbacks<Cursor> {
- Context mContext;
- public static final String QUERY_KEY = "query";
- public static final String TAG = "CLoaderCallbacks";
- public ContactablesLoaderCallbacks(Context context) {
- mContext = context;
- }
- @Override
- public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle bundle) {
- // Where the Contactables table excels is matching text queries,
- // not just data dumps from Contacts db. One search term is used to
- query
- // display name, email address and phone number. In this case, the
- query was extracted
- // from an incoming intent in the handleIntent() method, via the
- // intent.getStringExtra() method.
- // BEGIN_INCLUDE(uri_with_query)
- String query = bundle.getString(QUERY_KEY);
- Uri uri = Uri.withAppendedPath(
- CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);
- // END_INCLUDE(uri_with_query)
- // BEGIN_INCLUDE(cursor_loader)
- // Easy way to limit the query to contacts with phone numbers.
- String selection =
- CommonDataKinds.Contactables.HAS_PHONE_NUMBER + " = " +
- 1;
- // Sort results such that rows for the same contact stay together.
- String sortBy = CommonDataKinds.Contactables.LOOKUP_KEY;
- return new CursorLoader(
- mContext, // Context
- uri, // URI representing the table/resource to be
- queried
- null, // projection - the list of columns to
- return. Null means "all"
- selection, // selection - Which rows to return
- (condition rows must match)
- null, // selection args - can be provided
- separately and subbed into selection.
- sortBy); // string specifying sort order
- // END_INCLUDE(cursor_loader)
- }
- @Override
- public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
- TextView tv = (TextView) ((Activity)mContext).findViewById
- (R.id.sampleOutput);
- if(tv == null) {
- Log.e(TAG, "TextView is null?!");
- } else if (mContext == null) {
- Log.e(TAG, "Context is null?");
- } else {
- Log.e(TAG, "Nothing is null?!");
- }
- // Reset text in case of a previous query
- tv.setText(mContext.getText(R.string.intro) + "\n\n");
- if (cursor.getCount() == 0) {
- return;
- }
- // Pulling the relevant value from the cursor requires knowing the
- column index to pull
- // it from.
- // BEGIN_INCLUDE(get_columns)
- int phoneColumnIndex = cursor.getColumnIndex
- (CommonDataKinds.Phone.NUMBER);
- int emailColumnIndex = cursor.getColumnIndex
- (CommonDataKinds.Email.ADDRESS);
- int nameColumnIndex = cursor.getColumnIndex
- (CommonDataKinds.Contactables.DISPLAY_NAME);
- int lookupColumnIndex = cursor.getColumnIndex
- (CommonDataKinds.Contactables.LOOKUP_KEY);
- int typeColumnIndex = cursor.getColumnIndex
- (CommonDataKinds.Contactables.MIMETYPE);
- // END_INCLUDE(get_columns)
- cursor.moveToFirst();
- // Lookup key is the easiest way to verify a row of data is for the same
- // contact as the previous row.
- String lookupKey = "";
- do {
- // BEGIN_INCLUDE(lookup_key)
- String currentLookupKey = cursor.getString(lookupColumnIndex);
- if (!lookupKey.equals(currentLookupKey)) {
- String displayName = cursor.getString(nameColumnIndex);
- tv.append(displayName + "\n");
- lookupKey = currentLookupKey;
- }
- // END_INCLUDE(lookup_key)
- // BEGIN_INCLUDE(retrieve_data)
- // The data type can be determined using the mime type column.
- String mimeType = cursor.getString(typeColumnIndex);
- if (mimeType.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
- tv.append("\tPhone Number: " + cursor.getString
- (phoneColumnIndex) + "\n");
- } else
- {
- tv.setText("selected contact has no phone
- number");
- }
- // END_INCLUDE(retrieve_data)
- // Look at DDMS to see all the columns returned by a query to
- Contactables.
- // Behold, the firehose!
- for(String column : cursor.getColumnNames()) {
- Log.d(TAG, column + column + ": " +
- cursor.getString(cursor.getColumnIndex
- (column)) + "\n");
- }
- } while (cursor.moveToNext());
- }
- @Override
- public void onLoaderReset(Loader<Cursor> cursorLoader) {
- }
- }
- >-------
- This is my main layout file:
- >------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="top|left"
- android:orientation="vertical" tools:context=".MainActivity">
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:text="Please enter contact name:"
- android:textSize="36sp"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Contact Name"
- android:id="@+id/mainEditText"
- android:textSize="20sp"/>
- <TextView
- android:text="@string/intro"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/sampleOutput"
- android:textSize="20sp"
- android:paddingTop="10dp"/>
- </LinearLayout>
- >------
- Also this line in my ContactablesLoaderCallbacks file produces an error:
- >----
- >>Code:
- tv.setText(mContext.getText(R.string.intro) + "\n\n");
- >>Error:
- Method invocation tv.setText(mContext.getText(R.string.intro) + "\n\n")' may
- produce java.lang.NullPointerException' less... (Ctrl+F1) This inspection
- analyzes method control and data flow to report possible conditions that are
- always true or false, expressions whose value is statically proven to be
- constant, and situations that can lead to nullability contract violations.
- Variables, method parameters and return values marked as @Nullable or
- @NotNull are treated as nullable (or not-null, respectively) and used during
- the analysis to check nullability contracts, e.g. report possible munpoin
- terException errors.
- More complex contracts can be defined using @contract annotation, for
- example:
- @Contract("_, null -> null") — method returns null if its second argument is
- null @Contract("_, null -> null; _, !null -> !null") — method returns null
- if its second argument is null and not-null otherwise @Contract("true ->
- fail") —atypical assertFalse method which throws an exception if true is
- passed to it
- The inspection can be configured to use custom @Nullable @NotNull
- annotations (by default the ones from annotations.jar will be used)
- >
- >Please forgive me if there are an typing errors in the error part:
- >Or here is a pic:
- [![Warning][1]][1]
- [1]: http://i.stack.imgur.com/WuC4Z.png
- >If you want to know - this is a modified app of the app samples from the >android developers website. I know about the license this is just me testing >the app. Please do help. I Hope I Have Provided All Necessary Resource Files. >If not pls. let me know and I will comply.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement