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.
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>
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
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 have the app which converts speech to text.But i want the behavior to be like of Google assist app i.e i want my app to be invoked when there is trigger for Default voice assist app.Currently my app is not being listed in Assist app under Assist & voice input options.
Kindly help me out what changes can be made in existing below code so that it gets listed in the Assist app option and the phone takes it as default app.
Thanks in advance.
//mainactivity.java
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
// import android.support.v7.app.ActionBarActivity;
public class MainActivity extends Activity {
private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
// hide the action bar
//getActionBar().hide();
// getSupportActionBar().hide();
promptSpeechInput();
/* btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});*/
}
/**
* Showing google speech input dialog
* */
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
/**
* Receiving speech input
* */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gda6kor.speech3">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.BIND_VOICE_INTERACTION"></uses-permission>
<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.ASSIST"></action>
<action android:name="android.intent.action.VOICE_COMMAND"></action>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Try adding those line to the intent filter for the activity:
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT" />
This is my main file, which has image buttons, and it gives me an exception. When I click on courses image button, it just closes the application.
It works for rest of buttons (rest activities just consist a text view and button) whereas in courses view, I added 3 more buttons (before I added these 3 buttons, it used to work for switching between main and courses).
What i am trying to do is this:
Make 3 buttons in courses activity. They will navigate me to other views like "higher education" or "further education". In next views I will create a list which will display name of courses (like "CCNA" or "Health diploma"!). When the user clicks on course, it will display a picture and some text information about the course.
package com.NVT.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class Main extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton CoursesButton = (ImageButton)findViewById(R.id.CoursesButton);
ImageButton ILoveNescotButton = (ImageButton)findViewById(R.id.ILoveNescotButton);
ImageButton CampusMapButton = (ImageButton)findViewById(R.id.CampusMapButton);
ImageButton GettingHereButton = (ImageButton)findViewById(R.id.GettingHereButton);
CoursesButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Courses.class);
startActivityForResult(myIntent, 0);
}
});
ILoveNescotButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), ILoveNescot.class);
startActivityForResult(myIntent, 0);
}
});
CampusMapButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), CampusMap.class);
startActivityForResult(myIntent, 0);
}
});
GettingHereButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), GettingHere.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Code for Courses View "Courses.java" is given below,,,,
package com.NVT.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Courses extends Activity
{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.courses);
Button ButtonFurtherEducation = (Button)findViewById(R.id.Button_Courses_FurtherEducation);
Button ButtonHigherEducation = (Button)findViewById(R.id.Button_Courses_HigherEducation);
Button ButtonEmployernTraining = (Button)findViewById(R.id.Button_Courses_EmployersnTraining);
Button next = (Button) findViewById(R.id.Button01);
ButtonFurtherEducation.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), FurtherEducationCourses.class);
startActivityForResult(myIntent, 0);
}
});
ButtonHigherEducation.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), HigherEducationCourses.class);
startActivityForResult(myIntent, 0);
}
});
ButtonEmployernTraining.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), EmployersTrainingCourses.class);
startActivityForResult(myIntent, 0);
}
});
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
and code for the further Education Activity is given below,,,,
package com.NVT.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FurtherEducationCourses extends Activity
{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.further_education);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
my Manifest coding as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.NVT.android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Main"
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=".Courses">
</activity>
<activity android:name=".CampusMap">
</activity>
<activity android:name=".GettingHere">
</activity>
<activity android:name=".ILoveNescot">
</activity>
<activity android:name=".FurtherEducationCourses">
</activity>
<activity android:name=".HigherEducationCourses">
</activity>
<activity android:name=".EmployersTrainingCourses">
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
You will see in the logcat screen that the first line with class name from your application is 'Courses.java'. So, the error occurs there. In the logcat you will see NullPointerException at Courses.java line 63. In line 63 of Courses.java you have set the onclick listener for the next button. So, I think the next button stay null when this line executes. I think the main error is in line no 21
Button next = (Button) findViewById(R.id.Button01);
I think the id of the next button in the layout file (corses.xml) is not Button01. Please fix this thing then there will be no errors.
Just try it. :)
According to the logcat screenshot, your problem is that Button next is null, since it is a NullPointerException in line 63, which is when you call next.setOnClickListener, which means that R.id.Button01 is not in R.layout.courses, could you verify if this information is correct?