I have two activities:
MainActivity
Play
In MainActivity - there is a ImageButton - on clicking of which Intent will call Play activity.
But after calling startActivity(i) - My programe stopes.
Please please help
My manifest file is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multiplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Multiplication"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Multiplication">
<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=".Play"> </activity>
</application>
</manifest>
=====================================
And the MainActivity is:
package com.example.multiplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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);
ImageButton btnMathPlay = (ImageButton) findViewById(R.id.play);
ImageButton btnMathShare= (ImageButton) findViewById(R.id.share);
ImageButton btnMathRate = (ImageButton) findViewById(R.id.grade);
btnMathPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//
// Example of Explicit Intent
// When you click Play Button on the screen
// Game Activity will be started
//
//Intent i = new Intent(MainActivity.this,Play.class);
Intent i=new Intent(MainActivity.this, Play.class);
startActivity(i);
}
});
}
}
The error is:
public void run() {
try {
mMethod.invoke(null, new Object[] { mArgs });
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException(ex);
}
Just stops program
Related
This is a read/scan NFC code.
I'm trying it out and I need it to work so that I would write my own code for another read activity.
The app keeps on crashing and the error is always, "There's a bug, please fix it for the app to start."
The source video/code was this link https://www.youtube.com/watch?v=QzphwRdJ7r0
Main Activity
package com.example.nfcreadscan;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private IntentFilter[] readfilters;
private PendingIntent pendingintent;
#SuppressLint("UnspecifiedImmutableFlag")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
try {
Intent intent = new Intent (this, getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingintent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
IntentFilter jdf = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
jdf.addDataScheme("http");
jdf.addDataAuthority("javadude.com", null);
IntentFilter jdt = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED,"text/plain");
readfilters = new IntentFilter[]{jdf, jdt};
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
processNFC(getIntent());
}
private void enableRead(){
NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this,pendingintent, readfilters,null);
}
private void disableRead(){
NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}
#Override
protected void onResume(){
super.onResume();
enableRead();
}
#Override
protected void onPause(){
super.onPause();
disableRead();
}
#Override
protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
processNFC(intent);
}
private void processNFC(Intent intent){
Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
textView.setText("");
if(messages != null){
for(Parcelable message : messages){
NdefMessage ndefMessage = (NdefMessage) message;
for(NdefRecord record : ndefMessage.getRecords()) {
if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN) {
textView.append("well known: ");
if (Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) {
textView.append("text: ");
textView.append(new String(record.getPayload()));
textView.append("\n");
} else if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
textView.append("uri");
textView.append(new String(record.getPayload()));
textView.append("\n");
}
}
}
}
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfcreadscan">
<uses-permission android:name="android.permission.NFC"/>
<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.Nfcreadscan">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
First, I'm a beginner for android. I'm developing a simple login application by using firebase, when I run my code I faced this Runtime exception.i'm using phone number authentication, The application was installed successfully, but I can't go to TuserActivity can anyone tell me how to solve this?
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{TuserActivity}: java.lang.IllegalAccessException: class TuserActivity is not accessible from class android.app.Instrumentation
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public EditText emailId, passwd;
Button btnSignUp;
TextView signIn,txuser;
FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.ETemail);
passwd = findViewById(R.id.ETpassword);
btnSignUp = findViewById(R.id.btnSignUp);
signIn = findViewById(R.id.TVSignIn);
txuser = findViewById(R.id.tuser);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String emailID = emailId.getText().toString();
String paswd = passwd.getText().toString();
if (emailID.isEmpty()) {
emailId.setError("Enter your E-mail");
emailId.requestFocus();
} else if (paswd.isEmpty()) {
passwd.setError("Enter your password");
passwd.requestFocus();
} else if (emailID.isEmpty() && paswd.isEmpty()) {
Toast.makeText(MainActivity.this, "Fields Empty!", Toast.LENGTH_SHORT).show();
} else if (!(emailID.isEmpty() && paswd.isEmpty())) {
firebaseAuth.createUserWithEmailAndPassword(emailID, paswd).addOnCompleteListener(MainActivity.this, new OnCompleteListener() {
#Override
public void onComplete(Task task) {
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this.getApplicationContext(),
"SignUp unsuccessful: " + task.getException().getMessage(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(MainActivity.this, UserActivity.class));
}
}
});
} else {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
});
signIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent I = new Intent(MainActivity.this, Activity_Login.class);
startActivity(I);
}
});
txuser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent I = new Intent(MainActivity.this, TuserActivity.class);
startActivity(I);
}
});
}
}
manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<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=".Activity_Login"></activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"></activity>
<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=".UserActivity" />
<activity android:name=".VerifyPhoneActivity" />
<activity android:name=".TuserActivity" />
</application>
It just happened to me.
I was forgetting to set my activity (in your case, TuserActivity) as public with the public modifier. It seems that Android can't access a java Activity that is not explicitly public. e.g:
public class TuserActivity extends AppCompatActivity
Also, if you're new to Android in 2020, then you really should consider starting with Kotlin.
I've created a splash screen and it runs as intended. However, once the app has launched once, and is launched again, the splash screen doesn't show. It only shows at first launch which is not how it's supposed to work. I don't know how to resolve this. The splash screen code is posted below.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.content.Intent;
public class SplashScreenActivity extends AppCompatActivity {
private int SLEEP_TIMER = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
getSupportActionBar().hide();
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
private class LogoLauncher extends Thread{
public void run(){
try{
sleep(1000 * SLEEP_TIMER);
}catch(InterruptedException e){
e.printStackTrace();
}
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}
}
EDIT: I'm testing on my physical device in android studio. When I hit run and the app launches it works. If I quit the app and launch it again from my phone* It doesn't work
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jstudios.main">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="MainApp"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="MainApp"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
edit
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.content.Intent;
import android.os.Handler;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
getSupportActionBar().hide();
Handler h =new Handler() ;
h.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}, 5000);
}
}
please use handler to trigger your startActivity
setContentView(R.layout.activity_splash_screen);
getSupportActionBar().hide();
Handler h =new Handler() ;
h.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}, 5000);
Or use Timer
new Timer().schedule(new TimerTask() {
#Override
public void run()
{
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish()
}
}, 5000);
Sleeping the main thread to display a Splash screen is really a bad practice.
The Splash screen should be displayed only while the app is loading (not a fixed time) to your main activity.
I recommend this approach, I've been using it in my own apps:
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
No problems in first activity open on emulator but the problem is when press on the button it upon
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainlayoutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
final Button man = (Button) findViewById(R.id.getDATA);
man.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainlayoutActivity.this, second.class);
startActivityForResult(i, 77);
}
});
}
public void onActivityResult(int requstCode, int resultCode, Intent data) {
if ((requstCode == 77) && (resultCode == Activity.RESULT_OK)) {
String gett = data.getExtras().getString("TEXT value");
Toast.makeText(this, gett, Toast.LENGTH_LONG).show();
}
}
}
the problem is start second activity on emulator
public class second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seconlayout);
final Button btn = (Button) findViewById(R.id.GDATA);
final EditText ed = (EditText) findViewById(R.id.value);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String word = ed.getText().toString();
Intent i = new Intent();
i.putExtra("TEXT value", word);
setResult(Activity.RESULT_OK, i);
finish();
}
});
}
}
And the manifest file
<? xml version = "1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.l9"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.l9.MainlayoutActivity"
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.example.l9.second">
<intent-filter>
<action android:name="net.abdullaheid.action.ToDATA"/>
<category android:name="android.intent.categor.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
Intent intent = new Intent();
This without parameters is useless if you dont want to transfer from this activity back. You need to create Intent with parameters
Intent(ThisActivity.this, WhereIWantToGoActivity.class) and then call startActivity(intent).
So, you got error, because your Intent() constructor is empty.
I am trying to broadcast a toast message with the following code extending Activity. But the broadcast is not received by another Activity, the toast is not displayed. Can someone solve my error? The main activity is SendBroadcast.java
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION =
"com.unitedcoders.android.broadcasttest.SHOWTOAST";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
sendBroadcast(broadcast);
}
}
Toast Display Activity is ToastDisplay.java
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received",
Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
}
and manifest.xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broad"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".SendBroadcast"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ToastReceiver" >
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" />
</intent-filter>
</receiver>
</application>
</manifest>
There can be two types of broacast: static and dynamic. Static are those that are declared in the manifest file. Dynamic can be declared during runtime. You combined these two types of broadcast in one broadcast.
To declare a simple dynamic broadcast you can use the following code (that is based on your code). It will simply display toast message when activity is shown.
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class BroadcastTestActivity extends Activity {
public static String BROADCAST_ACTION =
"com.unitedcoders.android.broadcasttest.SHOWTOAST";
BroadcastReceiver br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.w("Check", "Inside On Receiver");
Toast.makeText(getApplicationContext(), "received",
Toast.LENGTH_SHORT).show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
filter.addAction(BROADCAST_ACTION);
filter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(br, filter);
}
#Override
protected void onResume() {
super.onResume();
sendBroadcast();
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(br);
}
public void sendBroadcast() {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
broadcast.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(broadcast);
}
}
So now instead of showing toast you can call your new activity. The following actions depend on your needs (what you want to do).
Where is ToastReceiver class?
<receiver android:name=".ToastReceiver">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>`
Change
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
}
to
public class ToastReceiver extends BroadcastReceiver {
}
Try
<activity android:name=".SendBroadcast" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BROADCAST" />
</intent-filter>
</activity>
In onCreate() call
sendBroadcast(v);
Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
String RESULT = new TestAsyncTask().execute(" ").get();
System.out.println("RESULT "+RESULT);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});