Saiyan Prince
Saiyan Prince

Reputation: 4020

Why can't I start the new activity on click of a button?

I am new to android. I am facing the problem of starting new activity on click of a button. I am trying to register the users to in order use the features of android application.

Following is the error that I get:

    DalvikVM [localhost:8600]   
        Thread [<1> main] (Suspended (exception RuntimeException))  
            <VM does not provide monitor information>   
            ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2351    
            ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2403 
            ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 165    
            ActivityThread$H.handleMessage(Message) line: 1373  
            ActivityThread$H(Handler).dispatchMessage(Message) line: 107    
            Looper.loop() line: 194 
            ActivityThread.main(String[]) line: 5391    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 525  
            ZygoteInit$MethodAndArgsCaller.run() line: 833  
            ZygoteInit.main(String[]) line: 600 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<10> Binder_2] (Running)    
        Thread [<9> Binder_1] (Running) 
        Thread [<11> Binder_3] (Running)    

Here's the updated source code of java:

public class InvestorsLogin extends Activity{

EditText PASSWORD, EMAILID;
String extracted_email, extracted_password, recieved_password,final_request;
ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.investor_login);

    ActionBar aBar = getActionBar();
    aBar.setDisplayHomeAsUpEnabled(true);

    Button btnInvLogClear = (Button) findViewById(R.id.btnInvClear);
    Button btnInvLogin = (Button) findViewById(R.id.btnInvLogin);
    Button btnInvRegister = (Button) findViewById(R.id.btnInvRegister);

    EMAILID = (EditText) findViewById(R.id.txtInvEmailAddress);
    PASSWORD = (EditText) findViewById(R.id.txtInvPassword);

    btnInvLogClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (EMAILID.length() >= 1) {
                EMAILID.setText("");
            }
            if (PASSWORD.length() >= 1) {
                PASSWORD.setText("");
            }
        }
    });

    btnInvRegister.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(InvestorsLogin.this, InvestorsRegister.class);
            startActivity(i);
        }
    });

    btnInvLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            extracted_email = EMAILID.getText().toString();
            extracted_password = PASSWORD.getText().toString();

            if(EMAILID.length() < 1 || PASSWORD.length() < 1) {
                Toast.makeText(getApplicationContext(), "Please Fill In All The Details", Toast.LENGTH_LONG).show();
            } else {
                new LoginInvestorTask().execute(extracted_email);
            }
        }
    });
}

