Context Menu/Window in WebView inside PopupWindow crashes the app? - android

I have an app with PopupWindow containing WebView that opens Facebook's page, any context menu like: the Autocomplete on text fields in the WebView or even the Long Press that should display options for the user to copy/paste/cut crashes the app right away with the follow error:
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#418cdab0 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:700)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.view.Window$LocalWindowManager.addView(Window.java:554)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1013)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:856)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:820)
at android.webkit.WebViewClassic$PastePopupWindow.show(WebViewClassic.java:971)
at android.webkit.WebViewClassic.showPasteWindow(WebViewClassic.java:7037)
at android.webkit.WebViewClassic.access$10300(WebViewClassic.java:235)
at android.webkit.WebViewClassic$PrivateHandler.handleMessage(WebViewClassic.java:11376)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Notice: my understanding of the problem is that the context menu/window are shown actually in the scope of the app itself not inside the WebView using another PopWindow (Auto generated by the Core WebView Class) this class references the context incorrectly.
My Code is as follows:
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/openpopup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Popup Window" />
</LinearLayout>
Popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/background_light"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#android:color/darker_gray"
android:orientation="vertical" >
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="20dp"
android:orientation="vertical" >
<WebView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/webviewActionView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="200dp"
android:minWidth="200dp"
android:scrollbars="none" >
</WebView>
<Button
android:id="#+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
WebView popupWebview = (WebView) popupView.findViewById(R.id.webviewActionView);
popupWebview.loadUrl("https://m.facebook.com");
Button btnDismiss = (Button) popupView
.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}
});
}
}

As commented above in my answer you can use Custom dialog for your requirement.. i have created a sample PopupDialog.. please check and let me know..if it solve your purpose.. Autocomplete and ContextMenus will work as expected..
MyActivity.java
package com.example.testapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View arg0) {
final PopupDialog popupDialog = new PopupDialog(MyActivity.this);
popupDialog.setContentView(R.layout.popup);
WebView popupWebview = (WebView) popupDialog.findViewById(R.id.webviewActionView);
popupWebview.setWebChromeClient(new WebChromeClient());
popupWebview.setWebViewClient(new WebViewClient());
popupWebview.loadUrl("http://m.facebook.com");
Button btnDismiss = (Button) popupDialog
.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupDialog.dismiss();
}
});
// popupDialog.showAtLocation(50,50);
popupDialog.showAsDropDown(findViewById(R.id.openpopup));
}
});
}
}
PopupDialog.java
package com.example.testapp;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class PopupDialog extends Dialog{
private final Context mContext;
public PopupDialog(Context context) {
super(context);
mContext=context;
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
public void showAtLocation(int x,int y)
{
WindowManager.LayoutParams wmlp = getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = x;
wmlp.y = y;
show();
}
public void showAsDropDown(View view)
{
float density = mContext.getResources().getDisplayMetrics().density;
WindowManager.LayoutParams wmlp = getWindow().getAttributes();
int[] location = new int[2];
view.getLocationInWindow(location);
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = location[0]+(int)(view.getWidth()/density);
wmlp.y = location[1]+(int)(view.getHeight()/density);
show();
}
}
edit: added popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="400dp"
android:minWidth="200dp"
android:background="#android:color/background_light"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#android:color/darker_gray"
android:orientation="vertical" >
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="20dp"
android:orientation="vertical" >
<WebView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/webviewActionView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="200dp"
android:minWidth="200dp"
android:scrollbars="none" >
</WebView>
<Button
android:id="#+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
</LinearLayout>

yes i think you should use your activity name in place of "getBaseContext()". i am just created one demo with your code. its working fine in my pc. and when you are getting this exception?

Nothing wrong in your code..seems to be Android 4.1+ specific issue..
Webview is also using PopupWindow for showing Paste context menu in input box..Nested popupwindows through webview is causing WindowManager crash, while adding new view to it.
I have tried Custom Dialog(in place of Popupwindow) with embedded Webview, Paste context menu is coming fine in Android 4.1+..
If you are not so specific about this paste context menu you can disable it for particular android version using below code..i have tried it.. it will stop app from being crash..
popupWebView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (Build.VERSION.SDK_INT >= 16)
return true;
return false;
}
});
Also you can look at Webview HitTestResult class for displaying some custom made Context menus, according to your needs..

Related

Dynamic CheckBoxes infinitely being added every click on TextView

