Run time error when run the android application - java

I am self learner of android and i am just trying to learn it with how i can deal with two activities and 3 and so on , for that i just make the sample code , it run fine with the one activity but when i run it for two activities it show me force stop error , i also mention second activity in my manifest.xml
manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.predictor.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.predictor.activity2"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN1" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Here is mainactivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View btn_activity_1 = findViewById(R.id.btn_activity_1);
btn_activity_1.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_activity_1:
Intent k = new Intent(MainActivity.this, activity2.class);
startActivity(k);
//finish();
break;
}
}
#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);
}
}

You should change :
<category android:name="android.intent.category.LAUNCHER" />
as
<category android:name="android.intent.category.DEFAULT" />
in second activity in xml .

I think I've seen your error: you are casting this to OnClickListener, but your class is not implementing the OnClickListener interface. You should have something like:
public class MainActivity extends ActionBarActivity implements OnClickListener {
// Your code for this class
}
It's difficult to say if it's the only problem, you should post your logs.

Related

How to add a loading image in android webview app?

I am totally new to android development basically I am a web developer, I done a Udemy course about converting a wordpress site to android webview app. Which I followed and made a app by just copying the code from files. Now when ever I open the app I see a white screen which sometimes give feeling that app is not working but the app is loading web page. Now I would like to add a loading image or loader any thing.
Here is my code of mainActivity.java
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of
//WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
another file is myAppWebViewClient.java
public class MyAppWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
and xml file is as below AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bikanershop.bikanershop" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change
"com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" in the
lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<uses-permission
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<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>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action
android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to
match your app's package name.
-->
<category android:name="com.bikanershop.bikanershop" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
I am using android studio latest version that is 1.1.0
Use a ProgressDialog, as outlined Here.
For your case, since you need to close the dialog, you should just in-line your functionality from myAppWebViewClient.java.
So your new code would be something like this:
public class MainActivity extends ActionBarActivity {
private ProgressDialog dialog = new ProgressDialog(WebActivity.this);
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
});
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient()); //replaced with code above
// Stop local links and redirects from opening in browser instead of
//WebView
//mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}

Android fatal exception unable to resume activity

Hi everybodi!
I'm new to Android programming and am trying to create a QR code reader app with Zxing. When I start my application in my phone, it gives me this error message. The app is started, after I push the button and then this is thrown... What is the problem?
Here is my code: This is the MainActivity
package com.example;
public class MainActivity extends Activity {
private Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan= (Button)findViewById(R.id.button1);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.i("App","Scan unsuccessful");
}
}
}
#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;
}
}
And this is my AndoridManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:noHistory = "true"
android:name="com.example.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.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>

How to start an IntentService from a WakefulBroadcastReceiver

I have an application, which you should be able to recreate entirely and very easily with the code I'll post in this question. Here's the Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcasttest.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>
<receiver
android:name="com.example.broadcasttest.TestReceiver"
android:label="#string/app_name"
android:enabled="true" >
</receiver>
<intentservice
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>
</application>
</manifest>
As you can see, the contains an activity, a (wakeful) broadcast receiver and an intentservice, all in the same package. The activity gets started at launch, here's the code:
package com.example.broadcasttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBroadcast(new Intent(this, TestReceiver.class));
}
#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);
}
}
This succesfully triggers the onReceive function of TestReceiver.
package com.example.broadcasttest;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class TestReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Intent service = new Intent("com.example.broadcasttest.MonitorService");
Intent service = new Intent(context, MonitorService.class);
startWakefulService(context, service);
}
}
This is where things go wrong though, I placed a breakpoint in the onReceive function and it definitely gets called. However, the MonitorService class never gets reached. I placed a breakpoint in the onHandleEvent function, but it seems like it never gets that far. Here's the code for this class:
package com.example.broadcasttest;
import android.app.IntentService;
import android.content.Intent;
public class MonitorService extends IntentService {
public MonitorService(String name) {
super(name);
}
public MonitorService()
{
super("MonitorService");
}
#Override
protected void onHandleIntent(Intent intent) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
TestReceiver.completeWakefulIntent(intent);
}
}
}
As you can tell from the commented line in the TestReceiver class, I've tried using an implicit intent instead of an explicit one. I've also read this question and tried everything mentioned there. Am I missing something here? I'm running this on an emulator (Nexus7 API L).
Is there anything I'm missing here?
There is no tag as <intentservice> in Application Manifest. IntentService is a subclass of Service, so you need to declare it as service in manifest.
Change
<intentservice
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>
to
<service
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</service>

Search Dialogue not activated when onSearchRequested is called (Android)

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>

Search dialog doesn't show up

I wanted to implement a search dialog in my android project. and I don't understand what I'm missing on showing the search dialog to the user.
Here's the manifest for the Activity that suppose to launch the search:
<activity
android:name=".ArrowsActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".Searchable" />
</activity>
Here's the manifest for the Searchable Activity:
<activity
android:name=".Searchable"
android:launchMode="singleTop"
android:label="#string/title_activity_searchable" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
Here's the searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/search_label">
</searchable>
Searchable class:
public class Searchable extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// doMySearch(query);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_searchable, menu);
return true;
}
}
SO.. how do I pop the search box to the user> what am I missing?
Just call onSearchRequested() somewhere, this will start the search activity declared in your manifest.

Categories