Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't seem to solve this NullPointerException
    primarykey
    data
    text
    <p>The error I'm getting is: </p> <pre><code>java.lang.NullPointerException at com.me.app.MainActivity.onCreate(MainActivity.java:34) </code></pre> <p>Lines 34-39 in <code>MainActivity.java</code> are as follows:</p> <pre><code>light_switch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { turn_torch_on(); } }); </code></pre> <p>This leads me to believe that <code>light_switch</code> is returning <code>null</code>, despite this on line 31:</p> <pre><code>light_switch = (ImageButton)findViewById(R.id.light_switch); </code></pre> <p>I've double checked the id is correct. Is there anything that I'm missing? I'm not really sure what I can do to fix this at this moment.</p> <p>The exception log is available here: <a href="http://pastebin.com/raw.php?i=yDPRjvry" rel="nofollow">http://pastebin.com/raw.php?i=yDPRjvry</a></p> <h2>MainActivity.java</h2> <pre><code>package com.me.app; import android.app.Activity; import android.app.AlertDialog; import android.app.Fragment; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; public class MainActivity extends Activity { ImageButton light_switch; private boolean has_flash; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } // get the light switch light_switch = (ImageButton)findViewById(R.id.light_switch); // make the light switch work light_switch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { turn_torch_on(); } }); // check if device has flash capability has_flash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); // alert user that their device is not supported if ( !has_flash ) { AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create(); alert.setTitle("Sorry :("); alert.setMessage("Your device does not have a flash"); alert.show(); return; } } /** * turn torch on */ private void turn_torch_on() { // change the layout to off toggle_layout(); } /** * toggle layout for on/off */ private void toggle_layout() { light_switch.setBackgroundResource(R.drawable.button_on); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } } </code></pre> <h2>fragment_main.xml</h2> <pre><code>&lt;RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:background="@drawable/bg_off" tools:context=".MainActivity$PlaceholderFragment"&gt; &lt;ImageButton android:id="@+id/light_switch" android:layout_height="100dp" android:layout_width="fill_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/button_off" android:contentDescription="On/Off switch" /&gt; &lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload