App stops when launching a new activity - java

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>

Related

Java Resource errors

We are having a resource error when we try to run our code.
package com.example.searchtest;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class GoogleSearchIntentActivity extends Activity {
private EditText editTextInput;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = (EditText) findViewById(R.id.editTextInput);
}
public void onSearchClick(View v)
{
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = editTextInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp">
<edittext
android:id="#+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search text">
<requestfocus>
</requestfocus></edittext>
<button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_gravity="center"
android:onclick="onSearchClick"
android:layout_margintop="10dp">
</button></linearlayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchtest">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<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=".GoogleSearchIntentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
these are our mainActivity.java, activity_main.xml, and AndroidManifest.xml codes from our app.
The error is within our public class GoogleSearchIntentActivity extends Activity
We are high school students trying to create an image search app for our computer science capstone class, we have been trying to fix this error without success, and our teacher is completely clueless.
Components in your xml file must have a first capitalized letter.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp">
<EditText
android:id="#+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search text">
<requestFocus>
</requestFocus></EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_gravity="center"
android:onclick="onSearchClick"
android:layout_margintop="10dp">
</Button></LinearLayout>

"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

I am trying to show pause button in second activity

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

When i click on my button my app crashes

i'm fairly new to coding but decided i would take the huge plunge into the ocean without knowing how to swim, anyway my button keeps failling to open my new activity when i click on it, instead it just crashes. when I run it on my emulator i get a message saying that it has stopped. I'm using a genymotion emulator and the darcula android studio version.
My main activity.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="wrap_content" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#drawable/background_black">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unpressed_button"
android:background="#drawable/unpressed"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
My second Activity.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/background_black"
tools:context="quirkykoders.flash_flash.Pressed_Button">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pressed_button"
android:background="#drawable/pressed"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="quirkykoders.flash_flash" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Pressed_Button"
android:label="#string/title_activity_pressed__button" >
<intent-filter>
<action android:name="android.intent.action.PRESSED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
My java
package quirkykoders.flash_flash;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static Button button_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.unpressed_button);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent ("quirkykoders.flash_flash.Pressed_Button");
startActivity(intent);
}
}
);
}
}
Any help will be greatly appreciated
When you want to create A new Activity you have to send two parameters in the intent object First is currentActivity name and the second is CallingActivity name
Intent intent=new Intent(MainActivity.this,Pressed_Button.class);
startActivity(intent);

Blank Page after clicking button android eclipse?

I thought I did everything right but when I click the button I get a blank page instead of a page with a text. I want that the creation.xml comes up after clicking the button on the main xml. What did I wrong ?
My main activity 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: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=".MainActivity"
android:background="#drawable/creeper" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/hello"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:textSize="20sp"
android:text="#string/button1_text" />
</RelativeLayout>
My main activity java
package com.berkcoop.deneme;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(MainActivity.this, Creation.class);
startActivity(intent1);
}
});
}
My creation xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Creations"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.berkcoop.deneme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.berkcoop.deneme.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.berkcoop.deneme.Creation"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
and my creation java
package com.berkcoop.deneme;
import android.app.Activity;
public class Creation extends Activity{
#Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
super.setContentView(R.layout.creation);
}
}
Thank you..
Why do you override the setContentView method in your Creation class? Since you don't call it, it won't set the correct view.
You should call setContentView from onCreate, and as far as I know, there is no need to override setContentView.
Try this in Creation:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.creation);
}

Categories