I have seen numerous similar questions on stackoverflow but I can not figure out what I am doing wrong. I have made a very simple app to replicate the error.
The app does the following:
it adds a fragment in the MainActivty's onCreate.
user can add another fragment from a dialog.
when the dialog calls the MainActivity's addFragment method an exception is thrown on the commit of the transaction.
Here is the MainActivity
package com.example.testapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.add_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddButtonDialog addButtonDialog = new AddButtonDialog();
addButtonDialog.show(getSupportFragmentManager(), "ADD");
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
CustomFragment fragment = new CustomFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
}
public void addButton() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
CustomFragment fragment = new CustomFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit(); // <-- Exception
}
}
and here is the AddButtonDialog that calls the MainActivity
public class AddButtonDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.new_button_dialog, null);
builder.setView(view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
MainActivity main = new MainActivity();
main.addButton();
}
});
return builder.create();
}
}
And here is the stacktrace
02-14 10:09:27.781: E/AndroidRuntime(26291): FATAL EXCEPTION: main
02-14 10:09:27.781: E/AndroidRuntime(26291): java.lang.IllegalStateException: Activity has been destroyed
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.example.testapp.MainActivity.addButton(MainActivity.java:35)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.example.testapp.AddButtonDialog$1.onClick(AddButtonDialog.java:22)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.os.Looper.loop(Looper.java:137)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.app.ActivityThread.main(ActivityThread.java:5328)
02-14 10:09:27.781: E/AndroidRuntime(26291): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 10:09:27.781: E/AndroidRuntime(26291): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-14 10:09:27.781: E/AndroidRuntime(26291): at dalvik.system.NativeStart.main(Native Method)
I am fairly certain I am not grasping the concepts of the Fragment lifecycle. Please would someone be so kind as to explain what I am doing wrong.
Pass a reference of MainActivity to the Dialog and try this
mainActivity.addButton()
Where mainActivity is the reference
don't:
MainActivity main = new MainActivity();
main.addButton();
the way to start Activities in Android is via startActivity(intent); method
So for example do instead:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
see Intent Guide
If your AddButtonDialog is an inner class then simply make a call to addActivity rather than doing main.addButton().
If not then try creating this method in a separate class and pass FragmentManager as a parameter to it. Thus you can call this from anywhere creating instance of the newly made class.
Also, try using the below in your addButton method,
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
CustomFragment fragment = new CustomFragment();
transaction.replace(R.id.fragment_container, fragment); //this will replace your fragment_container with this fragment
transaction.addToBackStack(null);// this will help you get back to your first fragment on back press
transaction.commit();
If all you want to do is calling a method in your Activity from somewhere else:
1 - create a static variable in your main activity, of type Activity, and instantiate it in onCreate to this, so you have your instance stored.
2 - create a static method in your main activity called get instance, that simply returns an instance of your activity
3 - wherever you are call : MainActivity.getInstance().yourMethod();
Of course it will only work if your activity is existing!
Related
I want to know why my application is throwing a runtime exception when I run it? I used to the code in the following link for my application,
dynamically add and remove view to viewpager.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ab=getActionBar();
pagerAdapter = new MainPagerAdapter();
pager = (ViewPager) findViewById (R.id.pager);
pager.setAdapter (pagerAdapter);
inflater = (LayoutInflater)getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v0 = (FrameLayout) inflater.inflate (R.layout.listlayout, null);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
pagerAdapter.addView (v0, 0);
pagerAdapter.notifyDataSetChanged();
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
{
pagerAdapter.addView (v0);
pagerAdapter.notifyDataSetChanged();
}
}
LogCat:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolistworkable/com.example.todolistworkable.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)Caused by: java.lang.NullPointerException
at com.example.todolistworkable.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
the object on line 43 of your code within your onCreate method is null. Most likely the target of one of your findViewById calls is not returning what you expect it to.
See this for more info.
How to interpret Logcat
I created an overlay "always on top button", which is a service HUD, and I can't start an activity screen from there, it gives the error: "Unfortunately App has stopped". In the beginning, all I used to know if there was any TouchEventwas a toast, and that toast was created, but it was created several times, so I don't know if it gives that error because this code, which is on TouchEvent body , is repeated several times too.
here is my code:
public class HUD extends Service implements OnClickListener, OnTouchListener, OnLongClickListener {
Button mButton;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setId(1);
mButton.setText("Button");
mButton.setClickable(true);
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.OPAQUE);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
#Override
public void onDestroy() {
super.onDestroy();
if(mButton != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
mButton = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getX()<mButton.getWidth() & event.getY()>0)
{
Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show(); //this my toast
Intent i = new Intent(); //this is my new acivity (intent)
i.setClass(HUD.this, screen.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
HUD.this.stopSelf();
}
return false;
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(this,"Click", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
System.exit(1);
return false;
}
}
So my question is, is this code on TouchEvent body being repeated several times? If it is, is that the cause of the error?
log cat:
07-20 22:11:06.962: I/Choreographer(1620): Skipped 52 frames! The application may be doing too much work on its main thread.
07-20 22:11:08.062: D/AndroidRuntime(1620): Shutting down VM
07-20 22:11:08.062: W/dalvikvm(1620): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-20 22:11:08.132: E/AndroidRuntime(1620): FATAL EXCEPTION: main
07-20 22:11:08.132: E/AndroidRuntime(1620): android.app.SuperNotCalledException: Activity {com.example.screenshot/com.example.screenshot.screen} did not call through to super.onCreate()
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.os.Looper.loop(Looper.java:137)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-20 22:11:08.132: E/AndroidRuntime(1620): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 22:11:08.132: E/AndroidRuntime(1620): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 22:11:08.132: E/AndroidRuntime(1620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-20 22:11:08.132: E/AndroidRuntime(1620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-20 22:11:08.132: E/AndroidRuntime(1620): at dalvik.system.NativeStart.main(Native Method)
screen.java:
public class screen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(getApplicationContext(), "Made it", 0).show();
finish();
}
}
See android start activity from service
Intent i= new Intent(getBaseContext(), screen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
You error seems to be inside screen activity. There are many thread which might help to figure out the error that you are getting for the activity:
Error in Android "SuperNotCalledException:Activity did not call through to super.OnCreate()"
android.app.SuperNotCalledException: Activity did not call through to super.onStop()
Update
The error is because you haven't called: super.onCreate(savedInstanceState); in your screen activity's onCreate(). That should be the first thing to be called in onCreate(). Do something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//.... other stuff
}
Hope this helps.
New to Android here. I've made a new Fragments app, and I'm having trouble accessing the database from the Fragment.
Here's the class:
public class ShopListActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
DatabaseHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shoplist);
db = new DatabaseHelper(this); // tried getApplicationContext(), and getBaseContext() as well
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public void onClickAddProduct(View view) {
TextView productName = (TextView) findViewById(R.id.add_product_text);
SQLiteDatabase dbWritable = db.getWritableDatabase(); // <-- crashes here
ContentValues values = new ContentValues();
values.put("Name", (String) productName.getText());
dbWritable.insert("Lists", "Name", values);
}
All the other solutions I found on SO didn't help.
DatabaseHelper is a very basic DB Helper class:
public DatabaseHelper(Context context) {
super(context, dbName, null, dbVersion);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLES);
db.execSQL("INSERT INTO Lists (Name) VALUES('Test')");
}
Here's the Logcat:
07-07 21:03:24.351 9641-9641/com.chas.shopforme E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3606)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17362)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3601)
... 11 more
Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.chas.shopforme.ShopListActivity.onClickAddProduct(ShopListActivity.java:69)
... 14 more
Note: I know there are more questions like this, but their solutions didn't work for me.
try this
db = new DatabaseHelper(this.getApplicationContext());
I tried adding this as a comment to your question, but nasty things happen to code in comments.
Assuming you've tried a) in the comment above, and it didn't work, an example of b) follows:
protected void onCreate(Bundle savedInstanceState) {
...
Handler hand = new Handler();
hand.post(new Runnable() {
public void run() {
dbInit();
}
});
}
public void dbInit() {
db = new DatabaseHelper(this);
}
This allows the fragment manager to finish setting up the fragment before you try to use it. What it is doing is sending a message to itself which is processed when the fragment's message handler loop is running.
I have a contact list that takes the user to a contact edit screen. I am trying to handle closing of the edit activity/fragment (contact edit) depending on which one I am currently using. So if I'm using a fragment, I'd like it to close the fragment after saving or deleting from the database, but the same goes for when I'm in portrait/landscape mode on a small device and running an activity. I have the following code that pops up an error (as I expected) when I'm in twopane mode. Basically, I want the user to see a list of contacts, edit the contacts, then see the updated list of contacts plus or minus their edits.
// Set the save button to check the required fields, save the contact and finish
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (TextUtils.isEmpty(mLastName.getText().toString()) ||
TextUtils.isEmpty(mFirstName.getText().toString())) {
makeToast();
} else {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStackImmediate();
getActivity().finish();
}
}
});
// Set the delete button to delete the contact and close the fragment
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Delete the contactUri created from an ID passed in from contactActivity
getActivity().getContentResolver().delete(contactUri, null, null);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStackImmediate();
getActivity().finish();
}
});
return aView;
With the above code I get a NullPointerException at contactEditFragment.onClick
I guess I need a way to differentiate between an activity and a fragment.
05-05 05:40:57.200: E/AndroidRuntime(1350): FATAL EXCEPTION: main
05-05 05:40:57.200: E/AndroidRuntime(1350): java.lang.NullPointerException
05-05 05:40:57.200: E/AndroidRuntime(1350): at com.somestuff.ContactEditFragment$1.onClick(ContactEditFragment.java:106)
05-05 05:40:57.200: E/AndroidRuntime(1350): at android.view.View.performClick(View.java:4204)
05-05 05:40:57.200: E/AndroidRuntime(1350): at android.view.View$PerformClick.run(View.java:17355)
05-05 05:40:57.200: E/AndroidRuntime(1350): at android.os.Handler.handleCallback(Handler.java:725)
05-05 05:40:57.200: E/AndroidRuntime(1350): at android.os.Handler.dispatchMessage(Handler.java:92)
05-05 05:40:57.200: E/AndroidRuntime(1350): at android.os.Looper.loop(Looper.java:137)
05-05 05:40:57.200: E/AndroidRuntime(1350): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-05 05:40:57.200: E/AndroidRuntime(1350): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 05:40:57.200: E/AndroidRuntime(1350): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 05:40:57.200: E/AndroidRuntime(1350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-05 05:40:57.200: E/AndroidRuntime(1350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-05 05:40:57.200: E/AndroidRuntime(1350): at dalvik.system.NativeStart.main(Native Method)
Here is the onContactSelected:
public void onContactSelected(String id) {
if (mTwoPane)
{
Log.i("SECOND INSIDE OF ONITEMSELECTED TWO PANE MODE !!! IN CONTACTS ACTIVITY - THE ID IS:", id);
// In two-pane mode, show the contact edit view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
// Create new fragment and transaction
Bundle arguments = new Bundle();
arguments.putString(ContactEditFragment.CONTACT_ID, id);
ContactEditFragment contactEditFragment = new ContactEditFragment();
contactEditFragment.setArguments(arguments);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.item_detail_container, contactEditFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
} else {
// In single-pane mode, simply start the detail activity
// for the selected contact ID.
Intent detailIntent = new Intent(this, ContactEditActivity.class);
detailIntent.putExtra(ContactEditFragment.CONTACT_ID, id);
startActivity(detailIntent);
}
I have tried the following code, and so far it works as I am expecting:
// Set the save button to check the required fields, save the contact and finish
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (TextUtils.isEmpty(mLastName.getText().toString()) ||
TextUtils.isEmpty(mFirstName.getText().toString())) {
makeToast();
} else {
if (getActivity().findViewById(R.id.item_detail_container) != null){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStackImmediate();
}
else{getActivity().finish();}
}
}
});
// Set the delete button to delete the contact and close the fragment
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Delete the contactUri created from an ID passed in from contactActivity
getActivity().getContentResolver().delete(contactUri, null, null);
if (getActivity().findViewById(R.id.item_detail_container) != null){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStackImmediate();
}
else{getActivity().finish();}
}
});
return aView;
i have Target.java(it is the main activity) and Layout.java.
When user clicks a button a url opens.
The button click is handled in Layout.java.
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
Target.openURL();
}
});
Target.java has a static function openURL
public static void openURL() {
Uri uri = Uri.parse("http://www.thecinema.in");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
I'd like to know how i can make this code work, since the error im getting now is "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity" . I can understand startActivity will not work within a static function, but please suggest me how i could acheive the objective ..Im a newbie to android.FYI ..Thanks guys
-Updated code :
Layout.java
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
Target iA = new Target();
iA.openURL();
}
});
Target.java
public void openURL() { //removed static keyword
DebugLog.LOGD("Within nDelete event of GO button ");
Uri uri = Uri.parse("http://www.thecinema.in");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Exception thrown:
03-02 10:10:28.351: D/QCAR(6820): Within onClick event of 'GO' button
03-02 10:10:28.351: W/dalvikvm(6820): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
03-02 10:10:28.361: E/AndroidRuntime(6820): FATAL EXCEPTION: main
03-02 10:10:28.361: E/AndroidRuntime(6820): java.lang.NullPointerException
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.app.Activity.startActivityForResult(Activity.java:2817)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.app.Activity.startActivity(Activity.java:2923)
03-02 10:10:28.361: E/AndroidRuntime(6820): at com.ingage.pocs.phonegap.Layout$4.onClick(Layout.java:124)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.view.View.performClick(View.java:2408)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.view.View$PerformClick.run(View.java:8816)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.os.Handler.handleCallback(Handler.java:587)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.os.Looper.loop(Looper.java:123)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.app.ActivityThread.main(ActivityThread.java:4633)
Guys what went wrong in here..
i was able to come up with a solution for this question.
Under Target.java i created openURL() as:
public void openURL(Context context)
{
Intent intent = new Intent("com.ingage.pocs.phonegap.URL");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
I created a new Activity OpenURL.java and associated the intent "com.ingage.pocs.phonegap.URL" with it:
package com.ingage.pocs.phonegap;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class OpenURL extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
DebugLog.LOGD("Within BookTicketsActivity Activity GO button ");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.thecinema.in"));
startActivity(browserIntent);
}
}
Layout.java:
private Context applicationContext;
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
ImageTargets ia =new ImageTargets();
ia.openURL(applicationContext);
iA.openURL();
}
});
The best thing is, it worked :) If you guys come up with a better or more sensible solution , pls do post here.thank you
startActivity is not static and since you call it from Target not Layout it cannot resolve call context.
You'll need to call startActivity from Layout.java or pass reference or Target to Layout class
do it like this
Layout.java:
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
Target.openURL(Layout.this);
}
});
Target.java:
public static void openURL(Context context) {
Uri uri = Uri.parse("http://www.thecinema.in");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
You have
Targets iA = new Targets();
Did you, perhaps, mean to write
Target iA = new Target();
That would throw a NullPointerException because you're asking Java to find a class named Targets when none exists.