First time app developer and I'm trying to follow the lovely tutorial provided by the nice guys at Google.
Everything was going well until I got to the section on adding an ActionBar.
I tried following the instruction for supporting Android 2.1 and above. I went to the SDK manager, installed v7 appcompat, set up a new library project, imported appcompat, imported appcompat, set up the build paths, configured build paths, yadda yadda. Basically I followed these steps to the letter: http://developer.android.com/tools/support-library/setup.html#libs-with-res
Once that was done I adjusted MainActivity to extend ActionBarActivity and updated my manifest file to use Theme.AppCompat.Light for the theme. Continuing with the tutorial I added an ActionBar with a search and settings button, but upon trying to compile I ran into nothing but errors. Originally my rampant ctrl+shift+O'ing had imported android.R - a problem I found an fixed. However after removing that import statement and going to Project > Clean it isn't generating the R.java file. I've looked through every resource and there are no files with a capital letter in the name. There's also no errors in my XML files, and every reference to #string/<name> exists in the strings.xml file
Any advice on what to try next would be appreciated. I can also post the contents of any file that you suspect may be the culprit. I've spent around 4 hours trying to debug this myself but I don't know where to go next.
For #bluebrain:
Description Resource Location Type
Unable to resolve target 'android-16' android-support-v7-appcompat Uknown Android Target Problem
For #Gem_Ram:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity
android:name="com.example.myfirstapp.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.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
MainActivity.java (Every occurrence of R is giving me "R cannot be resolved to a variable"):
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
//openSearch();
return true;
case R.id.action_settings:
//openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayMessageActivity.java (same error):
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
setupActionBar();
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(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.display_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="action_search">Search</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="title_activity_display_message">My Message</string>
</resources>
Any other files you'd like to see?
Update:
After changing import android.support.v7.app.ActionBarActivity; to import android.support.v7.app.ActionBar; I got a huge collection of red squigglies that I could not resolve since all of the methods I implemented and called are derivatives of the Activity class. I changed it back to import android.support.v7.app.ActionBarActivity; and hit save, then my Problems tab went berserk. First there were 63 problems, then 1, then 7, then 3, then 18... Problems ranging from some process failing with a negative return code, to variables being undefined, to some dependency not existing,... In an attempt to make Eclipse stop having a seizure I clicked Project > Clean, and now the old problem ("Unable to resolve target 'android-16'") is gone, and replaced by 7 instances of the problem "R cannot be resolved to a variable"
Of course this is the problem I expected all along, since my R.java file isn't being generated.
I has the same issue as you mentioned here. I imported library adroid-support-v7-appcompat and error say:
Unable to resolve target 'android-16' android-support-v7-appcompat Uknown Android Target Problem
then I opened project.properties file in android-support-v7-appcompat project and edit line
target=android-19
to
target=android-16
I saved change and make change back to
target=android-19
I saved everything and project was recompiled and all errors disappeared. Now Im able to run project and everything look fine.
Please share your code. I've run this app successfully without any issues. For your reference, adding my files here:
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionbar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.actionbar.HelloWorldMainActivity"
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.actionbar.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.actionbar.HelloWorldMainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.actionbar.HelloWorldMainActivity" />
</activity>
</application>
</manifest>
ActionBarActivity file:
package com.example.actionbar;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
public class ActionBarMainActivity extends Activity {
//public static final String EXTRA_MESSAGE = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world_main);
}
#SuppressWarnings("unused")
private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
}
MainActivity File:
package com.example.actionbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Intent;
public class HelloWorldMainActivity extends Activity {
public static final String EXTRA_MESSAGE = "com.example.actionbar.MESSAGE";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
for ( int i = 0; i < 4; i++ ){
actionBar.addTab(actionBar.newTab().setText("Tab" + (i+1)).setTabListener(tabListener));
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_main, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemsSelected(MenuItem item){
switch (item.getItemId())
{
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
case R.id.action_call:
openCall();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openCall() {
// TODO Auto-generated method stub
}
private void openSettings() {
// TODO Auto-generated method stub
}
private void openSearch() {
// TODO Auto-generated method stub
}
}
Strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActionBar</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="action_search">Search</string>
<string name="action_call">Call</string>
<string name="title_activity_main">ActionBarMainActivity</string>
<string name="title_activity_display_message">DisplayMessageActivity</string>
</resources>
Please update your "import android.support.v7.app.ActionBarActivity;" to
import android.support.v7.app.ActionBar; in your MainActivity.java as you are using the support library APIs
And I hope your AVD is properly configured to min-7 and max-19 as per your manifest description.
In your project.properties file, ensure it has ===> target=android-19
Related
After the splash screen following error displayed. How to solve this?
This error message is displayed after completing the specified time in thread in SplashScr.java
AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lalinda.splashscreen">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScr"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Splash"/>
</application>
</manifest>
SplashScr.java
package com.example.lalinda.splashscreen;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Lalinda on 8/7/2016.
*/
public class SplashScr extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread myThread = new Thread()
{
#Override
public void run()
{
try {
sleep(10000);
Intent startMainScreen = new Intent(getApplicationContext(), Splash.class);
startActivity(startMainScreen);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
Splash.java : This is the second activity
package com.example.lalinda.splashscreen;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
#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_splash, 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);
}
}
Edit : logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lalinda.splashscreen/com.example.lalinda.splashscreen.Splash}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
You need to remove the ActionBar, before setting a toolbar. Set your app theme something like:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Like #Habel Philip mentioned in his comment above, use a Handler to show the Splash screen for a limited time. In fact, I would suggest against using a Splash screen in the first place. This is the recommended pattern now (Launch Screen Pattern): https://www.bignerdranch.com/blog/splash-screens-the-right-way/
I follow this tutorial and i follow all the steps but when i start the app on my device it says it crashed.So i want to make the MainActivity show first and then if i click a Action bar menu, it will load the second activity (upload.java) ant it will show the upload_layout.I'm training making app for android so i'm not good to program ;)
HERE MY CODE:
MainActivity.java
package com.example.tosseapp.app;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.*;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
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;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void tosse1(View v) {
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.toxxe);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse2(View v) {
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tozze);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse3(View v) {
Button two = (Button)this.findViewById(R.id.button3);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossesei);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse4(View v) {
Button two = (Button)this.findViewById(R.id.button4);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.todde);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse5(View v) {
Button two = (Button)this.findViewById(R.id.button5);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossequattro);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse6(View v) {
Button two = (Button)this.findViewById(R.id.button6);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossecinque);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void upload(View view) {
Intent intent = new Intent(this, upload.class);
startActivity(intent);
}
}
upload.java:
package com.example.tosseapp.app;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class upload extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.upload, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tosseapp.app" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity
android:name="com.example.tosseapp.app.upload"
android:label="#string/app_name"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tosseapp.app.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml (menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tosseapp.app.MainActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:onClick="upload"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
You need to declare your MainActivity in your AndroidManifest.xml
<application
android:allowBackup="true"
<!-- other things -->
<activity
android:name="com.example.tosseapp.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- other activities, services, receivers & close application -->
You may need to add both your MainActivity and upload.java into your manifest, something like this:
<application
...
<activity
android:name="com.example.tosseapp.app.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.tosseapp.app.upload"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.tosseapp.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tosseapp.app.MainActivity" />
</activity>
</application>
I am new to Android development in Eclipse, and for some reason, when trying to run my Java code, Eclipse ignores it and only runs the XML code.
Here is my Java code so far:
package com.example.boat;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView mainView = new ScrollView(this);
LinearLayout mainLayout = new LinearLayout(this);
LinearLayout results = new LinearLayout(this);
mainView.addView(mainLayout);
mainLayout.addView(results);
results.setVisibility(View.GONE);
TextView mhs = new TextView(this);
mhs.setText("Maximum hull speed:");
mainLayout.addView(mhs);
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;
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boat"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ser421assignment4.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>
Does anyone know what is wrong here? I prefer to use Java, not XML.
I'm not sure what you think isn't running but the only thing that will happen here is that your activity_main.xml will be inflated because of this line
setContentView(R.layout.activity_main);
All of the Java code before this is just creating Views but not being added to the inflated View with setContentView(). Once you call setContentView() that is what will be displayed. If you want to set those Views on this layout then you need to do it after your call to setContentView() and you have to add them to that layout...not just on each other.
However, in this situation, I see no need to create those dynamically. Just add them to your activity_main.xml and take those out of onCreate().
I am doing a simple test to see if i can make an android game. All im tryihg to do is when the user clicks on my start button it goes into the game screen that for now says "Game screen" however when i click on the button it says the application has stopped Unexpected error has occured and i am forced to shut down.
I am new to eclipse and android but i am not new to java and my code has no errors
So if someone can help me fix this problem i would very much appreciate it
Here is my main:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClickMethod();
}
private void buttonClickMethod() {
// TODO Auto-generated method stub
Button start = (Button) findViewById(R.id.startB);
start.setOnClickListener( new OnClickListener(){
public void onClick(View v){
Intent i = new Intent(v.getContext(), GameView.class);
startActivityForResult(i,0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
here is my gameView code:
package com.example.coloroblind;
import android.app.Activity;
import android.os.Bundle;
public class GameView extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
}
}
thanks for your input
I agree with Simon, whenever you make a new Intent , you have to entry it in your manifest file.
All your intents should be present over here.
I can pretty much guess your problem lies in the Android Manifest. Have you declared your GameView Activity there? It should be <activity android:name=".GameView" /> and here is some sample code where to put it.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameView" />
</application>
i built a code that switch from one activity to another.
when run this code it prints following errors.
Error in an XML file: aborting build.
and
Installation error: INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID
here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wishme.code"
android:versionCode="1"
android:versionName="1.0" android:sharedUserId="#string/app_name">
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".WishMeActivity"
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=".Activity2"></activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
here is activity 1
package wishme.code;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts.Intents;
import android.widget.TextView;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class WishMeActivity extends Activity {
private EditText text;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text=(EditText) findViewById(R.id.editText1);
}
public void myClickhandler(View view)
{
switch(view.getId())
{
case R.id.button1:
RadioButton celsiusbutton=(RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheit=(RadioButton) findViewById(R.id.radio2);
if(text.getText().length()==0)
{
Toast.makeText(this,"Enter a Valid Value",Toast.LENGTH_LONG).show();
return;
}
float input=Float.parseFloat(text.getText().toString());
if(celsiusbutton.isChecked())
{text.setText(String.valueOf(convertFahrenheitToCelsius(input)));
celsiusbutton.setChecked(false);
fahrenheit.setChecked(true);}
else
{
text.setText(String.valueOf(convertCelsiusToFahrenheit(input)));
fahrenheit.setChecked(false);
celsiusbutton.setChecked(true);
}
break;
case R.id.button_nxt:
Intent myintent=new Intent(view.getContext(),Activity2.class);
startActivityForResult(myintent, 0);
break;
}
}
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
and finally this is activity 2
package wishme.code;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity{
public void onCreate(Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
setContentView(R.layout.main2);
Button previous=(Button) findViewById(R.id.button_prev);
}
public void prevclickhandler(View view)
{
Intent intent=new Intent();
setResult(RESULT_OK,intent);
finish();
}
}
please suggest me the solution.
I you just want to go from one activity to another, then you dont need to give the sharedUserID. Its used in cases where you have two different applications that are signed with same certificate and you want to share data between them or you want them to run in the same process.
If you remove it, it should solve your problem.
For information about sharedUserID: http://developer.android.com/guide/topics/manifest/manifest-element.html#uid
This may be due to your sharedUserId value being of a non-permitted value. It has to have the same format as the package structure (e.g. com.android).