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/
Related
I created a scrolling activity but when I debug the app and click on the button to open the activity, I get an error. Basically, it crashes the whole app. However, here's my code below.
Is it because it's a scrolling activity and not an empty activity?
Here is the java file:
news.java
package com.example.it5.foothillers;
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;
public class news extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
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();
}
});
}
}
main_acitivity.java
package com.example.it5.foothillers;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
private void buttonClick() {
startActivity(new Intent("it5.foothillers.news"));
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
buttonClick();
break;
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.it5.foothillers">
<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.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".news">
<intent-filter>
<action android:name="it5.foothillers.news" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Logs:
03-30 12:01:49.871 2122-2122/com.example.it5.foothillers E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.it5.foothillers, PID: 2122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.it5.foothillers/com.example.it5.foothillers.news}: 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.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: 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.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
at com.example.it5.foothillers.news.onCreate(news.java:17)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
This Activity already has an action bar supplied by the window decor.
Your exception message is clearly telling you that you already have a Toolbar as part of the Activity's theme.
You should set a theme like this in the styles.xml file if you want to call setSupportActionBar.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
You could probably also just remove these two lines
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
And replace with
ActionBar toolbar = getSupportActionBar();
Intent intent=new Intent(MainActivity.this,news.class);
startActivity(intent);
Mainifest.xml
<activity android:name="com.example.it5.foothillers.news" ></activity>
The easiest way to start an activity, is change the intent to this:
startActivity(new Intent(MainActivity.this, news.class));
The stack trace shows you're attempting to add another ActionBar and you already had one provided by the application's theme.
So, you can change the theme at yout activity declaration on manifest to not use an ActionBar, like that:
<activity android:name=".news"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="it5.foothillers.news" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Or you just remove the toolbar from your activity code and your activity_news.xml (because you're not using it later):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
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();
}
});
Beginner working on an android app and I'm trying to start a searchable activity when the onSearchRequested method is called after a button push in the main activity. Currently, when the button is pressed, nothing happens.
Here's MainActivity.Java:
package com.example.activity2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onSearchRequested();
}
});
}
/** Called when the user clicks the Send button */
// public void sendMessage(View view)
// {
// }
#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);
}
}
Here's the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity2"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
<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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.FLAG_ACTIVITY_NEW_TASK" />
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.activity2.SearchActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.authorwjf.youtubeapi.MainActivity"
android:label="#string/title_activity_activity2" >
</activity>
<activity android:name=".SearchActivity" >
android:launchMode="singleTop" >
<intent-filter>
<action android:name="com.example.activity2.SEARCHACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".OtherActivity"
android:label="#string/title_activity_other" >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity
android:name=".SearchListView"
android:label="#string/title_activity_search_list_view" >
</activity>
<activity
android:name=".SearchYoutube"
android:label="#string/title_activity_search_youtube" >
</activity>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</application>
</manifest>
SearchActivity.Java
package com.example.activity2;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.api.services.youtube.model.SearchResult;
public class SearchActivity extends Activity {
ListView lst;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_activity);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
ArrayList<SearchResult> results = new ArrayList<SearchResult>();
Iterator<SearchResult> it = results.iterator();
SearchYoutube.prettyPrint(it, query);
lst = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, SearchYoutube.ytstuff);
lst.setAdapter(adapter);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, 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);
}
}
In your searchable.xml file in the xml folder, the label cannot be hardcoded. It should be like #string/name or search dialogue wont show.
You need to implement your search activity.
public class SearchableActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SEARCH", "HERE");
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l, View v, int position, long id) {
// call the appropriate detail activity
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
Toast.makeText(getApplicationContext(), queryStr, Toast.LENGTH_LONG).show();
}
}
And specify the meta-data in your AndroidManifest.xml
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
<activity
android:name=".SearchableActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
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>
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
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>