I can't get android jsoup to work - android

Hi i need help with a tutorial i tried from ( http://xjaphx.wordpress.com/2012/02/04/android-xml-adventure-parsing-html-using-jsoup/ ). It's about jsoup but i can't get it to work i only get a lott of errors and stuff.
Here is my code:
package com.sigustgebran.appsl;
import android.os.Bundle;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainAct extends Activity {
//blog url
static final String BLOG_URL = "http://xjaphx.wordpress.com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
//set layout view
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
//process
try {
((TextView)findViewById(R.id.tvDisplay)).setText(getBlogStats());
} catch (Exception ex) {
((TextView)findViewById(R.id.tvDisplay)).setText("Error");
}
}
protected String getBlogStats() throws Exception {
String result = "";
//get html document structure
Document document = Jsoup.connect(BLOG_URL).get();
//select path
Elements nodeBlogStats = document.select("div#blog-stats ul li");
//check results
if(nodeBlogStats.size() > 0) {
//get value
result = nodeBlogStats.get(0).text();
}
//return
return result;
}
#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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_gravity="center"
android:id="#+id/tvDisplay"
/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sigustgebran.appsl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.sigustgebran.appsl.MainAct"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I would be really glad if anyone could help me. Thanks

I got it to work now. I had added the jsoup library in "External jars" as he said in the tutorial, but when I deleted it and just copied it in the "libs" map instead it worked.

you have to implement AsyncTask. I think this will help you

Related

How to open PDF file in android device?

I am fresher to android. My task is to open a pdf file in android device. I searched a tutorials and tried it. But I could not open a pdf file. I dont know the error where I did?. Please help me anyone.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp"
android:text="open" />
</RelativeLayout>
MainActivity.java
package com.example.pdftest;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#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 void onClick(View arg0) {
// TODO Auto-generated method stub
File pdffile=new File(Environment.getExternalStorageDirectory(),"/sdcard/abc.pdf");
try
{
if(pdffile.exists())
{
Uri path=Uri.fromFile(pdffile);
Intent objintent=new Intent(Intent.ACTION_VIEW);
objintent.setDataAndType(path, "application/pdf");
objintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objintent);
}
else
{
Toast.makeText(MainActivity.this,"File Not Found",Toast.LENGTH_SHORT).show();
}
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this,"No viewer application Found",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pdftest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.pdftest.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You have to remove the root from the second parameter in File class constructor. When you're instantiating File with File(String dirPath, String name) then the second parameter must contain just the file name and the first the directory where the file is located.
With this correction your code should work :
File pdffile=new File(Environment.getExternalStorageDirectory(),"abc.pdf");

Same xml code, different graphic layout

I am new to android development and i tried copying an xml layout from another project, but the graphic layout of that xml seems to be different from what i am getting. I have no idea what is missing in my project that I am not getting the same layout. Please help.
this is what it looks like
!1
and this is what i get after copying the exact code.
!2
my xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aditya.contentsharer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".InteriorSpottingApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".InteriorListActivity"
android:label="#string/title_activity_interior_list" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.app.Activity" />
</activity>
<activity
android:name=".NewInteriorActivity"
android:label="#string/title_activity_new_interior" >
</activity>
</application>
</manifest>
xml code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
java code
package com.aditya.contentsharer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.ParseQueryAdapter;
public class InteriorListActivity extends ListActivity{
private ParseQueryAdapter<Interior> mainAdapter;
private FavoriteInteriorAdapter favoritesAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setClickable(false);
mainAdapter = new ParseQueryAdapter<Interior>(this, Interior.class);
mainAdapter.setTextKey("title");
mainAdapter.setImageKey("photo");
// Subclass of ParseQueryAdapter
favoritesAdapter = new FavoriteInteriorAdapter(this);
// Default view is all interiors
setListAdapter(mainAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_interior_list, menu);
return true;
}
/*
* Posting interiors and refreshing the list will be controlled from the Action
* Bar.
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
updateinteriorList();
break;
}
case R.id.action_favorites: {
showFavorites();
break;
}
case R.id.action_new: {
newinterior();
break;
}
}
return super.onOptionsItemSelected(item);
}
private void updateinteriorList() {
mainAdapter.loadObjects();
setListAdapter(mainAdapter);
}
private void showFavorites() {
favoritesAdapter.loadObjects();
setListAdapter(favoritesAdapter);
}
private void newinterior() {
Intent i = new Intent(this, NewInteriorActivity.class);
startActivityForResult(i, 0);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
// If a new post has been added, update
// the list of posts
updateinteriorList();
}
}
}
I got the solution. I removed android:theme="#style/AppTheme" from the manifest file. That worked fine for me.
I am not used to deal with ListActivities since I usually do it with a ListView in a normal activity but my first guess is that you still need to have a line like this in your onCreate() method to actually get any layout...
setContentView(R.layout.fragmentContainer);
(assuming your xml file has the same name as it's ID right?
Try putting it after the
super.onCreate(savedInstanceState);
I am not sure if it will work but try it! :)

when add admob code to my app

i have exotic problem
nothing error in my project
how to solve this problem pleas help
thanks all to help me
hi all
i have exotic problem
nothing error in my project
how to solve this problem pleas help
thanks all to help me
The following classes could not be instantiated:
- com.google.android.gms.ads.AdView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads "
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxxxxxxxxxxxx"
/>
</LinearLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.admobapp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity android:name=".BannerXMLSample"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
MainAcitvity.java
package com.test.admobapp;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private static final String TEST_DEVICE_ID = "xxxxxxxxxxxxxxxxxxxx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(TEST_DEVICE_ID)
.build();
adView.loadAd(adRequest);
}
#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);
}
}
If there is no more detail to that log message then it means that you have not included the Google Play Services library into your APK.
You can use like this in layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
</LinearLayout>

unfortunately API demo has stopped while running

I have developed an app for page navigation . when a button is clicked the page should navigate to the second page. when i run the project and opened my app its showing unfortunately API demo has stopped. I have posted the entire code of my app . PLS HELP ME OUT...
//MAIN ACTIVITY
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="73dp"
android:layout_marginTop="14dp"
android:text="Click" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="34dp"
android:text="Main Activity" />
</RelativeLayout>
//SECONDSCREEN ACTIVITY
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sceond Screen Activity"
tools:context=".SecondScreenActivity"/>
</LinearLayout>
//JAVA CODE FOR MAIN ACTIVITY
package com.example.navigate;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonOnClickEventListener();
}
public void addButtonOnClickEventListener()
{
Button button = (Button)findViewById(R.id.button1);
final Context context = this;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,SecondScreenActivity.class);
startActivity(intent);
}
});
}
#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;
}
}
// JAVA CODE FOR SECOND SCREEN ACTIVITY
package com.example.navigate;
import android.app.Activity;
public class SecondScreenActivity extends Activity {
}
// MANIFEST FILE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.navigate"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.navigate.SecondScreenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name = "android.intent.action.MAIN"/>
<category android:name = "android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
First problem MainActivity is your launcher page here but you define the secondactivity as luncher in manifest.change it first.
Add an oncreate() to secondactivity otherwise there is nothing to show when the button click.
Third change your MainActivity Context to activity which is MainActivity.this not local
Other things are fine , and hope you understand your small problems..
Change your manifest to something like this:
// MANIFEST FILE
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.navigate.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name = "android.intent.action.MAIN"/>
<category android:name = "android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name = ".SecondScreenActivity" />
</application>
And in your SecondScreenActivity add an oncreate method with the contet view like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity_layout);
}
And also in your mainactivity change the context to MainActivity.this . Here you define the context inside onclicklistener which links to your local method not the class.

Android Activity Breaking

I'm just trying to come up with a skeleton for an activity that uses a ListView as a news feed for a game my class is making.
In this activity there's simply the news feed, and a Refresh button (to update the ListView with the latest news)
My activity_news.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<ListView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="320dp"
>
</ListView>
<Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:text="Refresh"
android:id="#+id/bRef"
/>
My NewsActivty.java:
package com.example.mynews;
import com.example.mynews.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class NewsActivity extends Activity {
Button refresh;
String newsfeed [] = {"latest news", "older news"};
Intent ref = new Intent(this, NewsActivity.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
refresh = (Button) findViewById(R.id.bRef);
ListView newsStuff = (ListView) findViewById(R.id.news);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, newsfeed);
newsStuff.setAdapter(adapter);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(ref);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
}
And my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mynews"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.mynews.NewsActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I built everything up piece by piece and ran it as I finished something new (the layout, the array adapter, etc) and the activity started to break once I added the OnClickListener (emulator tells me "mynews has unfortunately stopped") for my Refresh button. I'm sure there's a better way to accomplish what i'm trying to do, but any assistance with what I have so far would be greatly appreciated.
Did u define a class ref.java?
if yes then you need define that activity(ref) in the manifest file.

Categories

Resources