I have a bar code scanner app that has the following layout...
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scanner"
android:id="#+id/scan_barcode1"
android:layout_below="#+id/spinner7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:onClick="openScanner1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/scan_content"
android:layout_below="#+id/scan_barcode1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="#string/serial_number"
android:layout_alignRight="#+id/spinner7"
android:layout_alignEnd="#+id/spinner7" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scanner"
android:id="#+id/scan_barcode2"
android:layout_below="#+id/scan_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="openScanner2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/scan_content2"
android:layout_below="#+id/scan_barcode2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="#string/serial_number"
android:layout_alignRight="#+id/scan_content"
android:layout_alignEnd="#+id/scan_content" />
I want to be able to press scan_barcode1 and recieve the serial number of the barcode in editText scan_content, Then press scan_barcode2 and recieve the serial number of that barcode in editText scan_content2. My app crashes when i press either of the two buttons.
public void openScanner1 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator (this);
scanIntegrator.initiateScan();
startActivityForResult(scanIntegrator, 1);
}
public void openScanner2 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
startActivityForResult(scanIntegrator, 2);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null && requestCode == 1 && resultCode == RESULT_OK ) {
String scanContent = scanningResult.getContents();
contentTxt.setText(scanContent);
}
else if (scanningResult!=null && requestCode == 2 && resultCode == RESULT_OK) {
String scanContent = scanningResult.getContents();
contentTxt2.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
Here is my logcat...
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1817)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1515)
at android.app.Activity.startActivityForResult(Activity.java:4026)
at android.app.Activity.startActivityForResult(Activity.java:3973)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at com.example.gamezcua.etaksytemscop.BeforeBatteryInventory.openScanner1(BeforeBatteryInventory.java:521)
Here is my android manifest.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/etak_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=".EditExistingCloseOutPackage"
android:label="#string/title_activity_edit_existing_close_out_package" >
</activity>
<activity
android:name=".SiteInfo"
android:label="#string/title_activity_site_info" >
</activity>
<activity
android:name=".choice"
android:label="#string/title_activity_choice" >
</activity>
<activity
android:name=".NewOrExisting"
android:label="#string/title_activity_new_or_existing" >
</activity>
<activity
android:name=".NewBatteryInformation"
android:label="#string/title_activity_new_battery_information" >
</activity>
<activity
android:name=".ExistingBatteryInformation"
android:label="#string/title_activity_existing_battery_information" >
</activity>
<activity
android:name=".BatteryInstallationChecklist"
android:label="#string/title_activity_battery_installation_checklist" >
</activity>
<activity
android:name=".FinalInstallationChecklistBattery"
android:label="#string/title_activity_final_installation_checklist_battery" >
</activity>
<activity
android:name=".FinalSiteChecklist"
android:label="#string/title_activity_final_site_checklist" >
</activity>
<activity
android:name=".PhotoGallery"
android:label="#string/title_activity_photo_gallery" >
</activity>
<activity
android:name=".BeforeBatteryInventory"
android:label="#string/title_activity_before_battery_inventory" >
</activity>
<activity
android:name=".CompleteOnScopeWork"
android:label="#string/title_activity_complete_on_scope_work" >
</activity>
<activity
android:name=".JobType"
android:label="#string/title_activity_job_type" >
</activity>
<activity
android:name=".AfterInventory"
android:label="#string/title_activity_after_inventory" >
</activity>
<activity
android:name=".Both"
android:label="#string/title_activity_both" >
</activity>
</application>
updated java file
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult!=null) {
String scanContent = scanningResult.getContents();
editText32.setText(scanContent);
}
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
editText33.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
Related
I'm relatively new to Android Studio, this is my 2nd project. I'm currently having an issue that just recently appeared when when I run my app, when I click on the create_account button on the main menu activity. Its sending me to my ViewlogActivity for some reason. And when I click on my Viewlogs button it does nothing. I double checked my intents and have both activities in my manifest file. Whats going on?
My Code:
Manifest File:
<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" />
s<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <activity-->
<!-- android:name=".CreateAccountActivity"-->
<!-- android:theme="#style/AppTheme.NoActionBar">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- <category android:name="android.intent.category.DEFAULT" />-->
<!-- </intent-filter>-->
<!-- </activity>-->
<activity
android:name=".CreateAccountActivity2"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ViewLogActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".LogoutActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
**Main Activity Buttons:**
Button create_account_button = findViewById(R.id.create_account);
create_account_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the create account activity
Log.d("MainActivity", "onClick for create account called");
Intent intent = new Intent(MainActivity.this, CreateAccountActivity2.class);
startActivity(intent);
}
});
Button login_button = findViewById(R.id.login);
login_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the login Activity
Log.d("Login", "onClick for login activity called");
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
});
Button logout_button = findViewById(R.id.logout);
logout_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the logout Activity
Log.d("Logout", "onClick for logout activity called");
Intent intent = new Intent(MainActivity.this, LogoutActivity.class);
startActivity(intent);
}
});
Button view_logs_button = findViewById(R.id.viewlogs);
create_account_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the view log activity
Log.d("MainActivity", "onClick for view log called");
Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
startActivity(intent);
}
});
Button exit_button = findViewById(R.id.exit);
exit_button.setOnClickListener(new View.OnClickListener(){
// call to exit the application
#Override
public void onClick(View v) {
Log.d("Exit", "onClick for exit called");
finish();
}
});
**Main Activity XML layout File:**
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleX="2"
android:scaleY="2"
android:gravity="center"
android:text="Main Menu"/>
<Button
android:id="#+id/create_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Create Account" />
<Button
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:gravity="center_horizontal"
/>
<Button
android:id="#+id/logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Logout"
android:gravity="center_horizontal"
/>
<Button
android:id="#+id/viewlogs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="View Logs" />
<Button
android:id="#+id/exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit"
android:gravity="center_horizontal"
/>
Issue is here -
you are getting id of view_logs_button but applying click on create_account_button(you apply clicl listener on create_account_button 2 times so its taking latest click listener) and redirecting to ViewLogActivity.
Button view_logs_button = findViewById(R.id.viewlogs);
create_account_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the view log activity
Log.d("MainActivity", "onClick for view log called");
Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
startActivity(intent);
}
});
To fix the issue apply click listener on view_logs_button like below -
Button view_logs_button = findViewById(R.id.viewlogs);
view_logs_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the view log activity
Log.d("MainActivity", "onClick for view log called");
Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
startActivity(intent);
}
});
Hope this will help you!!
I'm making an Android game in Android Studio.
I first wrote the code for game loop. Then I created a a welcome page/start page and added it to the onclick event.
But when I run the app, and click the "start" button, the app stops abruptly. Here is the Java, XML and manifest files. Did I miss something?
My main activity (Game.java):
public class Game extends ActionBarActivity{
private static Button button_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//turn off title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game);
onClickButtonListener();
}
public void onClickButtonListener(){
button_sbm=(Button)findViewById(R.id.button);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent("com.project.androidgame.GAME");
startActivity(intent);
}
}
);
}
/*public void startGame(){
Intent intent=new Intent(Splash.this, Game.class);
startActivity(intent);
}*/
activity_game.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START"
android:textSize="50sp"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How to play"
android:textSize="24sp"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
activity for the starting page (Splash.java):
public class Splash extends Activity {
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle ILoveFootball) {
super.onCreate(ILoveFootball);
setContentView(R.layout.splash);
ourSong=MediaPlayer.create(Splash.this,R.raw.splashsound);
ourSong.start();
Thread timer=new Thread(){
public void run()
{
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint=new Intent("com.project.androidgame.GAME");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
ourSong.release();
finish();
}
}
splash.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_back">
manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:screenOrientation="landscape"
android:name=".Splash" android:label="AndroidGame">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:screenOrientation="landscape"
android:name=".Game" android:label="AndroidGame">
<intent-filter>
<action android:name="com.project.androidgame.GAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Follow this step to edit your code
In your manifest, remove the intent-filter in the second activity, intent-filter is only use to recognize a start up activity. Change this
<activity android:screenOrientation="landscape"
android:name=".Game" android:label="AndroidGame">
<intent-filter>
<action android:name="com.project.androidgame.GAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
To this
<activity android:screenOrientation="landscape"
android:name=".Game" android:label="AndroidGame">
</activity>
The best way to start an activity is the present content to the next class an example
Intent intent=new Intent(Splash.this, Game.class);
startActivity(intent);
I already have tried Cleaning the project and rebuilding it also syncing project with grade files and invalidate caches / restart.
But still I am facing this error.
P.S. I am very new to android programming.
here is my Android Manifest file .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shubhangkhattar.newboston" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.shubhangkhattar.newboston.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.TEXTPLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my MainActivity.java
package com.example.shubhangkhattar.newboston;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
int counter;
Button add,sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button) findViewById(R.id.bAdd);
sub=(Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("Your Total is " + counter );
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("Your Total is" + counter);
}
});
}
}
Here is my activity_main.xml
<LinearLayout 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:orientation="vertical"
>
<TextView
android:text="Your Total Is 0"
android:layout_width="fill_parent"
android:layout_height="wrap_ content"
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"
/>
<Button
android:id="#+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="20dp"
/>
<Button
android:id="#+id/bSub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="20dp"
/>
</LinearLayout>
This is what my error build gradle shows.
:app:processDebugResources
/Users/shubhangkhattar/AndroidStudioProjects/NewBoston/app/src/main/res/layout/activity_main.xml
Error:(11, 32) String types not allowed (at 'layout_height' with value 'wrap_ content').
/Users/shubhangkhattar/AndroidStudioProjects/NewBoston/app/src/main/res/layout/text.xml
Error:(16) No resource identifier found for attribute 'layout_orientation' in package 'android'
Error:Execution failed for task ':app:processDebugResources'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/shubhangkhattar/Library/Android/sdk/build-tools/22.0.1/aapt'' finished with non-zero exit value 1
You should import your R file in MainActivity.java
import com.example.shubhangkhattar.newboston.R;
Try to press alt + Enter on highlighted R symbol.
Create another Class "R"
package com.example.asus.yourname;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int b1=0x7f050001;
public static final int text1=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int app_name1=0x7f040003;
public static final int hello=0x7f040000;
public static final int hello1=0x7f040002;
}
}
I am developing an Android app consisting of two activites so far. The first activity (MainActivity) is started when the app is launched or when a QR code is scanned. The MainActivity starts the second activity (NFCActivity) when the user presses a button. The NFCActivity waits for the user to tap a NFC token, reads out data from the token, and returns the read data to the MainActivity.
This works fine if the app is started manually. If the app is started by scanning a QR code, taping the NFC tag does not invoke the NFCActivity's onNewIntent() method as exepcted, but instead creates a new instance of the NFCActivity on top of the already displayed one.
The enableForegroundDispatch() method is called and FLAG_ACTIVITY_SINGLE_TOP should be set. Relevant source code of a minimal example is provided below. Any help would be highly appreciated!
MainActivity:
public class MainActivity extends Activity {
private EditText dataRead;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
dataRead = (EditText) findViewById(R.id.data);
final Button readKeyButton = (Button) findViewById(R.id.readNFC);
readKeyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent keyIntent = new Intent(MainActivity.this,
NFCActivity.class);
keyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityForResult(keyIntent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == 1) {
String result = intent.getExtras().getString("resultData");
this.dataRead.setText(result);
}
}
}
Main Activity's GUI:
<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" >
<Button
android:id="#+id/readNFC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="127dp"
android:text="Read NFC Tag" />
<EditText
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/readNFC"
android:layout_centerHorizontal="true"
android:layout_marginBottom="85dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
NFCActivity:
public class NFCActivity extends Activity {
private NfcAdapter mAdapter;
private PendingIntent pendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_nfc);
mAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// // Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
IntentFilter td = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
mFilters = new IntentFilter[] { ndef, td };
//
// // Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { NfcV.class.getName(),
NfcF.class.getName(), NfcA.class.getName(),
NfcB.class.getName() } };
}
#Override
public void onResume() {
super.onResume();
if (mAdapter != null)
mAdapter.enableForegroundDispatch(this, pendingIntent, mFilters,
mTechLists);
}
#Override
public void onPause() {
super.onPause();
if (mAdapter != null)
mAdapter.disableForegroundDispatch(this);
}
#Override
public void onNewIntent(Intent intent) {
Log.d("TEST", "onNewIntent() called.");
// READ THE NFC TAG HERE [SKIPPED FOR MINIMAL EXAMPLE]
// Return dummy data for test
Intent result = new Intent();
result.putExtra("resultData", "DUMMY DATA");
setResult(1, result);
finish();
}
}
NFCActivity's GUI:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#303030"
android:paddingLeft="30dp"
android:paddingRight="30dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Tap your NFC tag.."
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF8811"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.nfcqrtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="test.nfcqrtest.MainActivity"
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.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="test.org" android:pathPrefix="/testapp" />
</intent-filter>
</activity>
<activity
android:name=".NFCActivity"
android:windowSoftInputMode="stateHidden" android:screenOrientation="portrait" android:launchMode="singleTop">
</activity>
</application>
</manifest>
In case somebody is facing a similar problem: I was finally able to overcome the issue described above by setting the android:launchMode property for the MainActivity to singleInstance.
hi sir I got this error when my app was launched. When I run my project unfortunately project was stop. This error is coming my log cat. When click on app unfortunately app was stop msg came.
I put one text view on activity_xml. When click on navigate to another page but here do not display my first page. There is problem to on mainactivity.java and activity main.xml
error is java.lang.runtime Exception: Unable to start activity component Info{com.example.b/com.example.b.MainActivity}: java. Lang.NullPointerException
**mainactivity.java**
andheri = (TextView) findViewById(R.id.Andheri1);
andheri.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Context context = null;
// TODO Auto-generated method stub
Intent intent = new Intent(context,andheri.class);
startActivity(intent);
}
});
**activity_xml**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/untitled1"
>
<TextView
android:id="#+id/Andher1"
android:layout_width="80dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FFE4B5"
android:capitalize="characters"
android:gravity="center"
android:text="Andheri"
android:textColor="#000000"
android:textColorHint="#0000CD"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
**mainifest file.**
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.b.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.example.b.andheri"
android:label="#string/app_name" >
</activity>
</application>
You should change this
andheri = (TextView) findViewById(R.id.Andheri1);
to
andheri = (TextView) findViewById(R.id.Andher1);
Your TextView id is id/Andher1 and you're trying to find it as #+id/Andheri1. So you got NPE.
In the intent you send from main activity, you should set the context to this, not null
and change the id of textview , you missed an 'i'