code below, don't know whats the problem
package and.views;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class androidView extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0,0, 0, "AutoComplete");
menu.add(0,1, 1, "Button");
menu.add(0,2, 2, "CheckBox");
menu.add(0,3, 3, "EditText");
menu.add(0,4, 4, "RadioGroup");
menu.add(0,5, 5, "Spinner");
return true;
}
/** Override onOptionsItemSelected to execute code for each menu item */
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case 0:
showAutoComplete();
return true;
case 1:
return true;
case 2:
return true;
case 3:
return true;
case 4:
return true;
case 5:
return true;
}
return true;
}
public void showAutoComplete()
{
Intent autocomplete = new Intent(this, AutoComplete.class);
try{
this.startActivity(autocomplete);
}
catch(Exception e)
{
System.out.print(" activity not found");
}
}
}
2nd class
package and.views;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
public class AutoComplete extends Activity{
public void onCreate(Bundle icircle) {
super.onCreate(icircle);
setContentView(R.layout.autocomplete);
ArrayAdapter<String> monthArray=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Months);
final AutoCompleteTextView textView= (AutoCompleteTextView)findViewById(R.id.testAutoComplete);
textView.setAdapter(monthArray);
final Button changeButton=(Button)findViewById(R.id.testAutoComplete);
changeButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
changeOption(textView);
}
});
final Button changeButton2 = (Button) findViewById(R.id.textColorButton);
changeButton2.setOnClickListener(new Button.OnClickListener()
{ public void onClick(View v)
{ changeOption2(textView);
}
});
}
static final String[]Months= new String[]{ "January","February","March","April","May","June","July","August", "September","October","November","December" };
public void changeOption(AutoCompleteTextView text)
{
if (text.getHeight()==100){ text.setHeight(30);
}
else
{
text.setHeight(100);
}
} public void changeOption2(AutoCompleteTextView text)
{
text.setTextColor(Color.RED);
}
}
Manifest file
enter code here<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="and.views"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".androidView"
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=".AutoComplete" android:label="AutoComplete" android:launchMode="standard" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chetan, I'm not quite sure what the exact problem you are having here, but I'm guessing you get a nullPointerException when you try to launch a new Activity? I didn't take an extensive look at your code but I noticed you don't have any of the extra Activities in your Manifest. Anytime you create a new Activity to be launched you need to add it to the Manifest. I'm pretty new to Android so I'm not too sure why all this is, but I came across that problem before as well.
Not too sure if the answer came too late for you.
Not much information about simple_list_item_1 online I can find, but if your problem is with the AutoCompleteTextView context popup not showing any of your prefilled autocomplete text, a change to simple_dropdown_item_1line should solve the problem
Note: I'm testing on API8
Related
I am starting the activity after clicking on menu item in the action bar menu.
I have added the activity in manifest file also . I cannot find why there is ActivityNotFoundException. I have used intent to start new Activity. FragmentActivity extends Activity.
MANIFEST FILE:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.myapplication">
<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.AppCompat.Light.NoActionBar">
<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="com.example.android.myapplication.FragmentActivity"
android:label="#string/app_name"></activity>
</application>
</manifest>
MAIN ACTIVITY FILE:
package com.example.android.myapplication;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatCallback;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements AppCompatCallback{
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
private EditText mEditTextName;
private EditText mEditTextEmail;
private EditText mEditTextPhone;
private Context context;
Toast mToast;
Intent mIntent;
// private String mPreferenceFileName = getString(R.string.preference_file_key);
// private String mNameKey = this.getString(R.string.name_key);
// private String mPhoneKey = this.getString(R.string.phone_key);
//private String mEmailKey = this.getString(R.string.email_key);
#Override
public void onSupportActionModeStarted(ActionMode mode) {}
#Override
public void onSupportActionModeFinished(ActionMode mode) {}
#Nullable
#Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
private AppCompatDelegate delegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
delegate = AppCompatDelegate.create(this, this);
delegate.installViewFactory();
super.onCreate(savedInstanceState);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar) findViewById(R.id.app_bar);
delegate.setSupportActionBar(toolbar);
mSharedPreferences = this.getSharedPreferences("myFile" , Context.MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
mEditTextName = (EditText)findViewById(R.id.name);
mEditTextPhone = (EditText)findViewById(R.id.phone);
mEditTextEmail= (EditText)findViewById(R.id.email);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.save:
mEditor.putString( "mNameKey",mEditTextName.getText().toString());
mEditor.putString( "mEmailKey",mEditTextPhone.getText().toString());
mEditor.putString( "mPhoneKey",mEditTextEmail.getText().toString());
mEditor.apply();
mToast = Toast.makeText(this,"saved",Toast.LENGTH_LONG);
mToast.show();
break;
case R.id.Clear:
mEditTextName.setText("");
mEditTextEmail.setText("");
mEditTextPhone.setText("");
mToast = Toast.makeText(this,"Cleared",Toast.LENGTH_LONG);
mToast.show();
break;
case R.id.retrieve:
if(mSharedPreferences.contains("mNameKey")){
mEditTextName.setText(mSharedPreferences.getString("mNameKey",""));
}
if(mSharedPreferences.contains("mEmailKey")){
mEditTextEmail.setText(mSharedPreferences.getString("mEmailKey",""));
}
if(mSharedPreferences.contains("mPhoneKey")){
mEditTextPhone.setText(mSharedPreferences.getString("mPhoneKey",""));
}
mToast = Toast.makeText(this,"Retrieved",Toast.LENGTH_LONG);
mToast.show();
break;
case R.id.FragementActivity:
// HERE I CALL THE NEW ACTIVITY
try {
mIntent = new Intent(MainActivity.this, FragmentActivity.class);
this.startActivity(mIntent);
}catch(ActivityNotFoundException e){
e.printStackTrace();
Log.i("tag","exception");
}
}
return super.onOptionsItemSelected(item);
}
}
When i click on the FragmentActivity menu item I cannot start the new activity. Is it not possible to start new activity this way? Is there some alternative?
as #Mike M. already mentioned, you are trying to open FragmentActivity from the support package instead of your package, hence the issue
Solution :
You have to remove this import
import android.support.v4.app.FragmentActivity;
and import your activity carefully.
you can use setAction as
mIntent = new Intent();
intent.setAction("com.example.android.myapplication.FragmentActivity");
this.startActivity(mIntent);
Note : For second solution , To avoid the user to select app to complete the action, you need to add DEFAULT as category along with intent-filter for FragmentActivity
No need of changing the class name or no need of removing this :-
import android.support.v4.app.FragmentActivity;
Instead write your full qualified class ( yourPackagename.FragmentActivity )name like
(MainActivity.this,com.example.android.myapplication.FragmentActivity.class );
in the explicit intent call !!
I am trying to read some information via NFC and created the intent-filter to do so, together with the xml that contains the required technologies. The intent-filter should start my ReadActivity, but it doesn't do so, when I put the card near my phone. NFC is activated, so this shouldn't be the problem. I really don't see the problem with my code, so it would be great if someone could take a look at it and maybe give me a hint in the right direction. Here is the code:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.johan.nfcreaderforunicard">
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<uses-sdk android:minSdkVersion="10"/>
<application
android:allowBackup="true"
android:icon="#mipmap/apple"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<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=".ReadActivity">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="#xml/tech"/>
</activity>
</manifest>
ReadActivity:
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;
import java.io.IOException;
public class ReadActivity extends AppCompatActivity {
private final byte[] selectAid = {(byte)90, (byte)95, (byte)-124, (byte)21};
private final byte[] creditPayload = {(byte)108, (byte)1};
private byte[] resultOk;
private byte[] creditBytes;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.read_view);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())){
IsoDep isodep = IsoDep.get((Tag)getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG));
if(isodep != null){
try{
isodep.connect();
resultOk = isodep.transceive(selectAid);
if(resultOk[0] == 0){
creditBytes = isodep.transceive(creditPayload);
}
}catch (IOException e){
}
}
}
float credit = (float)formatCredit(creditBytes);
TextView label = (TextView)findViewById(R.id.read_text);
label.setText("Dein Guthaben: "+String.valueOf(credit)+"€");
}
private double formatCredit(byte[] array) {
double credit = (double)(((0xff & array[4]) << 24) + ((0xff & array[3]) << 16) + ((0xff & array[2]) << 8) + (0xff & array[1])) / 1000D;
return credit;
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem){
switch(menuItem.getItemId()){
case(R.id.action_settings):
Intent startSettings = new Intent(ReadActivity.this, SettingsActivity.class);
startActivity(startSettings);
return true;
case(R.id.about):
Intent startAbout = new Intent(ReadActivity.this, AboutActivity.class);
startActivity(startAbout);
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
#Override
public void onBackPressed(){
Intent backToMain = new Intent(getApplicationContext(), MainActivity.class);
startActivity(backToMain);
finish();
}
}
MainActivity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem){
switch(menuItem.getItemId()){
case(R.id.action_settings):
Intent startSettings = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(startSettings);
return true;
case(R.id.about):
Intent startAbout = new Intent(MainActivity.this, AboutActivity.class);
startActivity(startAbout);
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
#Override
public void onBackPressed(){
finish();
}
}
The AboutActivity and SettingsActivity don't contain anything yet, so I didn't include them in this post. I really don't know where the problem is though.
Restarting my phone and the NFC-setting solved the problem.
I want after a click on the OK button the MenuActivity to be shown. I have already searched on the Internet to find an answer and tried everything. I haave the activity declared in the AndroidManifest and I also tried to use Intent(this, MenuActivity.class) and also the one with the action inside but it doesn't work.
MainActivity:
package com.jamesjohnson.chronos;
import android.app.AlertDialog;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.jamesjohnson.chronos.R;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setTitle("Willkommen");
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch(id) {
case R.id.action_mainmenu:
startActivity(new Intent("com.jamesjohnson.chronos.MenuActivity"));
return true;
case R.id.action_settings:
showMessageBox("Es gibt leider noch keine Einstellungen. Wir arbeiten daran!", true, true);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
try {
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
Context ctx = this;
intent.setClassName(ctx, "com.jamesjohnson.chronos.MenuActivity");
intent.setAction("com.jamesjohnson.chronos.MenuActivity");
if ((intent.getAction() == "com.jamesjohnson.chronos.MenuActivity") || (intent.getClass() != null)) {
MainActivity.this.startActivity(intent);
showMessageBox("Button has been pressed " + intent.toString(), true, true);
}
else {
showMessageBox("Error : Hauptmenü ist nicht erreichbar", true, true);
}
}
catch (ActivityNotFoundException an) {
showMessageBox("Error :" + an.getMessage(), true, true);
}
catch (Exception e) {
showMessageBox("Error :" + e.getMessage(), true, true);
}
}
protected void showMessageBox(String message, boolean showOKbutton, boolean canceable) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(MainActivity.this);
dlgAlert.setMessage(message);
dlgAlert.setTitle("Chronos");
if (showOKbutton) {
dlgAlert.setPositiveButton("OK", null);
}
if (canceable) {
dlgAlert.setCancelable(true);
}
dlgAlert.create().show();
}
}
Here's my AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jamesjohnson.chronos"
android:versionCode="1"
android:versionName="1.0.1
" >
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/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=".MenuActivity"
android:label="#string/title_activity_menu"
android:parentActivityName=".MainActivity" >
<intent-filter>
<action android:name="com.jamesjohnson.chronos.MenuActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.jamesjohnson.chronos.MainActivity" />
</activity>
</application>
</manifest>
And finally here's the MenuActivity:
package com.jamesjohnson.chronos;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MenuActivity extends ListActivity {
private static final String TAG = "MenuActivity";
static final String[] ENTRIES = new String[] {"Kunden", "Projekte", "Leistungen", "Zeiten"};
ListView listView = getListView();
#Override
protected void onCreate(Bundle savedInstanceState) {
showMessageBox("Activity is beeing created", true, true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
this.setTitle("Hauptmenü");
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ENTRIES));
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
showMessageBox("Kunden", true, true);
break;
case 1:
showMessageBox("Projekte", true, true);
break;
case 2:
showMessageBox("Leistungen", true, true);
break;
case 3:
showMessageBox("Zeiten", true, true);
break;
default:
showMessageBox("Error: Undefined index", true, true);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected void showMessageBox(String message, boolean showOKbutton, boolean canceable) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(MenuActivity.this);
dlgAlert.setMessage(message);
dlgAlert.setTitle("Chronos");
if (showOKbutton) {
dlgAlert.setPositiveButton("OK", null);
}
if (canceable) {
dlgAlert.setCancelable(true);
}
dlgAlert.create().show();
}
}
Unfortunately I can't show you my Logcat because it doesn't work on my computer. (I always have to export the APK to test the App).
P.S. I am working with Android Studio 1.0.1
...Please HELP ME !
To open a new activity you simply have to call it like this inside the onClick method.
Intent intent = new Intent(v.getContext(), MenuActivity.class);
startActivity(intent);
So your onClick method will look like this.
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MenuActivity.class);
startActivity(intent);
}
Hope this helps.
Is because you say MainActivity.this, but you aren't in the MainActivity context.
You could make a reference of your current context in onCreate() and save it in a field:
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
context = this;
//rest of your code here
}
and use it as:
Intent intent = new Intent(context, MenuActivity.class);
//Something else
context.startActivity(intent);
Go to your manifest file, you will for your MainActivity manifest:
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
Just copy them and paste them for your MenuActivity.
I had the same same problem than you and it worked for me, but I don't know why.
Good luck!
first make sure your AndroidManifest.xml file contain declaration of all Activities in your app, like
<activity android:name=".MenuActivity"/>
then you create new intent and start it where you need to start the second Activity
Intent intent = new Intent(v.getContext(), MenuActivity.class);
startActivity(intent);
My Main Activity class is as follows
package com.example.barnight2;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView date;
private Button taxiButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
dateAndTime();
}
public void addListenerOnButton() {
taxiButton = (Button) findViewById(R.id.btnTaxi);
taxiButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent taxiIntent = new Intent(MainActivity.this, TaxiActivity.class);
startActivity(taxiIntent);
}
});
}
public void dateAndTime() {
date = (TextView) findViewById(R.id.lblDate);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date day = new Date();
String dayOfTheWeek = sdf.format(day);
date.setText(dayOfTheWeek);
}
}
then i have my second activity called the Taxi Activity
package com.example.barnight2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class TaxiActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taxi);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.taxi, menu);
return true;
}
}
Here is my Manifest.xml i thought i had done everything perfectly but it seems to always crash upon start up of the app
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.barnight2"
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.example.barnight2.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.barnight2.TaxiActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
![image]http://i62.tinypic.com/16rma8.png
ClassCastException
you're casting an object that it's not the same type you're trying to cast, so that's why it throw a RunTimeException with ClassCastException.
It should be:
taxiButton = (ImageButton) findViewById(R.id.btnTaxi);
Since you declared it as ImageButton in xml.
Please remove :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
from your activity com.example.barnight2.TaxiActivity in the AndroidManifest.xml
I am currently working on a fahrenheit to celsius converter android app. I tried runing the app on the emulator and every time it gives "your app has stopped suddenly" error. tried many things like increasing ram of emulator, rebooting . I also added the activity to manifest.xml.
The mainactivity.java code
package com.example.yoyo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Menu;
public class MainActivity extends Activity {
EditText input;
EditText answer;
TextView celsius;
TextView fahrenheit;
Button convert;
#Override
protected void onCreate(Bundle savedInstanceState)
{
this.findAllViewsById();
convert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
String query = input.getText().toString();
answer.setText(query);
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void findAllViewsById()
{
input = (EditText) findViewById(R.id.Input);
answer= (EditText) findViewById(R.id.Answer);
convert= (Button) findViewById(R.id.ConvertButton);
celsius= (TextView) findViewById(R.id.celsius);
fahrenheit= (TextView) findViewById(R.id.fahrenheit);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yoyo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.yoyo.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>
</application>
</manifest>
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.findAllViewsById();
convert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
String query = input.getText().toString();
answer.setText(query);
}
});
}
first of all the super.onCreate has to been your onCreate does. Second thing you can not do all related findViewById operations if you has not called setContentView