0

I have my login page where i have my login So it means i have a button for the Login and an clickable text for the redirection to the register page. My button works fine but when I'm clicking my clickable text Unable to start activity ComponentInfo error shows.

MainActivity.class

package com.example.kun.carkila;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity  {
final String LOG = "MainActivity";
Button btnLogin;
EditText etUsername, etPassword;
TextView tvRegister;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    tvRegister = (TextView) findViewById(R.id.tvRegister);
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            HashMap postData = new HashMap();
            String username = etUsername.getText().toString();
            String password = etPassword.getText().toString();
            postData.put("username", username);
            postData.put("password", password);

            PostResponseAsyncTask task1 = new PostResponseAsyncTask(MainActivity.this, postData,
                    new AsyncResponse() {
                        @Override
                        public void processFinish(String s) {

                            if (s.contains("renterowner")) {
                                Toast.makeText(MainActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show();
                                Intent in = new Intent(MainActivity.this, ListActivity.class);
                                startActivity(in);

                            } else if (s.contains("ownerrenter")) {
                                Toast.makeText(MainActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
                                Intent in = new Intent(MainActivity.this, ownerhome.class);
                                startActivity(in);
                            } else {
                                Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
                            }


                        }
                    });
            task1.execute("http://carkila.esy.es/authenticate.php");
        }
    });

    tvRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Intent in = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(in);
        }
    });
}
}

My Register class is still empty though but still I cant manage to go there by clicking my textview. Thanks for the help. :)

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.MainActivity"
tools:showIn="@layout/activity_main">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/etUsername"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="Username" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:id="@+id/etPassword"
    android:layout_below="@+id/etUsername"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="Password" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    android:id="@+id/btnLogin"
    android:layout_below="@+id/etPassword"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Register"
    android:id="@+id/tvRegister"
    android:layout_below="@+id/btnLogin"
    android:layout_centerHorizontal="true"
    />
</RelativeLayout>

Manifest ##

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.kun.carkila">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="@android:icon">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ListActivity"
        android:label="@string/title_activity_list"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".DetailActivity"
        android:label="@string/title_activity_detail"
        android:parentActivityName=".ListActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.kun.carkila.ListActivity" />
    </activity>
    <activity
        android:name=".InsertActivity"
        android:label="@string/title_activity_insert"
        android:parentActivityName=".ListActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.kun.carkila.ListActivity" />
    </activity>
    <activity
        android:name=".ownerhome"
        android:label="@string/title_activity_ownerhome"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".RegisterActivity"
        android:label="@string/title_activity_register"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.kun.carkila.MainActivity" />
    </activity>
</application>

</manifest>

Error

07-18 00:18:18.282 24312-24312/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.example.kun.carkila, PID: 24312
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kun.carkila/com.example.kun.carkila.RegisterActivity}: java.lang.NullPointerException
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:136)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                   at dalvik.system.NativeStart.main(Native Method)
                                                Caused by: java.lang.NullPointerException
                                                   at com.example.kun.carkila.RegisterActivity.onCreate(RegisterActivity.java:20)
                                                   at android.app.Activity.performCreate(Activity.java:5231)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:136) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                   at dalvik.system.NativeStart.main(Native Method) 

RegisterActivity.class

package com.example.kun.carkila;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class RegisterActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

}
Batz
  • 69
  • 10
  • Your Register class is still empty? What does it means? No layout associated? – Lino Jul 17 '16 at 16:06
  • ok good, could you please post the stacktrace? – Lino Jul 17 '16 at 16:08
  • It is simply the android log of the error. It look like this http://stackoverflow.com/a/8217566/2442831 – Lino Jul 17 '16 at 16:12
  • You are reading the logcat incorrectly... Look for the words "Caused by" – OneCricketeer Jul 17 '16 at 16:14
  • @Lino - O there, i edited it and I post my error which the "Caused by" is there. – Batz Jul 17 '16 at 16:20
  • Thanks for the edit, now go fix the NullPointerException in the code that you haven't posted here.... `Caused by: java.lang.NullPointerException at com.example.kun.carkila.RegisterActivity.onCreate(RegisterActivity.java:20)` – OneCricketeer Jul 17 '16 at 16:21
  • Possibly relevant. http://stackoverflow.com/questions/19078461/android-null-pointer-exception-findviewbyid – OneCricketeer Jul 17 '16 at 16:22
  • @cricket_007 - How can i fix the NullPointer in my case? I really have no idea. :( – Batz Jul 17 '16 at 16:27
  • It appears that the Register activity xml does not contain a Floating action button... I think I counted those lines correctly. FindViewById returns null. That's the problem – OneCricketeer Jul 17 '16 at 16:29
  • paste the xml for your resgister activity, the issue seems to in your register class xml, maybe you have deleted the fab button in your xml – SaravInfern Jul 17 '16 at 16:29
  • @cricket_007 - That does the trick! Hahaha! I deleted the Floating Action Button. And now its okay. Thank you sir! :) – Batz Jul 17 '16 at 16:30
  • @SaravInfern - Its okay na I deleted my Floating Action Button on my .xml file and now I deleted it on my register.class. Still thanks for the concern. :) – Batz Jul 17 '16 at 16:31
  • 1
    @J.Deep can close this question – SaravInfern Jul 17 '16 at 16:34
  • Welcome. Advice: Unless you know how to edit the generated Activity files from File > New, always write activities manually from scratch. Then you get less boilerplate code – OneCricketeer Jul 17 '16 at 16:39

2 Answers2

0

Make sure you have declared the RegisterActivity in the AndroidManifest.xml file. Like this <activity android:name="com.example.kun.carkila.RegisterActivity " />

  • `android:name=".RegisterActivity"` is perfectly fine and in the question. Though the question was edited, if you don't have enough reputation to comment for more information, then it's best to move along to a question you can answer – OneCricketeer Jul 17 '16 at 16:16
0

Delete the
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } });

Batz
  • 69
  • 10