Advertisement
KishoreZE

Stack Overflow(Question)

Aug 13th, 2015
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.24 KB | None | 0 0
  1.     Can someone please help me figure out the reason behind this nullPointerException.
  2.  
  3. >This is my error log:
  4.  
  5.     8-13 23:27:33.924    4817-4817/? E/Zygote﹕ MountEmulatedStorage()
  6.     08-13 23:27:33.924    4817-4817/? E/Zygote﹕ v2
  7.     08-13 23:27:33.934    4817-4817/? E/SELinux﹕ [DEBUG] get_category: variable
  8.     seinfo: default sensitivity: NULL, cateogry: NULL
  9.     08-13 23:27:34.024    4817-4817/com.kishore_kumar.call E/AndroidRuntime﹕
  10.     FATAL EXCEPTION: main
  11.     Process: com.kishore_kumar.call, PID: 4817
  12.     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
  13.     {com.kishore_kumar.call/com.kishore_kumar.call.MainActivity}:
  14.     java.lang.NullPointerException: Attempt to invoke virtual
  15.     method 'android.view.View android.view.Window.findViewById(int)' on a null
  16.     object reference
  17.             at android.app.ActivityThread.performLaunchActivity
  18.     (ActivityThread.java:2515)
  19.             at android.app.ActivityThread.handleLaunchActivity    
  20.     (ActivityThread.java:2723)
  21.             at android.app.ActivityThread.access$900(ActivityThread.java:172)
  22.             at android.app.ActivityThread$H.handleMessage
  23.     (ActivityThread.java:1422)
  24.             at android.os.Handler.dispatchMessage(Handler.java:102)
  25.             at android.os.Looper.loop(Looper.java:145)
  26.             at android.app.ActivityThread.main(ActivityThread.java:5832)
  27.             at java.lang.reflect.Method.invoke(Native Method)
  28.             at java.lang.reflect.Method.invoke(Method.java:372)
  29.             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
  30.     (ZygoteInit.java:1399)
  31.             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
  32.      Caused by: java.lang.NullPointerException: Attempt to invoke virtual    
  33.      method 'android.view.View android.view.Window.findViewById(int)' on a null
  34.      object reference
  35.             at android.app.Activity.findViewById(Activity.java:2168)
  36.             at com.kishore_kumar.call.MainActivity.<init>(MainActivity.java:21)
  37.             at java.lang.reflect.Constructor.newInstance(Native Method)
  38.             at java.lang.Class.newInstance(Class.java:1650)
  39.             at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
  40.             at android.app.ActivityThread.performLaunchActivity
  41.      (ActivityThread.java:2505)
  42.             at android.app.ActivityThread.handleLaunchActivity  
  43.      (ActivityThread.java:2723)
  44.             at android.app.ActivityThread.access$900(ActivityThread.java:172)
  45.             at android.app.ActivityThread$H.handleMessage
  46.      (ActivityThread.java:1422)
  47.             at android.os.Handler.dispatchMessage(Handler.java:102)
  48.             at android.os.Looper.loop(Looper.java:145)
  49.             at android.app.ActivityThread.main(ActivityThread.java:5832)
  50.             at java.lang.reflect.Method.invoke(Native Method)
  51.             at java.lang.reflect.Method.invoke(Method.java:372)
  52.             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
  53.     (ZygoteInit.java:1399)
  54.             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
  55.  
  56. >----------
  57.  
  58.     My 'MainActivity' File:
  59.  
  60. >------------------------------------
  61.  
  62.     package com.kishore_kumar.call;
  63.  
  64.     import android.app.Activity;
  65.     import android.app.SearchManager;
  66.     import android.content.Context;
  67.     import android.content.Intent;
  68.     import android.os.Bundle;
  69.     import android.view.Menu;
  70.     import android.widget.SearchView;
  71.     import android.widget.*;
  72.  
  73.     /**
  74.     * Simple one-activity app that takes a search term via the Action Bar
  75.     * and uses it as a query to search the contacts database via the Contactables
  76.     * table.
  77.     */
  78.     public class MainActivity extends Activity {
  79.  
  80.     public static final int CONTACT_QUERY_LOADER = 0;
  81.     public static final String QUERY_KEY = "query";
  82.     public EditText cn = (EditText) findViewById(R.id.mainEditText);
  83.     String query = "";
  84.  
  85.     @Override
  86.     protected void onCreate(Bundle savedInstanceState) {
  87.         super.onCreate(savedInstanceState);
  88.         setContentView(R.layout.activity_main);
  89.         startQuery();
  90.     }
  91.  
  92.         private void startQuery()
  93.         {
  94.             query = cn.getText().toString();
  95.             // We need to create a bundle containing the query string to    
  96.     send along to the
  97.             // LoaderManager, which will be handling querying the database
  98.     and returning results.
  99.             if(query!="")
  100.             {
  101.             Bundle bundle = new Bundle();
  102.             bundle.putString(QUERY_KEY, query);
  103.  
  104.             ContactablesLoaderCallbacks loaderCallbacks = new    
  105.     ContactablesLoaderCallbacks(this);
  106.  
  107.             // Start the loader with the new query, and an object that will
  108.     handle all callbacks.
  109.             getLoaderManager().restartLoader(CONTACT_QUERY_LOADER, bundle,
  110.     loaderCallbacks);
  111.         }
  112.         }
  113.     }
  114.  
  115. >--------
  116.    
  117.     And this is my Loader Object Class File:
  118.  
  119. >--------
  120.  
  121.     package com.kishore_kumar.call;
  122.  
  123.     import android.app.Activity;
  124.     import android.app.LoaderManager;
  125.     import android.content.Context;
  126.     import android.content.CursorLoader;
  127.     import android.content.Loader;
  128.     import android.database.Cursor;
  129.     import android.net.Uri;
  130.     import android.os.Bundle;
  131.     import android.provider.ContactsContract.CommonDataKinds;
  132.     import android.util.Log;
  133.     import android.widget.TextView;
  134.  
  135.     /**
  136.     * Helper class to handle all the callbacks that occur when interacting with
  137.     loaders.  Most of the
  138.     * interesting code in this sample app will be in this file.
  139.     */
  140.     public class ContactablesLoaderCallbacks implements
  141.     LoaderManager.LoaderCallbacks<Cursor> {
  142.  
  143.     Context mContext;
  144.  
  145.     public static final String QUERY_KEY = "query";
  146.  
  147.     public static final String TAG = "CLoaderCallbacks";
  148.  
  149.     public ContactablesLoaderCallbacks(Context context) {
  150.         mContext = context;
  151.     }
  152.     @Override
  153.     public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle bundle) {
  154.         // Where the Contactables table excels is matching text queries,
  155.         // not just data dumps from Contacts db.  One search term is used to
  156.     query
  157.         // display name, email address and phone number.  In this case, the
  158.     query was extracted
  159.         // from an incoming intent in the handleIntent() method, via the
  160.         // intent.getStringExtra() method.
  161.  
  162.         // BEGIN_INCLUDE(uri_with_query)
  163.         String query = bundle.getString(QUERY_KEY);
  164.         Uri uri = Uri.withAppendedPath(
  165.             CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);
  166.         // END_INCLUDE(uri_with_query)
  167.  
  168.  
  169.         // BEGIN_INCLUDE(cursor_loader)
  170.         // Easy way to limit the query to contacts with phone numbers.
  171.         String selection =
  172.             CommonDataKinds.Contactables.HAS_PHONE_NUMBER + " = " +
  173.         1;
  174.  
  175.         // Sort results such that rows for the same contact stay together.
  176.         String sortBy = CommonDataKinds.Contactables.LOOKUP_KEY;
  177.  
  178.         return new CursorLoader(
  179.             mContext,  // Context
  180.             uri,       // URI representing the table/resource to be
  181.         queried
  182.             null,      // projection - the list of columns to
  183.         return.  Null means "all"
  184.             selection, // selection - Which rows to return
  185.         (condition rows must match)
  186.             null,      // selection args - can be provided
  187.         separately and subbed into selection.
  188.             sortBy);   // string specifying sort order
  189.         // END_INCLUDE(cursor_loader)
  190.     }
  191.  
  192.     @Override
  193.     public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
  194.         TextView tv  = (TextView) ((Activity)mContext).findViewById
  195.     (R.id.sampleOutput);
  196.         if(tv == null) {
  197.             Log.e(TAG, "TextView is null?!");
  198.         } else if (mContext == null) {
  199.             Log.e(TAG, "Context is null?");
  200.         } else {
  201.             Log.e(TAG, "Nothing is null?!");
  202.         }
  203.  
  204.         // Reset text in case of a previous query
  205.         tv.setText(mContext.getText(R.string.intro) + "\n\n");
  206.  
  207.         if (cursor.getCount() == 0) {
  208.             return;
  209.         }
  210.  
  211.         // Pulling the relevant value from the cursor requires knowing the
  212.            column index to pull
  213.         // it from.
  214.         // BEGIN_INCLUDE(get_columns)
  215.         int phoneColumnIndex = cursor.getColumnIndex
  216.         (CommonDataKinds.Phone.NUMBER);
  217.         int emailColumnIndex = cursor.getColumnIndex
  218.         (CommonDataKinds.Email.ADDRESS);
  219.         int nameColumnIndex = cursor.getColumnIndex
  220.         (CommonDataKinds.Contactables.DISPLAY_NAME);
  221.         int lookupColumnIndex = cursor.getColumnIndex  
  222.         (CommonDataKinds.Contactables.LOOKUP_KEY);
  223.         int typeColumnIndex = cursor.getColumnIndex
  224.         (CommonDataKinds.Contactables.MIMETYPE);
  225.         // END_INCLUDE(get_columns)
  226.  
  227.         cursor.moveToFirst();
  228.         // Lookup key is the easiest way to verify a row of data is for the same
  229.         // contact as the previous row.
  230.         String lookupKey = "";
  231.         do {
  232.             // BEGIN_INCLUDE(lookup_key)
  233.             String currentLookupKey = cursor.getString(lookupColumnIndex);
  234.             if (!lookupKey.equals(currentLookupKey)) {
  235.                 String displayName = cursor.getString(nameColumnIndex);
  236.                 tv.append(displayName + "\n");
  237.                 lookupKey = currentLookupKey;
  238.             }
  239.             // END_INCLUDE(lookup_key)
  240.  
  241.             // BEGIN_INCLUDE(retrieve_data)
  242.             // The data type can be determined using the mime type column.
  243.             String mimeType = cursor.getString(typeColumnIndex);
  244.             if (mimeType.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
  245.                 tv.append("\tPhone Number: " + cursor.getString
  246.                (phoneColumnIndex) + "\n");
  247.             } else
  248.             {
  249.                 tv.setText("selected contact has no phone
  250.           number");
  251.             }
  252.             // END_INCLUDE(retrieve_data)
  253.  
  254.             // Look at DDMS to see all the columns returned by a query to  
  255.                Contactables.
  256.             // Behold, the firehose!
  257.             for(String column : cursor.getColumnNames()) {
  258.                 Log.d(TAG, column + column + ": " +
  259.                       cursor.getString(cursor.getColumnIndex
  260.            (column)) + "\n");
  261.             }
  262.         } while (cursor.moveToNext());
  263.     }
  264.  
  265.     @Override
  266.     public void onLoaderReset(Loader<Cursor> cursorLoader) {
  267.     }
  268.     }
  269.  
  270. >-------
  271.  
  272. This is my main layout file:
  273.  
  274. >------
  275.    
  276.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  277.     xmlns:tools="http://schemas.android.com/tools"
  278.     android:layout_width="match_parent"
  279.     android:layout_height="match_parent"
  280.     android:gravity="top|left"
  281.     android:orientation="vertical" tools:context=".MainActivity">
  282.  
  283.         <TextView
  284.             android:layout_height="wrap_content"
  285.             android:layout_width="match_parent"
  286.             android:text="Please enter contact name:"
  287.             android:textSize="36sp"/>
  288.         <EditText
  289.             android:layout_width="match_parent"
  290.             android:layout_height="wrap_content"
  291.             android:hint="Contact Name"
  292.             android:id="@+id/mainEditText"
  293.             android:textSize="20sp"/>
  294.         <TextView
  295.             android:text="@string/intro"
  296.             android:layout_width="wrap_content"
  297.             android:layout_height="wrap_content"
  298.             android:id="@+id/sampleOutput"
  299.             android:textSize="20sp"
  300.             android:paddingTop="10dp"/>
  301.     </LinearLayout>
  302.  
  303. >------
  304.  
  305.     Also this line in my ContactablesLoaderCallbacks file produces an error:
  306.  
  307. >----
  308.  
  309. >>Code:
  310.     tv.setText(mContext.getText(R.string.intro) + "\n\n");
  311. >>Error:
  312.  
  313.     Method invocation tv.setText(mContext.getText(R.string.intro) + "\n\n")' may
  314.    produce java.lang.NullPointerException' less... (Ctrl+F1) This inspection
  315.     analyzes method control and data flow to report possible conditions that are
  316.     always true or false, expressions whose value is statically proven to be
  317.     constant, and situations that can lead to nullability contract violations.
  318.     Variables, method parameters and return values marked as @Nullable or
  319.     @NotNull are treated as nullable (or not-null, respectively) and used during
  320.     the analysis to check nullability contracts, e.g. report possible munpoin
  321.     terException errors.
  322.     More complex contracts can be defined using @contract annotation, for    
  323.     example:
  324.     @Contract("_, null -> null") — method returns null if its second argument is
  325.     null @Contract("_, null -> null; _, !null -> !null") — method returns null
  326.     if its second argument is null and not-null otherwise @Contract("true ->
  327.    fail") —atypical  assertFalse method which throws an exception if true is
  328.     passed to it
  329.     The inspection can be configured to use custom @Nullable @NotNull
  330.     annotations (by default the ones from annotations.jar will be used)
  331. >
  332. >Please forgive me if there are an typing errors in the error part:
  333. >Or here is a pic:
  334. [![Warning][1]][1]
  335.  
  336.  
  337.   [1]: http://i.stack.imgur.com/WuC4Z.png
  338.  
  339. >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