[Q] Linking Strings between Activities and WebViews - Android Q&A, Help & Troubleshooting

Hello XDA,
So I've not found anyone properly discussing this. If they do, please link me to the page and don't flame
I've been making an app for my school and so far, I've been doing alright. But now I have run into a problem which seems to be very hard to solve for me alone.
Namely, I have an EditText and a Button in one Activity and a WebView as a different Activity.
I'm trying to get the EditText String into the WebView URL (String being variable, Button initiating the WebView), but get a lot of errors, some not even related to that.
I would really appreciate it if someone could have a look at the code and help me with this
MyWebView.java:
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebView extends Activity {
WebView mWebView;
EditText mEtxtStudentNum;
static String StudentNumFromHome = bas.sie.Antonius.Home.StudentNum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://carmelcollegegouda.nl/site_ant/roosters/standaardroosters/Lee1_" + StudentNumFromHome + ".htm");
}
}
Home.java (Homescreen):
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Home extends Activity {
Button mBtnStSchedule;
static EditText mEtxtStudentNum;
static final String StudentNum = mEtxtStudentNum.getText().toString();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.BtnStSchedule);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MyWebView.class);
startActivityForResult(myIntent, 0);
}
});
// TODO Auto-generated method stub
}
}
AntoniusActivity.java (Main):
Code:
package bas.sie.Antonius;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
public class AntoniusActivity extends TabActivity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, External.class);
spec = tabHost.newTabSpec("external").setIndicator("External",
res.getDrawable(R.drawable.ic_tab_external))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Contact.class);
spec = tabHost.newTabSpec("contact").setIndicator("Contact",
res.getDrawable(R.drawable.ic_tab_contact))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
In AntoniusActivity, the private class HelloWebViewClient is underlined with yellow (never used), specifically the bit "HelloWebViewClient".
It's also throwing FC's at startup, and errors in LogCat, but I'll post those later on, as it's 10.30 PM here, and school goes on
Thanks in advance,
bassie1995

No suggestions yet?
How you doin'? Greetings from my GT-I9000!

Related

[Q] Android GridView imageView help

Hey Android experts,
this may be a n00b like question but I'm a beginner to android and i'm struggling with displaying images in a gridview. All i need to do is display the images i have located in res/drawable when clicked on.
I currently have the following code in java
My GridView Class​
package com.example.gridview;
import com.example.helloandroid.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class HelloGridView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Where I'm struggling, I just need to find a way to display img048 and img049 from mThumbsIds when clicked on
}
});
}
}
My ImageAdapter class​
package com.example.gridview;
import com.example.helloandroid.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.img048, R.drawable.img049
};
}
Thank you SO MUCH everybody!

[Q] [Dev] App Crashes instantly [problem solved]

Whats Wron with this? If I start the app it crashes instantly
It's only a Test app the name only is about what I plan to do in the future^^
Code:
package de.cwsurf.sorry.improvedBattery;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ImprovedBatteryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText eT = (EditText) findViewById(R.id.editText1);
eT.setText("Test");
eT.setText(eT.getText()+"\nTest1\nTest2\nTest3\nTest4");
setContentView(eT);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText eT = (EditText) findViewById(R.id.editText1);
eT.setText(eT.getText()+"\nTest1\nTest2\nTest3\nTest4");
setContentView(eT);
}
});
}
}
problem solved. i only need to leave this away: setContentView(eT);

[Q]ERROR: Void is an invalid type for the variable

I just started developing apps and I've come across the error: Void is an invalid type for the variable button1Click. I got the error on the following line of code
public void button1Click; {
It's in my java class, I'm trying to open an activity from another. This is my entire code looks like this
package trial.play;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class Food extends Activity implements View.OnClickListener {
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singer);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
public void button1Click; {
startActivity(new Intent("android.intent.action.TESTMENUACTIVITY"));}
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.button1:
button1Click: break;
}
}
public void Enter(View view) {
Intent openTestMenuActivity = (new Intent(
"trial.play.TESTMENUACTIVITY"));
startActivity(openTestMenuActivity);
}
}
I'm still new to development so be specific and show me what you mean if you can.
Thanks

