-3

I have linked a number of activities, to build an application, but the initial one, now when I start, gives an error, while working with. Though the activity was working initially, but now, it is showing that the application has closed forcefully. can anyone help me out with this? Also, how to attach the logcat, please tell me. I wish seeing that might help you all, solve the problem better. Thanks!

package com.example.newapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

    int counter;
    Button add, sub, change, mail, camera;
    TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

       add = (Button) findViewById(R.id.add);
       sub = (Button) findViewById(R.id.sub);
       display=(TextView) findViewById(R.id.textView1);
       change = (Button) findViewById(R.id.change);
       mail = (Button) findViewById(R.id.mail);
       camera = (Button) findViewById(R.id.camera);

       add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter = counter + 1;
            display.setText("Answer is "+ counter);

        }
    });

       sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter = counter - 1;
            display.setText("Answer is "+ counter);

        }
    });

      change.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent startTextPlay = new Intent("com.example.newapplication.TEXTPLAY");
            startActivity(startTextPlay);
        }
    });

      mail.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent startMailer = new Intent("com.example.newapplication.EMAIL");
            startActivity(startMailer);

        }
    });

      camera.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent startCamera = new Intent("com.example.newapplication.CAMERA");
            startActivity(startCamera);

        }
    });

    }
}

This is the code in the main activity, and the code for the activity not starting, the change one is again given below.

package com.example.newapplication;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity{

    Button chkCommand = (Button) findViewById(R.id.b1);
    ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
    EditText input = (EditText) findViewById(R.id.etCommands);
    TextView display = (TextView) findViewById(R.id.tv);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);

        passTog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (passTog.isChecked()){
                    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else{
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                }
            }
        });

        chkCommand.setOnClickListener(new View.OnClickListener() {          
            @Override
            public void onClick(View arg0) {
                String message = input.getText().toString();
                if(message.contentEquals("naman")){
                    display.setTextColor(Color.GREEN);
                    display.setGravity(Gravity.CENTER);
                    display.setText("Password is correct");
                    Random crazy = new Random();
                    display.setTextSize(crazy.nextInt(75));
                    display.setTextColor(Color.rgb(crazy.nextInt(200), crazy.nextInt(256), crazy.nextInt(256)));
                }
                else{
                    display.setTextColor(Color.RED);
                    display.setGravity(Gravity.CENTER);
                    display.setText("Invalid Password");
                }
            }
        });
    }



}

The logcat is as given below:

05-29 01:52:40.280: E/AndroidRuntime(892): FATAL EXCEPTION: main
05-29 01:52:40.280: E/AndroidRuntime(892): Process: com.example.newapplication, PID: 892
05-29 01:52:40.280: E/AndroidRuntime(892): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.newapplication/com.example.newapplication.TextPlay}: java.lang.NullPointerException
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.os.Looper.loop(Looper.java:136)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-29 01:52:40.280: E/AndroidRuntime(892):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 01:52:40.280: E/AndroidRuntime(892):  at java.lang.reflect.Method.invoke(Method.java:515)
05-29 01:52:40.280: E/AndroidRuntime(892):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-29 01:52:40.280: E/AndroidRuntime(892):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-29 01:52:40.280: E/AndroidRuntime(892):  at dalvik.system.NativeStart.main(Native Method)
05-29 01:52:40.280: E/AndroidRuntime(892): Caused by: java.lang.NullPointerException
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.Activity.findViewById(Activity.java:1884)
05-29 01:52:40.280: E/AndroidRuntime(892):  at com.example.newapplication.TextPlay.<init>(TextPlay.java:18)
05-29 01:52:40.280: E/AndroidRuntime(892):  at java.lang.Class.newInstanceImpl(Native Method)
05-29 01:52:40.280: E/AndroidRuntime(892):  at java.lang.Class.newInstance(Class.java:1208)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-29 01:52:40.280: E/AndroidRuntime(892):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-29 01:52:40.280: E/AndroidRuntime(892):  ... 11 more

The Android Manifest File is as follows:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <permission android:name="android.permission.SET_WALLPAPER"></permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.newapplication.Splash"
            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="com.example.newapplication.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.newapplication.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.newapplication.TextPlay"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="com.example.newapplication.TEXTPLAY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.newapplication.Email"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.newapplication.EMAIL" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.newapplication.Camera"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.newapplication.CAMERA" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


    </application>

</manifest>
BumbleBee
  • 103
  • 1
  • 3
  • 11

2 Answers2

3

Move this code of TextPlay.java

Button chkCommand = (Button) findViewById(R.id.b1);
ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.tv);

after

setContentView(R.layout.text);

You are getting Null Pointer Exception as you try to find the views before setting content of xml file. Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Fine. It worked. Thanks. I was supposed to add a new method to the program, to make the program look a bit clean, but I didn't and, supposedly it was giving the error for the null point exception. – BumbleBee May 29 '14 at 06:12
  • @user3676294 Glad that I could help :) Always welcome. – MysticMagicϡ May 29 '14 at 06:13
  • Can you like help with the use of final and this in JAVA? I am pretty new to programming, and the above code, when used, shows error for declaring the data type of all the variables as final. When done so, the program runs fine. So what does this final do? – BumbleBee May 29 '14 at 06:16
  • If you can brief up a bit, or suggest some study material link online. – BumbleBee May 29 '14 at 06:18
  • Sure.. @user3676294 refer [final modifier](http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_3.html) and [a descriptive answer for this](http://stackoverflow.com/a/3728188/1777090) – MysticMagicϡ May 29 '14 at 06:23
0

This code :

Button chkCommand = (Button) findViewById(R.id.b1);
ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.tv);

It should be Inside the OnCreate method and below setContentView(R.layout.text); this line In class you can declare the Variable Initialization is done inside the OnCreate Method.

public class TextPlay extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);

        Button chkCommand = (Button) findViewById(R.id.b1);
        ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
        EditText input = (EditText) findViewById(R.id.etCommands);
        TextView display = (TextView) findViewById(R.id.tv);


        passTog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (passTog.isChecked()){
                    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else{
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                }
            }
        });

        chkCommand.setOnClickListener(new View.OnClickListener() {          
            @Override
            public void onClick(View arg0) {
                String message = input.getText().toString();
                if(message.contentEquals("naman")){
                    display.setTextColor(Color.GREEN);
                    display.setGravity(Gravity.CENTER);
                    display.setText("Password is correct");
                    Random crazy = new Random();
                    display.setTextSize(crazy.nextInt(75));
                    display.setTextColor(Color.rgb(crazy.nextInt(200), crazy.nextInt(256), crazy.nextInt(256)));
                }
                else{
                    display.setTextColor(Color.RED);
                    display.setGravity(Gravity.CENTER);
                    display.setText("Invalid Password");
                }
            }
        });
    }



}
W I Z A R D
  • 1,224
  • 3
  • 17
  • 44