NewActivity not resolving in base class - should/how do I get the base class to recognize it? - eclipse

Using Eclipse on OSX:
I'm trying to start my first new activity from an Intent. The error seems to be here:
Intent newButton = new Intent(v.getContext(),RandomActivity.class);
startActivity(RandomActivity);
RandomActivity isn't getting resolved. I think I have it correct in my Manifest file. I think my imports are correct. Do I need to declare it somewhere in my base Activity? That doesn't seem to work. It's like the code doesn't recognize the existence of the new class, but I can't see where to fix that.
The rest of my code:
package course.examples.UI.Button;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import course.examples.UI.Button.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import course.examples.UI.Button.RandomActivity;
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent newButton = new Intent(v.getContext(),RandomActivity.class);
startActivity(RandomActivity);
^Multiple markers at this line
brandNewButton cannot be resolved to a type
RandomActivity cannot be resolved to a variable
Note: brandNewButton WAS a class, but I deleted it, then went File->New->Class to create RandomActivity.java.
}
});
}
}
My XML code is below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button
android:id="#+id/button"
android:text="Press Me!"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
>
</Button>
</RelativeLayout>
And here's my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.examples.UI.Button"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<application
android:allowBackup="false"
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".ButtonActivity"
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=".RandomActivity">
</activity>
</application>
</manifest>
Thanks in advance everyone, it's great to have such a community of support here for us noobies.

You don't really need to do :
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent newButton = new Intent(v.getContext(),RandomActivity.class);
Here's the issue: You are trying to start activity with different intent name ??
"startActivity(RandomActivity);" // Wrong intent name
Check your intent!
Instead, just try using this :
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent newButton = new Intent(ButtonActivity.this, RandomActivity.class);
startActivity(newButtom);//This is right
Also.. if your RandomActivity is not from your same package, register it with full path like:
<activity android:name="your packagename where random activity is.RandomActivity" />
Hope this helps.

Related

How to change widget view in background service

I have created widget. I am trying to update the widget in background each 5 seconds (For testing purpose 5 second). But it is not getting update.Is any mistake on my code.
Provider class
package com.widget.widgetapplication;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.util.Random;
public class MyWidget extends AppWidgetProvider {
private int randomNumber = 0;
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "Receive", Toast.LENGTH_SHORT).show();
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Toast.makeText(context, "Update", Toast.LENGTH_SHORT).show();
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// create some random data
randomNumber = new Random().nextInt(100);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(randomNumber));
// Set the text
remoteViews.setTextViewText(R.id.time, String.valueOf(randomNumber));
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.time);
// Register an onClickListener
Intent intent = new Intent(context, MyWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.watch, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
widget info xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="100dp"
android:minWidth="100dp"
android:previewImage="#drawable/analog_watch"
android:initialLayout="#layout/widget_layout"
android:updatePeriodMillis="5000">
</appwidget-provide
Registered receiver in manifest
<receiver android:name=".MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget_info"/>
</receiver>
Widget layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<AnalogClock
android:id="#+id/watch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textStyle="bold"
android:text="Time is : "
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"/>
</LinearLayout>
YES. you are doing it wrong.
the minimum available value for updatePeriodMillis is 30min (1800000 millis).
official document :
Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.

App crashes saying 'Unfortunately, app has stopped' while using asmack

Here is my code for connecting to OpenFire through aSmack API. I am getting an error saying 'unfortunately app has stopped' when trying to run the app on my phone. When i remove the code of aSmack, the app is working fine with creating one button. I also have added the jars into the classpath. Please help me.
package com.example.demo;
import java.io.File;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
String username = "swapnil", password = "swapnil", host = "192.168.0.4", service = "mirana";
int port = 5222;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
final EditText et =(EditText)findViewById(R.id.editText1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// et.setText("Test");
XMPPConnection xmpp = null;
ConnectionConfiguration xmppConfig = new ConnectionConfiguration("192.168.0.4", 5222,"mirana");
xmppConfig.setSASLAuthenticationEnabled(true);
xmppConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
if (xmppConfig == null)
throw new NullPointerException("XMPPService must be configured before it can connect!");
try {
if (xmpp == null) {
xmpp = new XMPPConnection(xmppConfig);
}
xmpp.connect();
} catch (XMPPException ex) {
et.setText("ERROR !");
}
}
});
}
#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;
}
}
And here is the manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.demo.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>
Without log, after watching your code,what I can guess is that, you are running smack methods on Main Thread.. The exception should be of NetworkOnMainThreadException.
You should do the task, in your button click, on background threads. You might want to create AsyncTask for this.
Also, you dont have Internet permission. So please fix that too..
While implementing Smack, you need to take care of maintaining sessions of Smack Connections and related ones.. For this, you can refer the following the GitHub demo app: Smack Android Demo
You have to add Internet permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
And you need to add below code in onCreate() method.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Call an activity using Alarm Manager & Broadcast Receiver

