errors in main_activity.java file - java

cannot find symbol class intent, cannot find...Activity2.class, cannot find...Activity-main
public class MainActivity extends AppCompatActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.MyButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
NewActivity2.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
all the imports precede the above code like import widget button, etc.

Open your Manifest and check if there are entries like these:
<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=".NewActivity2" />

Overriding method should call super, so it should be:
super.onCreate(savedInstanceState);
and make sure that your NewActivity2 is registered in manifest.

Related

Android: App won't load my splash screen and moves directly to the onboarding screens

I have just gotten into Android app development and have been working on parts of my project in bits and pieces.
I first created my onboarding screens and then made my animated splash screen. However, when I run my application my splash screen does not show and instead loads the onboarding pages first. How can I fix this to make my app first transition through the splash screen before moving on the onboarding screens?
This is my SplashActivity Class code
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_SCREEN = 5000;
// Variables
Animation topAnim;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
// Animations
topAnim = AnimationUtils.loadAnimation(this,R.anim.top_animation);
// Hooks
image = findViewById(R.id.splashScreenLogo);
image.setAnimation(topAnim);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, OnboardingActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_SCREEN);
}
}
This is my OnboardingActivity Class code
public class OnboardingActivity extends AppCompatActivity {
private OnboardingAdapter onboardingAdapter;
private LinearLayout layoutOnboardingIndicators;
private MaterialButton buttonOnboardingAction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onboarding);
// Onboarding Screens
layoutOnboardingIndicators = findViewById(R.id.layoutOnboardingIndicators);
buttonOnboardingAction = findViewById(R.id.buttonOnboardingAction);
setupOnboardingItems();
final ViewPager2 onboardingViewPager = findViewById(R.id.onboardingViewPager);
onboardingViewPager.setAdapter(onboardingAdapter);
setupOnboardingIndicators();
setCurrentOnboardingIndicator(0);
onboardingViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
setCurrentOnboardingIndicator(position);
}
});
buttonOnboardingAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onboardingViewPager.getCurrentItem() + 1 < onboardingAdapter.getItemCount()) {
onboardingViewPager.setCurrentItem(onboardingViewPager.getCurrentItem() + 1);
}
else {
startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
finish();
}
}
});
}
}
Edit - My Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mentalhealthapp">
<application
android:allowBackup="true"
android:icon="#drawable/treen_app_logo"
android:label="treen"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity"></activity>
<activity android:name=".HomeActivity" />
<activity android:name=".SignInActivity" />
<activity android:name=".SignUpActivity" />
<activity android:name=".OnboardingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
What should I change in my Manifest File?
You need to set the Launch activity as SplashActivity in your AndroidManifest.xml file
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

button not opening new activity