I am new to Android Studio/Development, but not programming itself. I am struggling with the syntax (as I do think it is my problem) yet I cannot find a solution anywhere on here or on Google. I have this 2nd activity named FilterActivity. FilterActivity currently has 2 TextViews. Both create dynamic CheckBoxes. Right now, I only have one doing this so I can get one right before I go onto another. Here's the issue, I click on the TextView to get the dynamically created CheckBox and it shows perfectly fine. However, clicking on it again just adds the same values until it appears to completely fill the parent via xml.
No matter what I search, what I do, it all does the same exact thing. I know, I have nothing for onCheckedChanged, but I have previously before and it did not work either. So question(s), should I not be using LinearLayout and do like a container of sorts? There will be a decently big database for the checkboxes (500+) so I was thinking I would have to implement ScrollView at some point. Also, if LinearLayout is the correct way to go, what am I doing wrong? I am just completely spinning and I know it should not be this hard.
Thank you to anyone that gives feedback!
Here's XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="xxx.AppEx.FilterActivity"
tools:layout_editor_absoluteY="81dp"
tools:showIn="#layout/activity_filter">
<View
android:id="#+id/firstView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<View
android:id="#+id/secondView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/AllTextView"
android:clickable="true"
android:text="All"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toLeftOf="#id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<TextView
android:id="#+id/DisTextView"
android:clickable="true"
android:text="Dis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toRightOf="#+id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<LinearLayout
android:id="#+id/CheckBoxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/secondView">
</LinearLayout>
</RelativeLayout>
Here's the main code:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.Intent;
public class FilterActivity extends AppCompatActivity implements
CompoundButton.OnCheckedChangeListener {
LinearLayout CheckBoxLayout;
CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
CheckBoxLayout = (LinearLayout) findViewById(R.id.CheckBoxLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView AllTextView = (TextView) findViewById(R.id.AllTextView);
AllTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
AllTextView.setBackgroundColor(Color.BLUE);
onAllClick();
}
});
TextView DisTextView = (TextView) findViewById(R.id.DisTextView);
DisTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//onAllClick();
}
});
}
public void onAllClick()
{
Intent intent = getIntent();
dummy();
}
public void dummy()
{
String[] array = new String[]
{
"Rice", "Beans"
};
LinearLayout CheckBoxLayout = (LinearLayout)findViewById(R.id.CheckBoxLayout);
for (int i = 0; i < array.length; i++)
{
CheckBox checkBox = new CheckBox(this);
checkBox.setText(array[i]);
CheckBoxLayout.addView(checkBox);
}
}
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
}

Create infinite number of EditText on Clicking the button in custom dialog

I have to create a edit text one by one below inside custom dialog while clicking the button.
So far I have tried and created one edit text on clicking the button in custom dialog.
MainActivity.java:
Below I am shown the code what I had tried so far:
Edited:
import android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button button;
private LinearLayout ll;
EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.addBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this);
//setting custom layout to dialog
dialog.setContentView(R.layout.custom_dialog_layout);
dialog.setTitle("Add List");
//adding button click event
Button createEditText = (Button) dialog.findViewById(R.id.button);
createEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// dialog.dismiss();
et= new EditText(MainActivity.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
dialog.addContentView(et,params);
((LinearLayout) dialog.findViewById(R.id.container)).addView(et);
}
});
dialog.show();
}
});
}
}
custom_dialog_layout.xml:
<?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" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Next" />
</LinearLayout>
</RelativeLayout>
I don't know how to create infinite number of edittext one by one below.Anyone can help me with this.Thank you.
The way I've done it before is to have some containing layout (linearlayout, usually) and add my views to that container. Rough code:
LinearLayout container = (LinearLayout) findViewById(R.id.container);
and in your onclick listener
EditText edit = new EditText(MainActivity.this);
container.addView(edit);
Edit
Okay, after managing to absolutely miss the point of your question in my initial post, I went and checked out what was going on. What's happening (I think) is that it IS actually adding more edittexts, but they're all on top of each other so it looks like that it's not. So here's what you have to do, which plays into what I wrote above:
In your custom dialog view, add a linearlayout container
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container"></LinearLayout>
and in your onclick, you add your edit texts to the linearlayout, NOT the dialog view
et= new EditText(TestActivity.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
((LinearLayout) dialog.findViewById(R.id.container)).addView(et);
and that should work.

debug error : confirm perspective switch

when i click on button add debug error
and opens confirm perspective switch dialog box
shows error in dis line " EditText add = (EditText) d1.findViewById(R.id.add); "
what is the mistake in my code??
xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question1" >
</TextView>
<EditText
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<EditText
android:id="#+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<Button
android:id="#+id/registerques"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="registerques" >
</Button>
</LinearLayout>
java class
showing error in editext line
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class Memo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Register Questions");
d1.setContentView(R.layout.memo);
d1.show();
Button view1 = (Button) d1.findViewById(R.id.view);
Button add = (Button) d1.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText add = (EditText) d1.findViewById(R.id.add);
EditText view = (EditText) d1.findViewById(R.id.view);
System.out.println(add.toString());
System.out.println(view.toString());
}
});
view1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), View.class);
startActivity(intent);
}
});
}
}
Probably da error in the code is that you never defined an id "add" - you have #+id/TextView01 in the xml "page" (you mean file - and you must tell us which file) but no #+id/add

EditText and Button unresponsive to touch input inside of TabHost