I'm trying to create an app that automatically make phone calls after regular intervals to a specified number. I'm using Alarm Manager and Broadcast Receiver for this purpose. Alarm Manager can't initiate PHONE CALL activity and application terminates giving an error.
Here's my code. I am new in this Dev this.
*MainActivity.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView label;
private EditText phoneNum;
private PendingIntent pendingIntent;
private AlarmManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label=(TextView)findViewById(R.id.entertextlabel);
phoneNum=(EditText)findViewById(R.id.phonenofield);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
//pendingIntent = PendingIntent.getActivity(this,1,alarmIntent,0);
}
public void startAlarm(View view) {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancelAlarm(View view) {
if (manager != null) {
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
}
*AlarmReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
context.startActivity(in);
}
}
I'm getting error "Call requires user permission...."
at
context.startActivity(in);
*mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter the Mobile number"
android:id="#+id/entertextlabel"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Alarm"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/callBtn"
android:layout_marginBottom="81dp"
android:onClick="startAlarm"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel Alarm"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/callBtn"
android:onClick="cancelAlarm"/>
</RelativeLayout>
*Mantifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saud.nexustelecom" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The issue is resolved.
I used the Flag "FLAG_ACTIVITY_NEW_TASK" with with the intent in AlarmReceiver class.
New code is as follows
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
// Intent in = new Intent(Intent.ACTION_CALL);
//in.setData(Uri.parse("tel:03324310929"));
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
}

Opening Activity Crashes App

Hey guys I'm new to coding and iv'e recently been working on an app. I have set OnClickListener's to my buttons each leading to another class that I believe I stated in the Manifest. When I launch my app one of the buttons works but the other crashes the app and tells me that the class is not found please help. Here is some code:
public class CharPage extends ActionBarActivity {
private static Button button_next;
private static Button button_kk;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_char_page);
OnClickButtonListener();
}
public void OnClickButtonListener() {
{
button_kk= (Button) findViewById(R.id.buttonkk);
button_kk.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.jmac.mortalkombat.Kotal_Khan");
startActivity(intent);
}
}
);
}
button_next = (Button) findViewById(R.id.button);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.jmac.mortalkombat.CharPage2");
startActivity(intent);
}
}
);
}
Then there is my Manifest:
<activity
android:name=".CharPage"
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=".CharPage2"
android:label="#string/title_activity_char_page2" >
<intent-filter>
<action android:name="com.example.jmac.mortalkombat.CharPage2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".CharPage3"
android:label="#string/title_activity_char_page3" >
<intent-filter>
<action android:name="com.example.jmac.mortalkombat.CharPage3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Kotal_Khan"
android:label="#string/title_activity_kotal__khan" >
<intent-filter>
<action android:name="com.example.jmac.mortalkombat.Kotal_Khan" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
The StackTrace:
26010-26010/com.example.jmac.mortalkombat E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jmac.mortalkombat, PID: 26010
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.jmac.mortalkombat.Kotal_Khan }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1801)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1499)
at android.app.Activity.startActivityForResult(Activity.java:3942)
at android.app.Activity.startActivityForResult(Activity.java:3889)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:4213)
at android.app.Activity.startActivity(Activity.java:4181)
at com.example.jmac.mortalkombat.CharPage$1.onClick(CharPage.java:37)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20926)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
First make sure you have an Activity in your project that has this name "Kotal_Khan" and is under the package "com.example.jmac.mortalkombat" since this is what you declared in your manifest.
Second Replace.
Intent intent = new Intent("com.example.jmac.mortalkombat.Kotal_Khan");
for
Intent intent = new Intent(getApplicationContext(), YOURCLASSNAME.class);
i assume will be Kotal_Khan.class.
Good Luck.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_char_page);
button_kk= (Button) findViewById(R.id.buttonkk);
button_next = (Button) findViewById(R.id.button);
OnClickButtonListener();
}
public void OnClickButtonListener() {
{
button_kk.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.jmac.mortalkombat.Kotal_Khan");
startActivity(intent);
}
}
);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.jmac.mortalkombat.CharPage2");
startActivity(intent);
}
}
);
}

PayPal Error - Please make sure all fields have been entered

I have a problem in creating a working PayPal button in Sandbox environment.
After entering my email and password in the sandbox environment.
This is what I see.
Here are my codes
AndroidManifest.xml
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MypaypalActivity"
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.paypal.android.MEP.PayPalActivity"
android:configChanges="keyboardHidden|orientation"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>
MypaypalActivity
public class MypaypalActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout MainLayout= new LinearLayout(this);
setContentView(R.layout.main);
PayPal pp = PayPal.initWithAppID(this, "APP-80W284485P519543T", PayPal.ENV_SANDBOX);
LinearLayout layoutSimplePayment = new LinearLayout(this);
layoutSimplePayment.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layoutSimplePayment.setOrientation(LinearLayout.VERTICAL);
CheckoutButton COButton = pp.getCheckoutButton(this, PayPal.BUTTON_118x24, CheckoutButton.TEXT_PAY);
COButton.setOnClickListener(this);
layoutSimplePayment.addView(COButton);
MainLayout.addView(layoutSimplePayment);
setContentView(MainLayout);
}
public void onClick(View v) {
PayPalPayment payment = new PayPalPayment();
payment.setSubtotal(new BigDecimal("10"));
payment.setCurrencyType("USD");
payment.setRecipient("becozofeuu_92#hotmail.com");
// payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
Intent checkoutIntent = PayPal.getInstance().checkout(payment, this);
startActivityForResult(checkoutIntent, 1);
}
I faced same problem, finally I solve it using this code to
pass integer value to Bigdecimal
payment.setSubtotal(new BigDecimal("10"));
instead of using:
payment.setSubtotal(new BigDecimal(10));
e.g.:
public void onClick(View v) {
PayPalPayment payment = new PayPalPayment();
payment.setSubtotal(new BigDecimal("10"));
payment.setCurrencyType("USD");
payment.setRecipient("becozofeuu_92#hotmail.com");
// payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
Intent checkoutIntent = PayPal.getInstance().checkout(payment, this);
startActivityForResult(checkoutIntent, 1);
}