My code:
package elf.app;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import elf.app.entity.ELFList;
import elf.app.entity.Entry;
import elf.app.test.FakeComm;
// TODO Kunna skicka att något är färdigt (ett rum är städat).
public class RoomListActivity extends ListActivity {
private ELFList eList;
// private FakeComm fakecomm;
private Bundle extras;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.extras = getIntent().getExtras();
eList = new ELFList();
// fakecomm = new FakeComm();
// eList.add(fakecomm.getData());
String[] strArr = {"asd","sdf","dfg"};
eList.add(strArr);
String[] str = eList.returnNames();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, str));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Entry e = eList.getEntry(position);
String roominfo = e.toString();
Intent intent = new Intent(this, RoomInfoActivity.class);
intent.putExtra("entry",roominfo);
this.startActivity(intent);
// old stuff
// String message;
// message = eList.getEntryInfo(position);
// Toast.makeText(getApplicationContext(),
// message, Toast.LENGTH_SHORT).show();
}
});
}
}
I'm getting errors at the following lines:
Intent intent = new Intent(this, RoomInfoActivity.class);
and
this.startActivity(intent);
I don't have much of a clue why I get these errors, the exact output in the editor for these errors are:
"The constructor Intent(new AdapterView.OnItemClickListener(){}, Class ) is undefined"
"The method startActivity(Intent) is undefined for the type new AdapterView.OnItemClickListener(){}"
I'm an Android newbie so please take that into consideration, however I have studied Java for about a year.
Fix
Intent intent = new Intent(this, RoomInfoActivity.class);
to
Intent intent = new Intent(RoomListActivity.this, RoomInfoActivity.class);
The error is because by this you refer to OnClickListener. The problem is fixed, if you refer to the Activity's this. The second error is the same - wrong reference. Just remove this, and startActivity() method will be searched within the enclosing class too.
try this
Intent intent = new Intent(RoomListActivity.this, RoomInfoActivity.class);
intent.putExtra("entry",roominfo);
RoomListActivity.this.startActivity(intent);
Related
I am not sure what I have done but for a moment my code was working smoothly and after I added a new activity the error Attempted to finish an input event but the input event receiver has already been disposed.
I need help on how to fix this.
package proj.com.desperationfinals;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
}
public void validate (View view){
String word = editText.getText().toString();
if(word.contentEquals("Sir Zalameda")) {
AlertDialog.Builder Alert = new AlertDialog.Builder(this);
Alert.setMessage("Correct!")
.create();
Alert.show();
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
}else{
AlertDialog.Builder Alert = new AlertDialog.Builder(this);
Alert.setMessage("Mali!")
.create();
Alert.show();
}
}
}
The problem is in the following section:
AlertDialog.Builder Alert = new AlertDialog.Builder(this);
Alert.setMessage("Correct!")
.create();
Alert.show();
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
You are getting the error because you are trying to show an alert right before starting a new activity. Now, the 'receiver' for the dialog is the current activity and as soon as you start new activity that is switched. To tackle this problem you can try something like this:
Alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
}
TL;DR: Can't show an alert right before starting a new activity.
I am trying to push url using Parse.com and i added a code in which get String is showing error. Can anyone help me out please!
Help me out Please! Getting an Error
The method getString(String) is undefined for the type String**
package com.example.pushnotificationdemo;
import org.json.JSONObject;
import com.example.pushnotificationdemo.R;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.PushService;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
WebView webframe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonData = extras.getString("com.parse.Data");
JSONObject json = new JSONObject(jsonData);
String pushStore = json.getString("data");
if(pushStore!=null) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pushStore.getString("url")));
startActivity(browserIntent);
}
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
/** Cerco l'elemento in /res/layout/main.xml */
webframe = (WebView) findViewById(R.id.webview);
/** Javascript abilitato (ma non flash) */
webframe.getSettings().setJavaScriptEnabled(true);
/** Simulo il webbrowser chrome di android*/
webframe.setWebChromeClient(new WebChromeClient());
webframe.setWebViewClient(new WebViewClient());
/** Assegno l'url di apertura del webframe */
webframe.loadUrl("http://www.dlybugs.com");
}
}
You're getting the error from this code:
pushStore.getString("url")
because pushStore is a String. It's not a JSONObject that you can call getString on. You probably want this code instead:
String pushStore = json.getString("data.url");
if(pushStore!=null) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pushStore));
startActivity(browserIntent);
}
When i try to add the "Define the Remote Input" code to get voice input, it's giving me error in "reply_label" and "EXTRA_VOICE_REPLY" lines (tried to replace them, still error) :
package com.wear.myapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.preview.support.wearable.notifications.RemoteInput;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(this, MainActivity.class);
viewIntent.putExtra("Helllo", "Hello Wear !");
PendingIntent viewPendingIntent =
PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My App")
.setContentText("Hello Wear !")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
// Key for the string that's delivered in the action's intent
private static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
String replyLabel = getResources().getString(R.string.reply_label);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.build();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I again tried to solve it , but not solving, any solution ?
private static final String EXTRA_VOICE_REPLY = "extra_voice_reply"; is a constant declaration and cannot be placed here..
Move it in the line after MainActivity extends Activity, like this:
public class MainActivity extends Activity {
private static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
// ...
You should also really read a basic java tutorial (for basic syntax)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Add several appWidgets with different configuration?
I have a widget which displays a simple textview, which is editable as an edittextfield in a configuration activity. I'm trying to make it possible to add several widgets to the homescreen with different textconfigurations, fx. one widget display: "Buy a lamp.." and another one saying: "I love Bacon".
My problem is that the widgets keeps displaying the same text, and if I edit one widget the other one changes too. I'm using sharedpreferences to save the inputted data for later re-configuration. (I am not using PreferenceActivity)
I've been struggling with this for hours, any help would be much appreciated.
Why doesn't this work:
SharedPreferences sp;
EditText info;
String note;
int appWidgetId;
private void loadPrefs(){
sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
, Context.MODE_PRIVATE);
note = sp.getString("Note", "");
info.setText(note);
}
private void savePrefs(String key, String value){
sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.putString("Note", info.getText().toString());
editor.commit();
}
and then i have a confirm button to end configuration, which calls the savePrefs method
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("Note", info.getText().toString());
Full code configuration activity code:
import java.io.File;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.Spinner;
import android.widget.ToggleButton;
public class WidgetConfig extends Activity implements OnClickListener, OnItemSelectedListener {
AppWidgetManager awm;
int awID;
Context context;
EditText info;
Button b;
String note;
int styleStart = -1, cursorLoc = 0;
int appWidgetId;
SharedPreferences sp;
Spinner spinner;
String[] paths = { "10", "20", "30" };
File path = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
context = WidgetConfig.this;
info = (EditText)findViewById(R.id.etwidgetconfig);
...
b = (Button)findViewById(R.id.bwidgetconfig);
loadPrefs();
b.setOnClickListener(this);
//Getting Info about the widget that launched this activity
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null){
awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID );
}
awm = AppWidgetManager.getInstance(context);
}
...
private void loadPrefs(){
sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
, Context.MODE_PRIVATE);
note = sp.getString("Note", "");
info.setText(note);
}
private void savePrefs(String key, String value){
sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.putString("Note", info.getText().toString());
editor.commit();
}
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("Note", info.getText().toString());
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.tvConfigInput, info.getText());
ComponentName thisWidget = new ComponentName(this, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, views);
Intent in = new Intent(context, WidgetConfig.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.B_EditAgain, pi);
awm.updateAppWidget(awID, views);
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, result);
finish();
}
}
Oh cool, so just set appWidgetId to awID. or even simpler, pass the awID to the methods you are calling sharedPreferences. That should fix it.
I'm diving into Java (this is day 1) and I'm trying to create a button that will trigger a notification when I click it...
This code is based off of the notification documentation here, and UI events documentation here
package com.example.contactwidget;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class ContactWidget extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button calc1 = (Button) findViewById(R.id.calc_button_1);
calc1.setOnClickListener(buttonListener);
setContentView(R.layout.main);
}
private static final int HELLO_ID = 1;
//Error: OnClickListener cannot be resolved to a type
private OnClickListener buttonListener = new OnClickListener() {
public void onClick (View v) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence ticketBrief = "Button Pressed Brief";
CharSequence ticketTitle = "Button pressed";
CharSequence ticketText = "You pressed button 1";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, ticketBrief, when);
Intent notificationIntent = new Intent(this, ContactWidget.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), ticketTitle, ticketText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
}
}
}
I'm running into a problem: OnClickListener cannot be resolved to a type. The problem here is that I don't see any problems with my code in relation to the example I'm using
Add this import:
import android.view.View.OnClickListener;
If you are using Eclipse, you can use Ctrl+Shift+O to make it import those clases or interfaces automagically.
Make sure you have both these imports:
import android.view.View;
import android.view.View.OnClickListener;
setContentView(R.layout.main);
Should be above the button declaration, just below
super.onCreate(savedInstanceState);