I'm making an app for a friends band, and am using a cloud storage API called parse. I'm trying to get a string from the cloud and then display it in my edit text, and the idea is to make the text what you want and then upload it to the database. I can sort of do that, but whenever I change the text then flip the phone horizontal then vertical again it removes my edits. In addition it looks like touch has stopped working for that particular layout. I have a TabHost and switching tabs works fine, but whenever I try to click the button I get nothing, nor can I move the cursor in the EditText. I placed breakpoints and toasts inside of my onClick method for my button and nothing's getting fired. I have no idea what's happening. Please help!
Java
package com.example.undaunted.admin;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;
public class UndauntedAdminMain extends Activity {
TabHost tabHost;
Button saveWelcomeText;
EditText welcomeText;
ParseFile file;
ParseObject welcomeTextObject;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.undaunted_main);
welcomeText = (EditText) findViewById(R.id.editText1);
Parse.initialize(this, "REMOVED",
"REMOVED");
initialize();
}
public void initialize() {
tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec = tabHost.newTabSpec("Welcome");
spec.setContent(R.id.welcomeTab);
spec.setIndicator("Welcome");
tabHost.addTab(spec);
TabSpec spec3 = tabHost.newTabSpec("Biographies")
.setContent(R.id.biographyTab).setIndicator("Biographies");
tabHost.addTab(spec3);
// tabHost.newTabSpec("Biographies").setIndicator("Biographies").setContent();
TabSpec spec2 = tabHost.newTabSpec("Gallery");
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.galleryTab);
relativeLayout.setBackgroundColor(Color.BLACK);
spec2.setContent(R.id.galleryTab);
spec2.setIndicator("Gallery");
tabHost.addTab(spec2);
ParseQuery query2;
query2 = new ParseQuery("Welcome");
query2.getInBackground("fdZL8G4PIk", new GetCallback() {
#Override
public void done(ParseObject arg0, ParseException arg1) {
// TODO Auto-generated method stub
if (arg1 == null) {
// welcomeTextObject = arg0;
// welcomeText.setText(welcomeTextObject.getString(
// "WelcomeText").toString());
} else {
welcomeText.setText("Failed to load data");
}
}
});
saveWelcomeText = (Button) findViewById(R.id.saveWelcomeText);
saveWelcomeText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
welcomeTextObject.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException arg0) {
// TODO Auto-generated method stub
welcomeTextObject.put("WelcomeText",
welcomeText.getText());
welcomeTextObject.saveInBackground();
Toast.makeText(getApplicationContext(), "Data sent",
Toast.LENGTH_SHORT).show();
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_undaunted_admin_main, menu);
return true;
}
}
XML
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#+android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/welcomeTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/saveWelcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Save" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
<RelativeLayout
android:id="#+id/galleryTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout
android:id="#+id/biographyTab"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/memberList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/biographyTab"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/showDatesTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/showDateList"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/showDatesTab">
</ListView>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Additional details, I'm using a cloud storage library called Parse. I don't know if anyone's here is familiar with it or if it could be interfering, but I'd really appreciate some help. Thanks!
Remove what's inside initialize() and put the code directly inside your onCreate() method.. I don't know why this is happening but I came across this exact problem and doing this helped me
I fixed it! I had a RelativeLayout that wasn't being used by my TabHost and consequently broke my views. I added the Layout to my TabHost and now everything works!

Runtime Android exception after pressing a button to go to next Activity

I have this code:
package com.problemio;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProblemioActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addProblemButton = (Button)findViewById(R.id.add_problem_button);
Button browseProblemsButton = (Button)findViewById(R.id.browse_problems_button);
Button searchProblemsButton = (Button)findViewById(R.id.search_problems_button);
Button myProblemsButton = (Button)findViewById(R.id.my_problems_button);
addProblemButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(ProblemioActivity.this, AddProblemActivity.class);
ProblemioActivity.this.startActivity(myIntent);
}
});
}
}
It compiles fine and displays the addProblemButton button, but when that button is clicked, the system gives a runtime exception.
Here is the AddProblemActivity class:
package com.problemio;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AddProblemActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TextView text = (TextView) dialog.findViewById(R.id.addProblemText);
//text.setText(R.string.addProblemText);
TextView tv = new TextView(this);
tv.setText("Please Add a Problem");
setContentView(tv);
}
}
and here is the layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First, add the problem you want to solve!"
/>
<TextView
android:id="#+id/add_problem_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add a Problem You Want To See Solved"
/>
<Button
android:id="#+id/add_problem_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add a Problem"
/>
<Button
android:id="#+id/browse_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Browse Problems"
/>
<Button
android:id="#+id/search_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search Problems"
/>
<Button
android:id="#+id/my_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View My Problems"
/>
</LinearLayout>
any idea what might be going wrong? By the way, I can't seem to locate the stack trace of the exception. Where should I look for that in Eclipse? All it currently shows me is AndroidRuntimeException - dalvik.system.NativeStart.main
Thanks!!
The only problem I can think of is that your Activity "AddProblemActivity" is not register in the manifest.
See the logs under LogCat...you will find it in Window >Show View >Android > LogCat

Categories

Resources