0

I am new to making Android Applications. I was working on an Android App(in Eclipse) that uses Bluetooth APIs and the problem occurs when I select a particular device name from the list of paired devices(i.e. jumping from one activity to the next), the app stops and it displays this message instead of showing the next screen. I am not able to understand what the error could be. I am sharing the code snippet from the Main Activity:

// listening to single list item on click
           listOfDevices.setOnItemClickListener(new OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                  // Launching new Activity on selecting single List Item
                 Intent i = new Intent(getApplicationContext(), Controls.class);
                 startActivity(i);

              }
            });

This is the code for the 2nd activity- Controls.java (contains only buttons as of now)

public class Controls extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this.setContentView(R.layout.controls);

   final Button bt1 = (Button) findViewById(R.id.button1);
    final Button bt2 = (Button) findViewById(R.id.button2);
    final Button bt3 = (Button) findViewById(R.id.button3);
    final Button bt4 = (Button) findViewById(R.id.button4);
bt1.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
bt2.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
bt3.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
bt4.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
    Intent i = getIntent(); 



    }
}

And this is the Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="team2.cse8.project"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              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=".Controls"
                android:label="@string/app_name">
    </activity>

</application>
</manifest>
user3024292
  • 97
  • 2
  • 12
  • 2
    Why did you comment `//this.setContentView(R.layout.controls);` in your second activity? If you are running the code you posted, `findViewById` will return `null` and hence your app will crash when trying to set the the color filter for the background of the button1. Also always add the stacktrace with your question. – Alexis C. Apr 25 '14 at 12:40
  • 1
    yes uncomment that line @ZouZou has pointed out – mungaih pk Apr 25 '14 at 12:42
  • @ZouZou Oh yes! I had earlier commented it out because R.java file was not getting generated and later on I forgot to uncomment it.Thanks!! Sorry but what is stacktrace and how do I add it? – user3024292 Apr 25 '14 at 12:49
  • @user3024292 See http://stackoverflow.com/questions/3280051/how-to-enable-logcat-console-in-eclipse-for-android – Alexis C. Apr 25 '14 at 12:52

1 Answers1

3

I assume you are getting NullPointerException because you have commented following line.

//this.setContentView(R.layout.controls);

remove the comment from this line. That file holds your view components.

Lucifer
  • 29,392
  • 25
  • 90
  • 143