This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm new to android programming, now my application ceashes. The MainAcitivity is used to login, when the "login" button was clicked, it should hava been changed to another activity, but it crashed.
Here's my MainAcitivity code:
package com.example.mt;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText accountEdit;
private EditText passwordEdit;
private Button login_button;
private CheckBox rememberPass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences settings = getPreferences(MODE_PRIVATE);
accountEdit = findViewById(R.id.account);
passwordEdit = findViewById(R.id.password);
rememberPass = findViewById(R.id.remember_pass);
login = findViewById(R.id.login);
boolean isRemember = settings.getBoolean("remember_pass",false);
if(isRemember){
String account = settings.getString("account","");
String password = settings.getString("password","");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}
login_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
if (account.equals("tianmiao")&&password.equals("tianmiao")){
SharedPreferences.Editor editor = settings.edit();
if(rememberPass.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("account",account);
editor.putString("password",password);
} else{
editor.clear();
}
editor.apply();
Intent intent = new Intent(MainActivity.this,SearchActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(MainActivity.this,"wrong account or password(both tianmiao)",Toast.LENGTH_SHORT).show();
}
}
});
}
}
Here's SearchAcitivity code:
package com.example.mt;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SearchActivity extends AppCompatActivity {
private EditText searchEditText;
private Button search_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
searchEditText = findViewById(R.id.search);
search_button = findViewById(R.id.search_button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
search_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String search = searchEditText.getText().toString();
if(search.equals("balabala")){
Intent intent = new Intent(SearchActivity.this,ListActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(SearchActivity.this,"No"+search+"(there's only balabala)",Toast.LENGTH_SHORT).show();
}
}
});
}
}
MainAcitivity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="account:"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:id="#+id/account"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="password:"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:id="#+id/password"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/remember_pass"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="remember"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="#+id/login_button"
android:text="login"/>
</LinearLayout>
SearchActivity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".SearchActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="buy what?:"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:id="#+id/search"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="#+id/search_button"
android:text="search"/>
</LinearLayout>
Here's Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mt">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SearchActivity"></activity>
<activity android:name=".ListActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
red part of logcat:
2019-12-22 19:10:30.600 12135-12135/? E/com.example.mt: Unknown bits set in runtime_flags: 0x8000
2019-12-22 19:10:31.403 12135-12161/com.example.mt E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-12-22 19:10:31.404 12135-12161/com.example.mt E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
Process: com.example.mt, PID: 12135
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mt/com.example.mt.SearchActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.mt.SearchActivity.onCreate(SearchActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2019-12-22 19:10:49.884 12135-12135/com.example.mt I/Process: Sending signal. PID: 12135 SIG: 9
Thank you!
You are connecting views with ids before layout is inflated and that gives you and null pointer exception here:
searchEditText = findViewById(R.id.search);
search_button = findViewById(R.id.search_button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
You need to move:
searchEditText = findViewById(R.id.search);
search_button = findViewById(R.id.search_button);
below: setContentView(R.layout.activity_search); like this:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
//now view is inflated you can connect views with ids
searchEditText = findViewById(R.id.search);
search_button = findViewById(R.id.search_button);
Your original query has already been answered, as for the problem with remember password, it is not working because the value for getting the remember password is
remember_pass
boolean isRemember = settings.getBoolean("remember_pass",false);
And the value for setting the SharedPreference is
remember_password
editor.putBoolean("remember_password",true);
Make them same and it will work.
Related
I am developing a simple application which allows users to login using a username and password, I am trying to set up a "register" link in the form of a TextView which will take the user to the register page of the app. I can't find any error in my coding or logic but when I run the app and click the register link, the app just shuts down.
Here is my MainActivity code -
package com.example.squashsademo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText mTextUsername;
EditText mTextPassword;
Button mButtonLogin;
TextView mTextViewRegister;
// #RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mButtonLogin = (Button)findViewById(R.id.button_login);
mTextViewRegister = (TextView)findViewById(R.id.textview_register);
mTextViewRegister.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent registerIntent = new Intent(MainActivity.this,RegisterActivity.class);
startActivity(registerIntent);
}
});
}
}
Here is the RegisterActivity I am trying to get to -
package com.example.squashsademo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RegisterActivity extends AppCompatActivity {
EditText mTextUsername;
EditText mTextPassword;
EditText mTextCnfPassword;
Button mButtonRegister;
TextView mTextViewLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mTextCnfPassword = (EditText) findViewById(R.id.edittext_cnf_password);
mButtonRegister = (Button)findViewById(R.id.button_login);
mTextViewLogin = (TextView)findViewById(R.id.textview_register);
mTextViewLogin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent LoginIntent = new Intent(RegisterActivity.this,MainActivity.class);
startActivity(LoginIntent);
}
});
}
}
and here is my AndroidManifest -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.squashsademo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.SquashSADemo">
<activity android:name=".RegisterActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Can anyone help see why this is not working because from my understanding it should be :(
Thank you
EDIT - here are the layout files aswell
MainActivity -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#drawable/background">
<ImageView
android:layout_width="299dp"
android:layout_height="80dp"
app:srcCompat="#drawable/logo" />
<EditText
android:id="#+id/edittext_username"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="160dp"
android:background="#drawable/custom_input"
android:drawableLeft="#drawable/username"
android:drawablePadding="12dp"
android:hint="#string/username"
android:textColorHint="#color/white"/>
<EditText
android:id="#+id/edittext_password"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:background="#drawable/custom_input"
android:drawableLeft="#drawable/password"
android:drawablePadding="12dp"
android:hint="#string/password"
android:textColorHint="#color/white"/>
<Button
android:id="#+id/button_login"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:text="#string/login"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#ffffff"
android:text="#string/not_registered"/>
<TextView
android:id="#+id/textview_register"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:text="#string/register"/>
</LinearLayout>
</LinearLayout>
RegisterActivity -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#drawable/background"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".RegisterActivity">
<ImageView
android:layout_width="298dp"
android:layout_height="80dp"
app:srcCompat="#drawable/logo" />
<EditText
android:id="#+id/edittext_username"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="160dp"
android:background="#drawable/custom_input"
android:drawableLeft="#drawable/username"
android:drawablePadding="12dp"
android:hint="#string/username"
android:textColorHint="#color/white"/>
<EditText
android:id="#+id/edittext_password"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:background="#drawable/custom_input"
android:drawableLeft="#drawable/password"
android:drawablePadding="12dp"
android:hint="#string/password"
android:textColorHint="#color/white" />
<EditText
android:id="#+id/edittext_cnf_password"
android:drawablePadding="12dp"
android:background="#drawable/custom_input"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:drawableLeft="#drawable/password"
android:hint="#string/confirm_password"
android:textColorHint="#color/white"/>
<Button
android:id="#+id/button_register"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:text="#string/register"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#ffffff"
android:text="#string/already_registered"/>
<TextView
android:id="#+id/textview_login"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="16dp"
android:text="#string/login"/>
</LinearLayout>
</LinearLayout>
here are the log files aswell -
2020-10-21 20:56:22.881 11328-11328/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.squashsademo, PID: 11328
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.squashsademo/com.example.squashsademo.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.squashsademo.RegisterActivity.onCreate(RegisterActivity.java:28)
at android.app.Activity.performCreate(Activity.java:7963)
at android.app.Activity.performCreate(Activity.java:7952)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3629)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Below line of code is throwing NullPoinerException
mTextViewLogin = (TextView)findViewById(R.id.textview_register);
in the register activity layout, there is no id as textview_register that's why you are getting the issue
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
possible fix using below line
mTextViewLogin = (TextView)findViewById(R.id.textview_login);
instead of
mTextViewLogin = (TextView)findViewById(R.id.textview_register);
you can see you are trying to link XML view to java with an invalid id
This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
When i want to run app in my device i get this error in logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.allo/com.example.android.allo.WelcomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setAnimation(android.view.animation.Animation)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setAnimation(android.view.animation.Animation)' on a null object reference
at com.example.android.allo.WelcomeActivity.onCreate(WelcomeActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
WelcomActivty.java :
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
public class WelcomeActivity extends AppCompatActivity {
LinearLayout l1,l2;
Button btnsub;
Animation uptodown,downtoup;
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.l1);
LinearLayout linearLayout1=(LinearLayout) findViewById(R.id.l2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
btnsub = (Button)findViewById(R.id.buttoncom);
uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
}
protected void onStart(){
super.onStart();
linearLayout.startAnimation(uptodown);
linearLayout1.startAnimation(downtoup);
}
public void com(View view){
Intent intent = new Intent(this, Login.class);
Button buttoncom = (Button) findViewById(R.id.buttoncom);
startActivity(intent);
}
}
welcome.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.allo.WelcomeActivity"
android:background="#drawable/background"
android:orientation="vertical">
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="120dp"
android:text="#string/bienvenue"
android:textColor="#color/lightorange"
android:textSize="60sp"
android:textStyle="bold" />
<TextView
android:textColor="#color/lightorangedark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/dans_quoi"
android:textAlignment="center"
android:textSize="20sp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/l2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="115dp"
android:background="#drawable/spaceullustration"
android:orientation="vertical">
<Button
android:id="#+id/buttoncom"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstyle"
android:text="#string/button_commencer" />
</LinearLayout>
activity_login.xml
<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:background="#drawable/backgroudlogin"
tools:context=".Login">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="232dp"
android:background="#drawable/ic_email_button"
android:hint=" Email"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="159dp"
android:background="#drawable/ic_email_button"
android:hint=" password"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:background="#drawable/ic_sign_in"
android:text="Sign IN" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="create an account"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
Login.xml
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
i know, there is a lot of question in the forum with the same subject but I can not find the solution, so please do not mark the question as duplicated
You should do this way -
public class WelcomeActivity extends AppCompatActivity {
Button btnsub;
Animation uptodown,downtoup;
LinearLayout linearLayout;
LinearLayout linearLayout1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
linearLayout=(LinearLayout) findViewById(R.id.l1);
linearLayout1=(LinearLayout) findViewById(R.id.l2);
btnsub = (Button)findViewById(R.id.buttoncom);
uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);
linearLayout.setAnimation(uptodown);
linearLayout1.setAnimation(downtoup);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I got my App running for the most part.
Only problem: I want a login screen before the main Window.
Included the Intent stuff, but the login will crash
I don't know what exactly I'm doing wrong. I'm still learning to use the debugger. The MainActivity class will work without any problems
the crash says:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cris.zeiterfassungv2, PID: 19716
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cris.zeiterfassungv2/com.example.cris.zeiterfassungv2.login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
at com.example.cris.zeiterfassungv2.login.onCreate(login.java:34)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
my login.java is
package com.example.MrX.zeiterfassungv2;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class login extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
ed1 = (EditText)findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
b2 = (Button)findViewById(R.id.button2);
tx1 = (TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") && ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...",Toast.LENGTH_SHORT).show();
Intent i = new Intent(login.this, MainActivity.class);
startActivity(i);
}
else{
Toast.makeText(getApplicationContext(), "Fehlerhafte Eingabe",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
login.xml is
<?xml version = "1.0" encoding = "utf-8"?>
<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"
tools:context = ".MainActivity">
<TextView android:text = "#string/login" android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/textview"
android:textSize = "35dp"
android:textColor = "#00549F"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "#string/rwth_aachen"
android:id = "#+id/textView"
android:layout_below = "#+id/textview"
android:layout_centerHorizontal = "true"
android:textColor = "#00549F"
android:textSize = "35dp" />
<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/editText"
android:hint = "#string/name"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "46dp"
android:layout_below = "#+id/imageView"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="#string/passwort" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/verbleibende_versuche"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textview"
android:textSize="20dp"
android:layout_toRightOf="#+id/textview" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/textview"
android:layout_below="#+id/textView2"
android:layout_marginTop="58dp"
android:text="login" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App verlassen"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toEndOf="#+id/textview" />
The Manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cris.zeiterfassungv2">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".login"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:exported="true"/>
</application>
Thank you
You aren't inflating the right layout. In your Activity (called login), replace this line setContentView(R.layout.activity_main); by this setContentView(R.layout.login);.
My MainActivity java file
package com.example.lenovo.infinity.app;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends Activity {
RelativeLayout btn;
ImageView imageView;
TextView textView;
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = (AdView)findViewById(R.id.adsView);
//adView.setAdSize(AdSize.SMART_BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
//set font to text view
textView = (TextView)findViewById(R.id.start_Text);
Typeface custom = Typeface.createFromAsset(getAssets(),"fonts/font.ttf");
textView.setTypeface(custom);
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Game.class);
startActivity(myIntent);
}
});
}
}
Game java file
package com.example.lenovo.infinity.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
public class Game extends Activity {
View pauseButton;
RelativeLayout Rel_main_game;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
final int heights = dm.heightPixels;
final int widths = dm.widthPixels;
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext()
.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
pauseButton = layoutInflater.inflate(R.layout.activity_pause,null,false);
pauseButton.setX(widths - 250);
pauseButton.setY(0);
Rel_main_game.addView(pauseButton);
pauseButton.getLayoutParams().height = 250;
pauseButton.getLayoutParams().width = 250;
}
}
In my project, I have three layouts: the first, layout is main activity which has start button, the second layout is game panel which contains a pause button and button is the third layout
the XML files are
First XML, this xml have start button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_activity"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:scaleType="fitXY"
android:src="#drawable/game_bg"
android:contentDescription="#string/bg" />
<RelativeLayout
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_marginBottom="#dimen/layout_marginBottom"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/StartBtn"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:src="#drawable/btn"
android:scaleType="fitXY" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start_game"
android:id="#+id/start_Text"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="#dimen/textSize"
android:textAppearance="#android:style/TextAppearance.Medium" />
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/adsView"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-5303974617236905/1000694879" />
</RelativeLayout>
The second XML, this contains the main game panel with pause button
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/rel_main_game"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.lenovo.infinity.app.Game">
</RelativeLayout>
The third layout is a pause button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/pause"
tools:context="com.example.lenovo.infinity.app.Pause"
android:background="#000000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pauseImage"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/pause_margin"
android:scaleType="fitXY"
android:src="#drawable/btn" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="II"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="#dimen/pause_text_size"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
</RelativeLayout>
My Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.infinity.app">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<activity
android:name=".Game"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
Logcat
04-04 19:47:25.021 8325-8432/? D/GassUtils: Found app info for package com.example.lenovo.infinity.app:1. Hash: e93251d423a7add58a3428a9e259cdd8b57c335ec3a30cc9897279fdeed6e512
04-04 19:47:25.021 8325-8432/? D/k: Found info for package com.example.lenovo.infinity.app in db.
04-04 19:47:25.235 514-703/? V/ActivityManager: com.example.lenovo.infinity.app/.MainActivity: task=TaskRecord{41c684b8 #132 A com.example.lenovo.infinity.app U 0}
04-04 19:47:25.774 8409-8409/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo.infinity.app/com.example.lenovo.infinity.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.lenovo.infinity.app.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
04-04 19:47:25.782 514-531/? W/ActivityManager: Force finishing activity com.example.lenovo.infinity.app/.MainActivity
04-04 19:47:25.789 131-131/? I/BufferQueue: [Starting com.example.lenovo.infinity.app](this:0x41f78008,api:2) new GraphicBuffer needed
04-04 19:47:25.803 131-14023/? I/BufferQueue: [Starting com.example.lenovo.infinity.app](this:0x41f78008,api:2) [queue] fps:0.28, dur:3530.34, max:3530.34, min:3530.34
04-04 19:47:25.820 131-204/? I/SurfaceTexture: [Starting com.example.lenovo.infinity.app](this:0x41f91558,api:2) [void* android::SurfaceTexture::createImage(EGLDisplay, const android::sp<android::GraphicBuffer>&)]
04-04 19:47:25.943 8409-8454/? D/dalvikvm: open_cached_dex_file : /data/data/com.example.lenovo.infinity.app/cache/ads1962259775.jar /data/data/com.example.lenovo.infinity.app/cache/ads1962259775.dex
04-04 19:47:26.024 514-535/? V/WindowManager: Changing focus from null to Window{41cae560 u0 Application Error: com.example.lenovo.infinity.app}
04-04 19:47:26.024 514-534/? I/WindowManager: Gaining focus: Window{41cae560 u0 Application Error: com.example.lenovo.infinity.app}
Logcat
You are using the same botton for both start and pause botton:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pauseImage"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/pause_margin"
android:scaleType="fitXY"
android:src="#drawable/btn" />
Change it for:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pauseImage"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/pause_margin"
android:scaleType="fitXY"
android:src="#drawable/btn_pause" />
where btn_pauseis the source of the pause image.
In the second xml you mentioned that you have pause button but the snippet you posted has relative layout only. Use Framelayout if you want to show button on top of the other view.
Also in third xml, you need to change the src to pause image reference. currently you are referring to the same btn image.
Add this in mainActivity
imageView = (ImageView)findViewById(R.id.StartBtn);
btn = (RelativeLayout)findViewById(R.id.Start_layout);
i Have Error when i choose ImageButton to get next Activty here the logcat :
E/AndroidRuntime(820): FATAL EXCEPTION: main
E/AndroidRuntime(820): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pengendalipagar/com.ta.pengendalipagar.MnUtama}: android.view.InflateException: Binary XML file line #52: Error inflating class <unknown>
E/AndroidRuntime(820): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(820): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(820): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(820): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(820): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(820): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(820): at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(820): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(820): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(820): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(820): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(820): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(820): Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class <unknown>
E/AndroidRuntime(820): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
E/AndroidRuntime(820): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime(820): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
E/AndroidRuntime(820): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
This is My Code activity1 activity_login.xml :
<?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"
android:background="#drawable/cover" >
<TextView
android:id="#+id/txtPasscode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/editText1"
android:layout_marginBottom="42dp"
android:text="#string/passcode"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imbLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtPasscode"
android:layout_marginLeft="26dp"
android:layout_marginTop="22dp"
android:src="#drawable/login"/>
<ImageButton
android:id="#+id/imbExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imbLogin"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/imbLogin"
android:src="#drawable/exit"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:src="#drawable/logo" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imbLogin"
android:layout_alignRight="#+id/imbExit"
android:layout_below="#+id/imageView1"
android:layout_marginTop="69dp"
android:ems="10"
android:inputType="numberPassword" >
<requestFocus />
</EditText>
</RelativeLayout>
and This my code of activity1 LoginActivity.java :
package com.ta.pengendalipagar;
import com.example.pengendalipagar.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class LoginActivity extends Activity{
/*private ImageButton Login;
private ImageButton Exit;
private EditText Passcode;*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ImageButton Login = (ImageButton)findViewById(R.id.imbLogin);
ImageButton Exit = (ImageButton)findViewById(R.id.imbExit);
//EditText Passcode = (EditText)findViewById(R.id.txtPasscode);
Login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callLogin();
}
});
Exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callKeluar();
}
});
}
public void callLogin(){
Intent myMenu = new Intent(this, MnUtama.class);
startActivity(myMenu);
}
public void callKeluar(){
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
This is My Code activity2 activity_mnutama.xml
<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:background="#drawable/cover">
<ImageButton
android:id="#+id/imbOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="141dp"
android:src="#drawable/open" />
<ImageButton
android:id="#+id/imbClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imbOpen"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/imbOpen"
android:src="#drawable/close" />
<ImageButton
android:id="#+id/imbSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imbOpen"
android:layout_below="#+id/imbOpen"
android:layout_marginTop="25dp"
android:src="#drawable/setting" />
<ImageButton
android:id="#+id/imbExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/imbClose"
android:layout_alignTop="#+id/imbSetting"
android:src="#drawable/exit"/>
<EditText
android:id="#+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imbClose"
android:layout_alignLeft="#+id/imbOpen"
android:layout_alignRight="#+id/imbClose"
android:layout_marginBottom="16dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="#string/main_menu"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#style/AppBaseTheme"
android:textSize="15pt"
android:textStyle="bold" />
</RelativeLayout>
and This my code of activity2 MnUtama.java :
package com.ta.pengendalipagar;
import com.example.pengendalipagar.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class MnUtama extends Activity {
/*private ImageButton Open;
private ImageButton Close;
private ImageButton Setting;
private ImageButton Exit;
private EditText Status;*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mnutama);
ImageButton Open = (ImageButton)findViewById(R.id.imbOpen);
ImageButton Close = (ImageButton)findViewById(R.id.imbClose);
ImageButton Setting = (ImageButton)findViewById(R.id.imbSetting);
ImageButton Exit = (ImageButton)findViewById(R.id.imbExit);
EditText Status = (EditText)findViewById(R.id.txtStatus);
Exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callKeluar();
}
});
}
public void callKeluar(){
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
And this is my Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pengendalipagar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.ta.pengendalipagar.LoginActivity"
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.ta.pengendalipagar.MnUtama"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I believe the problem is at the following string at the last TextView of activity_mnutama.xml:
android:textColor="#style/AppBaseTheme"
You are doing it a wrong way, delete this line. To set textColor via style, you should add this line to the TextView declaration:
style="#style/AppBaseTheme"
where AppBaseTheme is like following:
<style name="AppBaseTheme">
<item name="android:textColor">#FFF</item>
</style>