I am trying to show pause button in second activity - java

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);

Related

Setting up a text view as a clickable button

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

Android application crashes at Intent [duplicate]

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.

"App has stopped" "Close app" android studio

ACTIVITY_MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="#77dd77"
tools:context=".MainActivity">
<ImageView
android:id="#+id/splashIcon"
android:layout_width="97dp"
android:layout_height="216dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:contentDescription="#string/todo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo1" />
</android.support.constraint.ConstraintLayout>
MAIN_SCREEN.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Main Screen BVOOM!"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
SCREENONE.JAVA
package com.example.admin.test2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ScreenOne extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
}
}
MAINACTIVITY.JAVA
package com.example.admin.test2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Timer().schedule(new TimerTask(){
#Override
public void run(){
startActivity(new Intent(getApplicationContext(), ScreenOne.class));
}
}, 2000);
}
}
ANDROID MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.test2">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ScreenOne">
</activity>
</application>
</manifest>
Error from Logcat/Run tab:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.test2, PID: 16184
java.lang.RuntimeException: Canvas: trying to draw too large(144000000bytes) bitmap.
at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:229)
at android.view.RecordingCanvas.drawBitmap(RecordingCanvas.java:97)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:529)
at android.widget.ImageView.onDraw(ImageView.java:1367)
at android.view.View.draw(View.java:20370)
at android.view.View.updateDisplayListIfDirty(View.java:19315)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.draw(View.java:20373)
at android.view.View.updateDisplayListIfDirty(View.java:19315)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.draw(View.java:20373)
at com.android.internal.policy.DecorView.draw(DecorView.java:980)
at android.view.View.updateDisplayListIfDirty(View.java:19315)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:686)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:692)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:800)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3501)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3288)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2823)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1785)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7822)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.
When I build my file and emulate in my Phone Samsung Galaxy A3 2017 the app will crash and say "Test2 has stopped." "Close app" what can I do with these please help
Bro, In the SCREENONE.JAVA, you have, override the same method twice. Please correct it and your app your run perfectly

App stops when launching a new activity