class LoginInvestorTask extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute(){
        pDialog = new ProgressDialog(InvestorsLogin.this);
        pDialog.setTitle("Processing...");
        pDialog.setMessage("Checking Your Credentials...");
        pDialog.setCancelable(true);
        pDialog.setIndeterminate(true);
        pDialog.show();
    }

    @SuppressWarnings("deprecation")
    @Override
    protected String doInBackground(String... params) {
        final_request="http://mehul.wink.ws/selectEntrepreneurToLogin.php?emailid="+URLEncoder.encode(extracted_email);
        GetPasswordFromServer test=new GetPasswordFromServer();         
        try {
            return test.getServerData(final_request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressLint("ShowToast")
    @Override
    protected void onPostExecute(String result) {
        try {
            recieved_password=result;
            if(pDialog != null) {
                pDialog.dismiss();
            }

            if(recieved_password.equals(extracted_password)){
                Toast.makeText(getApplicationContext(), "Success !", Toast.LENGTH_LONG).show();
                /*Intent welcomeInvestor = new Intent(getApplicationContext(), WelcomeInvestor.class);
                welcomeInvestor.putExtra("email", extracted_email);
                startActivity(welcomeInvestor);*/
            }
        } catch(Exception e) {
            Toast.makeText(getApplicationContext(), "Invalid Credentials", Toast.LENGTH_LONG).show();
        }
    }
}

}

And here's the manifest file

<application
        android:allowBackup="true"
        android:icon="@drawable/entreprenuerexpress"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.entrepreneurexpress.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- All Investors Activities Starts Here -->
            <activity
                android:name="com.example.entrepreneurexpress.investors.InvestorsLogin"
                android:label="@string/btnInvestors"
                android:parentActivityName="com.example.entrepreneurexpress.MainActivity" >
            </activity>
            <activity
                android:name="com.example.entrepreneurexpress.investors.InvestorsRegister"
                android:label="@string/register"
                android:parentActivityName="com.example.entrepreneurexpress.investors.InvestorsLogin" >
            </activity>
            <activity
                android:name="com.example.entrepreneurexpress.investors.WelcomeInvestor"
                android:label="@string/welcomeInvestor">
            </activity>

Here's the logcat:

04-19 18:46:26.760: E/AndroidRuntime(16605): FATAL EXCEPTION: main
04-19 18:46:26.760: E/AndroidRuntime(16605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.entrepreneurexpress/com.example.entrepreneurexpress.investors.InvestorsRegister}: java.lang.NullPointerException
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.ActivityThread.access$600(ActivityThread.java:165)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.os.Handler.dispatchMessage(Handler.java:107)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.os.Looper.loop(Looper.java:194)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.ActivityThread.main(ActivityThread.java:5391)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at java.lang.reflect.Method.invoke(Method.java:525)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at dalvik.system.NativeStart.main(Native Method)
04-19 18:46:26.760: E/AndroidRuntime(16605): Caused by: java.lang.NullPointerException
04-19 18:46:26.760: E/AndroidRuntime(16605):    at com.example.entrepreneurexpress.investors.InvestorsRegister.onCreate(InvestorsRegister.java:47)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.Activity.performCreate(Activity.java:5122)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
04-19 18:46:26.760: E/AndroidRuntime(16605):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
04-19 18:46:26.760: E/AndroidRuntime(16605):    ... 11 more

Here's the InvestorRegister.java file

public class InvestorsRegister extends Activity {

    String extracted_email, extracted_password, extracted_fullName;
    EditText YourFirstName, YourLastName, email, password;
    ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.investor_register);

        ActionBar aBar = getActionBar();
        aBar.setDisplayHomeAsUpEnabled(true);

        Button btnInvClear = (Button) findViewById(R.id.btnInvClear);
        Button btnInvRegister = (Button) findViewById(R.id.btnInvRegister);

        YourFirstName = (EditText) findViewById(R.id.txtInvRegFullName);
        email = (EditText) findViewById(R.id.txtInvRegEmailAddress);
        password = (EditText) findViewById(R.id.txtInvRegPassword);

        btnInvClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (YourFirstName.length() >= 1) {
                    YourFirstName.setText("");
                }
                if (email.length() >= 1) {
                    email.setText("");
                }
                if (password.length() >= 1) {
                    password.setText("");
                }
            }           
        });

        btnInvRegister.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(YourFirstName.length() < 1 || email.length() < 1 || password.length() < 1) {
                    Toast.makeText(getApplicationContext(), "Please Fill In All The Details", Toast.LENGTH_LONG).show();
                } else {
                    new RegisterInvestorsTask().execute("registerExpert");
                }
            }
        });
    }

    class RegisterInvestorsTask extends AsyncTask<String,Void,String>{

        protected void onPreExecute(){
            pDialog = new ProgressDialog(InvestorsRegister.this);
            pDialog.setTitle("Processing...");
            pDialog.setMessage("Registering Yourself With Us...");
            pDialog.setCancelable(true);
            pDialog.setIndeterminate(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params){
            extracted_email = email.getText().toString();
            extracted_fullName = YourFirstName.getText().toString();

            @SuppressWarnings("deprecation")
            String requesturl = "http://mehul.wink.ws/insertInvestor.php?nm=" + URLEncoder.encode(extracted_fullName) + 
                    "&email=" + URLEncoder.encode(extracted_email) + "&pass=" + URLEncoder.encode(extracted_password);

            try {
                HttpClient client = new DefaultHttpClient();
                URI website;
                website = new URI(requesturl);
                HttpGet request = new HttpGet();
                request.setURI(website);
                client.execute(request);
            } catch (IOException e) {
                e.printStackTrace();
            } catch(URISyntaxException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            if(pDialog != null) {
                pDialog.dismiss();
            }
            try {
                Toast.makeText(getApplicationContext(), "Sucessfully Inserted !", Toast.LENGTH_LONG).show();
                Intent i = new Intent(getApplicationContext(), WelcomeInvestor.class);
                startActivity(i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Here's the investor_register.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="19dp"
        android:text="@string/headerforInvestorLogin" />

    <EditText
        android:id="@+id/txtInvRegFullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="28dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="@string/fullName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/txtInvRegEmailAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtInvRegFullName"
        android:layout_alignRight="@+id/txtInvRegFullName"
        android:layout_below="@+id/txtInvRegFullName"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="@string/emailAdd"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/txtInvRegPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtInvRegEmailAddress"
        android:layout_alignRight="@+id/txtInvRegEmailAddress"
        android:layout_below="@+id/txtInvRegEmailAddress"
        android:layout_marginTop="19dp"
        android:ems="10"
        android:hint="@string/password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnInvRegClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtInvRegPassword"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/txtInvRegPassword"
        android:layout_marginRight="80dp"
        android:layout_marginTop="16dp"
        android:text="@string/btnClear" />

    <Button
        android:id="@+id/btnInvRegRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnInvRegClear"
        android:layout_alignBottom="@+id/btnInvRegClear"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/txtInvRegPassword"
        android:layout_marginLeft="140dp"
        android:text="@string/btnRegister" />

</RelativeLayout>

Please help me in solving my query. Thanks.

Upvotes: 1

Views: 88

Answers (1)

M D
M D

Reputation: 47807

Try this

    Button btnInvClear = (Button) findViewById(R.id.btnInvRegClear);
    Button btnInvRegister = (Button) findViewById(R.id.btnInvRegRegister);

instead of

    Button btnInvClear = (Button) findViewById(R.id.btnInvClear);
    Button btnInvRegister = (Button) findViewById(R.id.btnInvRegister);

Your app got crash it's because your investor_register.xml contains buttons with id btnInvRegClear and btnInvRegRegister and you reference wrong ids in your InvestorsRegister Activity.

Upvotes: 1

Related Questions