I've created 3 buttons. Each should open up different activities. I've tested it on my device and it never opens a new activity. Could it be due to the main.xml file where I've used the onClick feature for the button despite no using it within my main activity.
public class Main extends Activity implements View.OnClickListener{
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {//when the app starts this method is run
super.onCreate(savedInstanceState);
// Set the layout for fragment_layout.xml
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
playButton.setOnClickListener(this);
rulesButton = (Button) findViewById(R.id.button_rules);
rulesButton.setOnClickListener(this);
aboutButton = (Button) findViewById(R.id.button_about);
aboutButton.setOnClickListener(this);
}
public void buttonPlayClick(){
startActivity(new Intent("com.example.will.sata.openGLActivity"));
}
public void buttonRulesClick(){
startActivity(new Intent("com.example.will.sata.DetailsActivity"));
}
public void buttonAboutClick(){
startActivity(new Intent(""));
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button_play:
buttonPlayClick();
break;
case R.id.button_about:
buttonAboutClick();
break;
case R.id.button_rules:
buttonRulesClick();
break;
}
}
}
AndroidManifest.xml
<activity
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.will.sata.DetailsActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.will.sata.openGLActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
main.layout.xml
android:id="#+id/button_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="PlayGame"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
style="?android:attr/borderlessButtonStyle"
android:focusable="true" />
I have some corrections of your code, I hope it helps:
In main_layout.xml you don't need android:onClick="playGame" because you are using View.OnClickListener in the main class.
You can also use the android:onClick="playGame", but your code in the Main.class would be like this:
public class Main extends Activity {
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
rulesButton = (Button) findViewById(R.id.button_rules);
aboutButton = (Button) findViewById(R.id.button_about);
}
public void playGame(View v) {
switch (v.getId())
{
case R.id.button_play:
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
break;
case R.id.button_rules:
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
break;
}
}
To register a new activity in AndroidManifest.xml you just need to do this
<activity android:name="com.example.will.sata.OpenGLActivity"/>
<activity android:name="com.example.will.sata.DetailsActivity"/>`
To start a new activity from Main.class you have to do this (It is really important to register the Activity first in the AndroidManifest.xml):
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
Tip: be careful with the naming conventions

No Activity Found to handle Intent in the class file

I am pretty new to Android. I am learning about passing Intents within activities. Here is my code to pass Intent between 2 activties.
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.btOk);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText etName = (EditText)findViewById(R.id.etName);
String Data = etName.getText().toString();
Intent i = new Intent("com.adhish.passingintentdata.layout2");
Bundle extras = new Bundle();
extras.putString("Name", Data);
i.putExtras(extras);
startActivityForResult(i,1);
}
});
}
layout2.java
public class layout2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout2);
String myName = null;
Bundle extras = getIntent().getExtras();
if(extras != null)
{
myName = extras.getString("Name");
}
TextView tvData = (TextView)findViewById(R.id.tvData);
tvData.setText(myName);
}
}
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/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=".layout2"
android:label="#string/title_activity_layout2"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.adhish.passingintentdata.MainActivity" />
</activity>
When i run this code and click on the OK button to pass the data, my app crashes with a fatal error.
The error is:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.adhish.passingintentdata.layout2 (has extras) }
Please give me a detailed help about working with this issue, because I am new to Android.
Thanks.
Use com.adhish.passingintentdata.MainActivity as Action string for creating Intent to launch layout2 Activity :
Intent i = new Intent("com.adhish.passingintentdata.layout2");
and in Manifast add intent-filter for layout2 Activity:
<intent-filter>
<action android:name="com.adhish.passingintentdata.layout2" />
</intent-filter>
Use this intent instead:
Intent i = new Intent(MainActivity.this, layout2.class);

How to get results from search result activity?

I have an Activity (MainActivity) with a search button in the action bar. It searches by some input string and shows the results in a ListView in another Activity (SearchResultsActivity). The user might click in any result to select it.
I want to return the value selected by the user to the main activity, but it's not working. I looked in the documentation but I didn't find anything related.
I tried to use setResult(Intent) in the results activity but the onActivityResult() from the main activity never gets called.
What am I doing wrong? How can I do it?
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="foo.bar" >
<!-- ... -->
<uses-sdk android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Search results activity -->
<activity android:name=".SearchResultsActivity"
android:parentActivityName=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
</manifest>
Menu.xml:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity">
<!-- Search -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<!-- ... -->
</menu>
MainActivity:
public class MainActivity extends FragmentActivity {
// ...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.activity_main_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// never gets called!
super.onActivityResult(requestCode, resultCode, data);
}
// ...
}
SearchResultsActivity:
public class SearchResultsActivity extends Activity {
private ListView listResults;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
listResults = (ListView) findViewById(R.id.listResults);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
final Serializable[] results = find(query);
listResults.setAdapter(new ArrayAdapter<BusLine>(this, R.layout.list_view, busLines));
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
setResult(RESULT_OK, new Intent().putExtra("result", selectedResult));
finish();
}
});
}
}
}
I can understand why you might think that you would use setResult and onActivityResult to handle clicks in your search results activity, but that is not how it works. Only when you launch an activity with startActivityForResult do these functions apply -- not when displaying search results.
The solution is to call startActivity on your MainActivity in the normal fashion, that is, not using activity results. Something like this:
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
Intent intent = new Intent();
intent.putExtra("result", selectedResult);
intent.setClass(SearchResultsActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
Now you will also need to capture this search result in your MainActivity, although you need to make sure you handle the normal case as well as the search results case:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent.hasExtra("result")) {
// Launched from search results
Serializable selectedResult = (Serializable) intent.getSerializableExtra("result");
...
}
...
}

Unable to instantiate activity ComponentInfo… java.lang.NullPointerException

OK, so the problem I'm having occurs when the startGame button is pressed. The app crashes when the button is pressed. The activity is instantiated in the manifest so I'm not sure where the error is. The code for the intent is a copy of the other one (which works) so I have no clue where I went wrong.
Error Log:
02-25 14:46:51.064: E/AndroidRuntime(1261): FATAL EXCEPTION: main
02-25 14:46:51.064: E/AndroidRuntime(1261): Process: com.example.hegemony, PID: 1261
02-25 14:46:51.064: E/AndroidRuntime(1261): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hegemony/com.example.hegemony.PlayerTurn}: java.lang.NullPointerException
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hegemony"
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" >
<activity
android:name="com.example.hegemony.SplashScreen"
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.hegemony.StartScreen" >
<intent-filter>
<action android:name="com.example.hegemony.STARTSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.SetupHomeScreen" >
<intent-filter>
<action android:name="com.example.hegemony.SETUPHOMESCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.SetupPlayer"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.hegemony.SETUPPLAYER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.PlayerTurn"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.hegemony.PLAYERTURN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Java Code of sending activity:
public class SetupHomeScreen extends Activity{
private ArrayList<Player> p = GameMaster.players;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup_home_screen);
getActionBar().hide();
updatePlayers();
Button gotoInput = (Button) findViewById(R.id.btnSetupPlayer);
gotoInput.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toInput = new Intent("com.example.hegemony.SETUPPLAYER");
startActivity(toInput);
}
});
Button startGame = (Button) findViewById(R.id.btnStartGame);
startGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toStart = new Intent("com.example.hegemony.PLAYERTURN");
startActivity(toStart);
}
});
}
public void updatePlayers(){
TextView playerList = (TextView) findViewById(R.id.playerList);
String msg = "";
for(int i=0;i < p.size();i++)
msg = msg + "\n - "+p.get(i).getName();
playerList.setText(msg);
if(p.size() >=2){
Button enable = (Button) findViewById(R.id.btnStartGame);
enable.setEnabled(true);
}
}
}
Java code of receiving activity:
public class PlayerTurn extends Activity {
final ActionBar actionBar = getActionBar();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_turn);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
};
}
}
Button startGame = (Button) findViewById(R.id.btnStartGame);
startGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toStart = new Intent(SendingActivity.this, NewActivity.class);
startActivity(toStart);
}
});
I think this is how you should start an intent. Maybe its missing a context or the "activityname" is not the right way to do it. I tried to start an activity the way you are trying and it gave me an error as well, not the same one, but it didn't work.
I'm sorry if the solution I'm trying to provide doesn't work. This is really the first time I'm trying to help someone. I hope it works.
As per the logcat error message, your activity class is failing to instantiate. Instantiation involves the allocation and assignment of any class member variables. In the case of PlayerTurn, the only one is this:
final ActionBar actionBar = getActionBar();
The call to getActionBar() throws a NullPointerException because the activity's window has not been built yet - you should call getActionBar() after setContentView() has been called in onCreate(). You can resolve this by simply moving that line of code into onCreate().
If you still want to retain this as a class member variable, declare it but don't assign it:
ActionBar actionBar;
...and then do the assignment in onCreate():
actionBar = getActionBar();

Categories

Resources