I've been trying to launch a new activity in my app and, although it does for a moment, inmediately after opening the new screen, the app stops. I've already added the new actvity to the manifest.
This is the method I use to call to the new activity, whuich worked perfectly when it only had the toast in the clickListener:
public void Bs()
{
View.OnClickListener listSet = new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
Intent intent= new Intent(MainActivity.this, Set.class);
startActivity(intent);
}
};
b= (ImageButton) findViewById(R.id.imageButton7);
b.setOnClickListener(listSet);
}
This is the class that is being called:
public class Set extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
}
}
And this is the layout class setting that only has a textview(I've tried several layouts and nne of them work, so I guess this is no the problem but I add it anyway in case it helps):
?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:text="#string/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
These are the errors I have in the logcat:
--------- beginning of crash
07-23 07:10:04.022 3267-2868/? A/google-breakpad: -----BEGIN BREAKPAD MICRODUMP-----
07-23 07:10:04.022 3267-2868/? A/google-breakpad: V WebView:51.0.2704.90
(...)
07-23 07:10:11.548 2202-2891/? E/SystemUpdateService: Failed to call RecoverySystem.cancelScheduledUpdate
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at xae.c(:com.google.android.gms:134)
at afuw.d(:com.google.android.gms:195)
at afuw.p(:com.google.android.gms:2178)
at afuw.a(:com.google.android.gms:448)
at afuw.doInBackground(:com.google.android.gms:50475)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.io.IOException: cancel scheduled update failed
at android.os.RecoverySystem.cancelScheduledUpdate(RecoverySystem.java:555)
at java.lang.reflect.Method.invoke(Native Method) 
at xae.c(:com.google.android.gms:134) 
at afuw.d(:com.google.android.gms:195) 
at afuw.p(:com.google.android.gms:2178) 
at afuw.a(:com.google.android.gms:448) 
at afuw.doInBackground(:com.google.android.gms:50475) 
at android.os.AsyncTask$2.call(AsyncTask.java:304) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at java.lang.Thread.run(Thread.java:761) 
07-23 07:10:12.453 2202-3333/? W/art: Verification of com.google.android.gms.common.data.DataHolder[] com.google.android.gms.games.broker.AppContentAgent.loadCardStream$3489344c(com.google.android.gms.games.broker.GamesClientContext, com.google.android.gms.games.broker.AppContentContext, long) took 841.459ms
07-23 07:10:12.592 2202-2891/? E/SystemUpdateTask: exception trying to cancel scheduled update
java.io.IOException: Failed to invoke RecoverySystem.cancelScheduledUpdate
at xae.c(:com.google.android.gms:140)
at afuw.d(:com.google.android.gms:195)
at afuw.p(:com.google.android.gms:2178)
at afuw.a(:com.google.android.gms:448)
at afuw.doInBackground(:com.google.android.gms:50475)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.lang.Thread.run(Thread.java:761)
[ 07-23 07:10:12.629 2443: 2488 D/ ]
HostConnection::get() New Host Connection established 0x895a1900, tid 2488
(...)
07-23 07:10:44.198 1534-2908/? E/RecoverySystemService: Timed out connecting to uncrypt socket
07-23 07:10:44.198 1534-2908/? E/RecoverySystemService: Failed to connect to uncrypt socket
07-23 07:10:44.201 2202-3376/? E/SystemUpdateService: Failed to call RecoverySystem.cancelScheduledUpdate
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at xae.c(:com.google.android.gms:134)
at afuw.d(:com.google.android.gms:195)
at afuw.p(:com.google.android.gms:2178)
at afuw.a(:com.google.android.gms:448)
at afuw.doInBackground(:com.google.android.gms:50475)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.io.IOException: cancel scheduled update failed
at android.os.RecoverySystem.cancelScheduledUpdate(RecoverySystem.java:555)
at java.lang.reflect.Method.invoke(Native Method) 
at xae.c(:com.google.android.gms:134) 
at afuw.d(:com.google.android.gms:195) 
at afuw.p(:com.google.android.gms:2178) 
at afuw.a(:com.google.android.gms:448) 
at afuw.doInBackground(:com.google.android.gms:50475) 
at android.os.AsyncTask$2.call(AsyncTask.java:304) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at java.lang.Thread.run(Thread.java:761) 
07-23 07:10:44.201 2202-3376/? E/SystemUpdateTask: exception trying to cancel scheduled update
java.io.IOException: Failed to invoke RecoverySystem.cancelScheduledUpdate
at xae.c(:com.google.android.gms:140)
at afuw.d(:com.google.android.gms:195)
at afuw.p(:com.google.android.gms:2178)
at afuw.a(:com.google.android.gms:448)
at afuw.doInBackground(:com.google.android.gms:50475)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.lang.Thread.run(Thread.java:761)
Am I doing something wrong when calling the new activity? Or is it the class I'm calling the one isn't fine?
EDIT
This is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xx.yy">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:debuggable="false"
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="com.xx.yy.MainActivity"
android:configChanges= "orientation|screenSize"
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.xx.yy.Set"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
I have been doing some more attempts and I think the problem was I had fullScreen on the first activity but not on the second one. Therefore, adding these two lines to the Set class solved the problem:
public class Set extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.settings);
}
}
Can you share your MANIFEST file here
I tried the similar kind of code you were written, Check the working code below.
MainActivity.java
package com.service.rajeshm.activitytest;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener listSet = new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
Intent intent= new Intent(MainActivity.this, Set.class);
startActivity(intent);
}
};
ImageButton b= (ImageButton) findViewById(R.id.imageButton1);
b.setOnClickListener(listSet);
}
}
activity_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:id="#+id/activity_main"
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"
tools:context="com.service.rajeshm.activitytest.MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="17dp"
android:id="#+id/imageButton1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Set.java
package com.service.rajeshm.activitytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Set extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set);
}
}
activity_set.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:id="#+id/activity_set"
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"
tools:context="com.service.rajeshm.activitytest.Set">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#android:drawable/btn_star_big_on"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="64dp"
android:layout_marginStart="64dp"
android:layout_marginTop="48dp"
android:id="#+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="32dp"
android:layout_marginEnd="32dp" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service.rajeshm.activitytest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Set"></activity>
</application>
</manifest>

android.view.InflateException: Binary XML file line #52: Error inflating class <unknown>

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>

Categories