Android Webview load website html part

Hi i have created an App with an WebView to show a Website for example Sammobile.
When you go on The website http://www.sammobile.com/ you can see at the bottom of the page a black content.
My Question is: Is it possible to load the page without the black content of the webside or what should i do that it works
My Code:
Code:
package com.androidnews;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AndroidNews extends Activity {
private WebView myWebView;
private String LOG_TAG = "AndroidWebViewActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webView1);
// enable Javascript
myWebView.getSettings().setJavaScriptEnabled(true);
// loads the WebView completely zoomed out
myWebView.getSettings().setLoadWithOverviewMode(true);
// true makes the Webview have a normal viewport such as a normal
// desktop browser
// when false the webview will have a viewport constrained to it's own
// dimensions
myWebView.getSettings().setUseWideViewPort(true);
// myWebView.getSettings().setLoadsImagesAutomatically(false);
// override the web client to open all links in the same webview
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebChromeClient(new MyWebChromeClient());
// Injects the supplied Java object into this WebView. The object is
// injected into the
// JavaScript context of the main frame, using the supplied name. This
// allows the
// Java object's public methods to be accessed from JavaScript.
myWebView.addJavascriptInterface(new JavaScriptInterface(this),
"Android");
// load the home page URL
myWebView.loadUrl("http://www.sammobile.com");
}
// customize your web view client to open links from your own site in the
// same web view otherwise just open the default browser activity with the
// URL
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://www.sammobile.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
private class MyWebChromeClient extends WebChromeClient {
// display alert message in Web View
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
Log.d(LOG_TAG, message);
new AlertDialog.Builder(view.getContext()).setMessage(message)
.setCancelable(true).show();
result.confirm();
return true;
}
}
public class JavaScriptInterface {
Context mContext;
// Instantiate the interface and set the context
JavaScriptInterface(Context c) {
mContext = c;
}
// using Javascript to call the finish activity
public void closeMyActivity() {
finish();
}
}
// Web view has record of all pages visited so you can go back and forth
// just override the back button to go back in history if there is page
// available for display
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.actionbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
myWebView.loadUrl("http://www.sammobile.com");
break;
case R.id.firmware:
myWebView
.loadUrl("http://www.sammobile.com/category/firmware-news/");
break;
case R.id.latest:
myWebView
.loadUrl("http://www.sammobile.com /news/?section=all-sections");
break;
}
return true;
}
}

[JAVA PROBLEM] Successfull compiled app crashes,

Anyone see what im doing wrong here? im new to java so i dont have a clue..
successfully compiles without a problem but crashes on start, the blue code i have added
Code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
[COLOR=DeepSkyBlue]import android.widget.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;[/COLOR]
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
[COLOR=DeepSkyBlue] Button startpatching = (Button)findViewById(R.id.start);
startpatching.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Process proc = Runtime.getRuntime().exec("/data/local/startpatch.sh /");
BufferedReader read = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
try {
proc.waitFor();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
while (read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}}
});
};[/COLOR]
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 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;
}
}
}
Probably would be wise to include the stack trace...
ricky310711 said:
Anyone see what im doing wrong here? im new to java so i dont have a clue..
successfully compiles without a problem but crashes on start, the blue code i have added
Code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
[COLOR=DeepSkyBlue]import android.widget.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;[/COLOR]
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
[COLOR=DeepSkyBlue] Button startpatching = (Button)findViewById(R.id.start);
startpatching.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Process proc = Runtime.getRuntime().exec("/data/local/startpatch.sh /");
BufferedReader read = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
try {
proc.waitFor();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
while (read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}}
});
};[/COLOR]
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 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;
}
}
}
Click to expand...
Click to collapse
Post a log/stacktrace as jcase suggested and also the content of activity_main.xml
jcase said:
Probably would be wise to include the stack trace...
Click to expand...
Click to collapse
Jonny said:
Post a log/stacktrace as jcase suggested and also the content of activity_main.xml
Click to expand...
Click to collapse
thanks guy, got it working but

Categories

Resources