So im parsing JSON data from a API Enabled website, It requires authentiucation by a APIKEY, My issue is that im trying to parse the data then insert it into TextViews on the UI. My current code previously used to use a HashMap and a ListView However now im trying to change it to just populate the data into the TextViews. Currently, I've started changing the code over and now im getting RunTimeExceptions with my current code. Any Help would be greatly appreciated.
Issue Resolved
public class ParseMoreDetails extends Activity {
private Context context;
private static String url = "https://api.company.com/api/systems?";
private static final String TAG_SYSTEM = "systems";
private static final String TAG_SYSTEM_ID = "system_id";
private static final String TAG_CITY = "city";
private static final String TAG_STATE = "state";
private static final String TAG_SYSTEM_NAME = "system_name";
private static final String TAG_SYSTEM_PUBLIC_NAME = "system_public_name";
private static final String TAG_STATUS = "status";
TextView textView;
TextView textView2;
TextView textView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView2);
textView2 = (TextView) findViewById(R.id.TextView01);
textView3 = (TextView) findViewById(R.id.TextView03);
new ProgressTask(ParseMoreDetails.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, ArrayList<String>> {
private ProgressDialog dialog;
ArrayList<String> arrfortextviews;
private ParseMoreDetails activity;
// private List<Message> messages;
public ProgressTask(ParseMoreDetails parseMoreDetails) {
this.activity = parseMoreDetails;
context = parseMoreDetails;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(ArrayList<String> success) {
if(arrfortextviews.size() >0){
textView.setText(success.get(0));
textView2.setText(success.get(1));
textView3.setText(success.get(2));
}
if (dialog.isShowing()) {
dialog.dismiss();
}
}
protected ArrayList<String> doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
arrfortextviews=new ArrayList<String>();
// Using APIKEY from strings.xml
String apikey = getString(R.string.apikey);
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url + "&key=" + apikey);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String vcolor = c.getString(TAG_SYSTEM_ID);
String vfuel = c.getString(TAG_CITY);
String vtread = c.getString(TAG_STATE);
arrfortextviews.add(vcolor);
arrfortextviews.add(vfuel);
arrfortextviews.add(vtread);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return arrfortextviews;
}
}
Layout
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="System ID: " />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..." />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City:" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..." />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="State:" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..." />
</LinearLayout>
</FrameLayout>
</LinearLayout>
Current Errors
RuntimeException - NullPointerException
Leaked Window com.android.internal.policy.impl.PhoneWindow$DecorView
LogCat - UPDATED
05-01 10:27:47.040: E/AndroidRuntime(28236): FATAL EXCEPTION: AsyncTask #1
05-01 10:27:47.040: E/AndroidRuntime(28236): java.lang.RuntimeException: An error occured while executing doInBackground()
05-01 10:27:47.040: E/AndroidRuntime(28236): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-01 10:27:47.040: E/AndroidRuntime(28236): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.lang.Thread.run(Thread.java:856)
05-01 10:27:47.040: E/AndroidRuntime(28236): Caused by: java.lang.NullPointerException
05-01 10:27:47.040: E/AndroidRuntime(28236): at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.doInBackground(ParseMoreDetails.java:116)
05-01 10:27:47.040: E/AndroidRuntime(28236): at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.doInBackground(ParseMoreDetails.java:1)
05-01 10:27:47.040: E/AndroidRuntime(28236): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-01 10:27:47.040: E/AndroidRuntime(28236): ... 5 more
05-01 10:27:55.035: I/Choreographer(28236): Skipped 464 frames! The application may be doing too much work on its main thread.
05-01 10:27:55.210: E/WindowManager(28236): Activity com.jitesh.androidjsonparser.ParseMoreDetails has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#4285c818 that was originally added here
05-01 10:27:55.210: E/WindowManager(28236): android.view.WindowLeaked: Activity com.jitesh.androidjsonparser.ParseMoreDetails has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#4285c818 that was originally added here
05-01 10:27:55.210: E/WindowManager(28236): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:402)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.Window$LocalWindowManager.addView(Window.java:554)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.Dialog.show(Dialog.java:277)
05-01 10:27:55.210: E/WindowManager(28236): at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.onPreExecute(ParseMoreDetails.java:86)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.AsyncTask.execute(AsyncTask.java:534)
05-01 10:27:55.210: E/WindowManager(28236): at com.jitesh.androidjsonparser.ParseMoreDetails.onCreate(ParseMoreDetails.java:63)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.Activity.performCreate(Activity.java:5206)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.Looper.loop(Looper.java:137)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-01 10:27:55.210: E/WindowManager(28236): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 10:27:55.210: E/WindowManager(28236): at java.lang.reflect.Method.invoke(Method.java:511)
05-01 10:27:55.210: E/WindowManager(28236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-01 10:27:55.210: E/WindowManager(28236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-01 10:27:55.210: E/WindowManager(28236): at dalvik.system.NativeStart.main(Native Method)
In Here :
TextView textView = (TextView) findViewById(R.id.textView2); //<<< here
TextView textView2 = (TextView) findViewById(R.id.TextView01);//<<< here
TextView textView3 = (TextView) findViewById(R.id.TextView03);//<<< here
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
you are using findViewById before setting layout for current Activity .so you will need to move all Widgets initialize inside onCreate method of Activity after setting layout for it. Change your code as:
TextView textView,textView2,textView3; //<<<declare here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize all textViews here
textView = (TextView) findViewById(R.id.textView2);
textView2 = (TextView) findViewById(R.id.TextView01);
textView3 = (TextView) findViewById(R.id.TextView03);
......
you are having an error in doInBackround of the AsyncTask
I suspect that
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url + "&key=" + apikey);
returns null
put this line instead may be it will work
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url + "key=" + apikey);
you shouldn't handle UI elements (TextViews) from doInBackground OnPostExecute is made for that
Related
I am using Jsoup in my Android app to get images from the internet but an getting a NullPointerException.
Java:
public class CollegeInfo extends Activity{
TextView body;
ImageView image1;ImageView image2;ImageView image3;ImageView image4;
ImageView image8;ImageView image7;ImageView image6;ImageView image5;
String college;
String ataglance;
double satHigh;
double satLow;
Bitmap[] bitmap;
String[] imgSrcs;InputStream[] input;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.collegeinfo);
Bundle extras = getIntent().getExtras();
if (extras != null) {
college = extras.getString("tag");
}
SpannableStringBuilder buffer = new SpannableStringBuilder();
body = (TextView) findViewById(R.id.title);
image1 = (ImageView) findViewById(R.id.imageView1);
image2 = (ImageView) findViewById(R.id.imageView2);
image3 = (ImageView) findViewById(R.id.imageView3);
image4 = (ImageView) findViewById(R.id.imageView4);
image5 = (ImageView) findViewById(R.id.imageView5);
image6 = (ImageView) findViewById(R.id.imageView6);
image7 = (ImageView) findViewById(R.id.imageView7);
image8 = (ImageView) findViewById(R.id.imageView8);
try {
Document doc = Jsoup.connect("http://www.forbes.com/colleges/"+college+"/").get();
String name = doc.select("meta[name=keywords]").attr("content");
int nameLen = name.length();
buffer.append(name+"\n");
String city = doc.select("ul[class=address]").select("li").first().text();
buffer.append(city+"\n");
ataglance = "";
for(int i=0;i<8;i++){
ataglance += doc.select("div[class=ataglanz fleft]").select("li").get(i).ownText()+" ";
ataglance += doc.select("div[class=ataglanz fleft]").select("b").get(i).text();
ataglance += "\n";
}
String satRange = doc.select("div[class=ataglanz fleft]").select("b").get(8).text();
int satLength = satRange.length();
try{
satHigh = Double.parseDouble(satRange.substring(satLength-4, satLength));
satLow = Double.parseDouble(satRange.substring(0, satLength-5));
}catch(NumberFormatException e){
e.printStackTrace();
}
satHigh*=1.5; satLow*=1.5;
int satL = (int) Math.round(satLow); int satH = (int) Math.round(satHigh);
ataglance += "SAT Composite Range: "+Integer.toString(satL)+"-"+Integer.toString(satH);
buffer.append(ataglance+"\n");
for(int i = 0;i<8;i++){
Elements img = doc.select("div[id=photos]").select("a");
imgSrcs[i] = img.text();
input[i] = new java.net.URL(imgSrcs[i]).openStream();
bitmap[i] = BitmapFactory.decodeStream(input[i]);
}
image1.setImageBitmap(bitmap[0]);
image2.setImageBitmap(bitmap[1]);
image3.setImageBitmap(bitmap[2]);
image4.setImageBitmap(bitmap[3]);
image5.setImageBitmap(bitmap[4]);
image6.setImageBitmap(bitmap[5]);
image7.setImageBitmap(bitmap[6]);
image8.setImageBitmap(bitmap[7]);
body.setText(buffer.toString());
}catch(IOException e){
e.printStackTrace();
}
}
{
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
}
XML:
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.06" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Before I added in the images, everything was working fine, so I believe that I am not parsing the images correctly.
Logcat:
07-29 17:19:49.985: E/AndroidRuntime(10855): FATAL EXCEPTION: main
07-29 17:19:49.985: E/AndroidRuntime(10855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.collegeselector/com.collegeselector.CollegeInfo}: java.lang.NullPointerException
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.os.Looper.loop(Looper.java:137)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-29 17:19:49.985: E/AndroidRuntime(10855): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 17:19:49.985: E/AndroidRuntime(10855): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-29 17:19:49.985: E/AndroidRuntime(10855): at dalvik.system.NativeStart.main(Native Method)
07-29 17:19:49.985: E/AndroidRuntime(10855): Caused by: java.lang.NullPointerException
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.collegeselector.CollegeInfo.onCreate(CollegeInfo.java:87)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.Activity.performCreate(Activity.java:5104)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-29 17:19:49.985: E/AndroidRuntime(10855): ... 11 more
What should I change to get the images to work?
Look at your stacktrace:
07-29 17:19:49.985: E/AndroidRuntime(10855): Caused by: java.lang.NullPointerException
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.collegeselector.CollegeInfo.onCreate(CollegeInfo.java:87)
You haven't initialized the string array String[] imgSrcs; yet you try to add content to it in your for each loop:
imgSrcs[i] = img.text();
Also, the size of an array cannot be modified, so you either decide upon a size of it when you initialize it and then stick with it. If you want a dynamic size, use an ArrayList object instead.
I am trying to create a simple application that can sign in / sign up users . All the data are
stored in Mysql database .
My app was installed successfully but as I opened it, it gave an error saying "Unfortunately, 'myapp' has stop working".
I have read almost all related problems posted here but no luck!
here's from logcat .
07-25 01:22:31.452: E/AndroidRuntime(2101): FATAL EXCEPTION: main
07-25 01:22:31.452: E/AndroidRuntime(2101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysqltest/com.example.mobileapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Looper.loop(Looper.java:137)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invoke(Method.java:525)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 01:22:31.452: E/AndroidRuntime(2101): at dalvik.system.NativeStart.main(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.setContentView(Activity.java:1895)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.example.mobileapp.MainActivity.onCreate(MainActivity.java:45)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.performCreate(Activity.java:5133)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 11 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.constructNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 23 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.OutOfMemoryError
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.Resources.loadDrawable(Resources.java:2105)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3364)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3293)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.ViewGroup.<init>(ViewGroup.java:453)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" >
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/password"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:text="Password" />
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp" />
<TextView
android:id="#+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="38dp"
android:gravity="center"
android:text="PhilTaxWindow User Login"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/username"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp"
android:text="Username" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/register"
android:layout_alignBottom="#+id/register"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/register"
android:text="Don't Have an Account?"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/register"
android:layout_alignParentLeft="true"
android:layout_marginBottom="60dp"
android:text="Login" />
<EditText
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tv2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:ems="10"
android:inputType="textPassword"
android:singleLine="true" />
<Button
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv"
android:layout_alignParentRight="true"
android:layout_marginBottom="17dp"
android:layout_marginRight="29dp"
android:text="Register here" />
</RelativeLayout>
mainactivity.java
package com.example.mobileapp;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.mysqltest.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText Username, Password;
private Button Submit, Register;
private ProgressDialog Dialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.4/checkingPHP/CheckUser.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Username = (EditText)findViewById(R.id.username);
Password = (EditText)findViewById(R.id.password);
Submit = (Button)findViewById(R.id.Button01);
Register = (Button)findViewById(R.id.register);
Submit.setOnClickListener(this);
Register.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
boolean failure = false;
#Override
protected void onPreExecute() {
super.onPreExecute();
Dialog = new ProgressDialog(MainActivity.this);
Dialog.setMessage("Validating...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
#Override
protected String doInBackground(String... args) {
int success;
String username = Username.getText().toString();
String password = Password.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", username));
params.add(new BasicNameValuePair("Password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(MainActivity.this, WelcomPage.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String file_url) {
Dialog.dismiss();
if (file_url != null){
Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
The emulator ran into out of memory exception
Caused by: java.lang.OutOfMemoryError due to
android:background="#drawable/background"
The image that you are trying to load is causing this. Try reducing the image resolution.
I was trying to add a new TextView every time I click on a button, in a new activity (already created), but I encountered multiple errors such as Null Pointer Exception and Illegal State Exception too.
This is the code I used in the button activity, and it's activated when I press that button.
In the code I'm recalling the layout activity_position which is the activity I would like to add the TextView to.
RelativeLayout rl = (RelativeLayout)findViewById(R.layout.activity_position);
TextView tv = new TextView(this);
tv.setId(10);
tv.setText(addr);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
Running this code gets me only errors (on rl.addView(tv) line) and I can't figure it out why.
This is the XML of the activity where I would like to add the TextViews dynamically.
<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=".PositionActivity" >
02-18 12:21:47.348: E/AndroidRuntime(4995): FATAL EXCEPTION: main
02-18 12:21:47.348: E/AndroidRuntime(4995): java.lang.IllegalStateException: Could not execute method of the activity
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$1.onClick(View.java:3758)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View.performClick(View.java:4377)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$PerformClick.run(View.java:18044)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Handler.handleCallback(Handler.java:725)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Looper.loop(Looper.java:137)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.app.ActivityThread.main(ActivityThread.java:5306)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-18 12:21:47.348: E/AndroidRuntime(4995): at dalvik.system.NativeStart.main(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.reflect.InvocationTargetException
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$1.onClick(View.java:3753)
02-18 12:21:47.348: E/AndroidRuntime(4995): ... 11 more
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.NullPointerException
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.example.placeholder.LocatingActivity.getCoords(LocatingActivity.java:101)
EDIT: added the LogCat
Can you help me out?
Thank you!
FULL CODE: https://drive.google.com/folderview?id=0B3uaHczyJIVQVXhMR1lyN3JnWjQ&usp=sharing
Currently you are trying to cast R.layout.activity_position to RelativeLayout. assign id to RelativeLayout then use R.id.relatv_layout to in code as:
in xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relatv_layout"
....
tools:context=".PositionActivity" >
In code use R.layout.relatv_layout to initialize rl :
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relatv_layout);
The root exception in your logcat output is in LocatingActivity, line 101. In that activity there are no OnClickListeners, so let's see if I got it right: you're trying to modify contents of an Activity from another Activity (I assume first one is under second one in the stack)?
You can't do that, ever. You can only manipulate views/layouts of the activity that's currently active (i.e. shown), all other activities are paused and may have even been destroyed.
I suggest you read this: http://developer.android.com/guide/components/activities.html along with other basic documentation on the site.
Full answer:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Test extends Activity {
private LinearLayout LL;
private RelativeLayout RL;
private Button btn;
int click_counter = 0;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_test);
RL = (RelativeLayout)findViewById(R.id.relative_layout);
btn = (Button)findViewById(R.id.btn);
LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.VERTICAL);
RL.addView(LL);
// Listener
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(getApplicationContext());
tv.setText("Click: " + ++click_counter);
LL.addView(tv);
}
});
} // onCreate()
}
and layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
This is my very first app. All I need is five buttons, two that call certain phone numbers (only have created one so far) and three buttons that take the user to a certain URL. I have no errors or warnings or any direction on how to navigate the LogCat.
LogCat:
10-08 14:41:40.716: D/AndroidRuntime(793): Shutting down VM
10-08 14:41:40.716: W/dalvikvm(793): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-08 14:41:40.777: E/AndroidRuntime(793): FATAL EXCEPTION: main
10-08 14:41:40.777: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{acps.mhs/acps.mhs.MainActivity}: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Looper.loop(Looper.java:137)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invoke(Method.java:525)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 14:41:40.777: E/AndroidRuntime(793): at dalvik.system.NativeStart.main(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at acps.mhs.MainActivity.onCreate(MainActivity.java:25)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Activity.performCreate(Activity.java:5133)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-08 14:41:40.777: E/AndroidRuntime(793): ... 11 more
10-08 14:42:19.696: I/Process(793): Sending signal. PID: 793 SIG: 9
Mainactivity.java
package acps.mhs;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button mhshome, pp, mhsdir, cmhs;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
mhshome = (Button) findViewById(R.id.mhshome);
pp = (Button) findViewById(R.id.pp);
mhsdir = (Button) findViewById(R.id.mhsdir);
cmhs = (Button) findViewById(R.id.cmhs);
mhshome.setOnClickListener(this);
pp.setOnClickListener(this);
mhsdir.setOnClickListener(this);
cmhs.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.mhshome:
Uri uri = Uri.parse("http://www2.k12albemarle.org/school/mohs/Pages/default.aspx");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
case R.id.pp:
Uri uri2 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(intent2);
break;
case R.id.mhsdir:
Uri uri3 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent3 = new Intent(Intent.ACTION_VIEW, uri3);
startActivity(intent3);
break;
case R.id.cmhs:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);
break;
}
}
#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;
}
}
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" >
<Button
android:id="#+id/pp"
style="#style/AppBaseTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhshome"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="#string/pp"/>
<Button
android:id="#+id/mhshome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:onClick="onClick"
android:text="#string/mhshome" />
<Button
android:id="#+id/mhsdir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/mhsdir" />
<Button
android:id="#+id/cmhs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhsdir"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/callmhs" />
</RelativeLayout>
Its gonna be something incredibly simple I'm certain. Please point me in the right direction.
You need to call setContentView(R.layout.activity_main) :) That's why findViewById is returning null on your views.
You are not setting your layout.
Call setContentView(layout) in your onCreate() method.
I cannot figure out why these 2 buttons are not working, i have a layout file inwhich is created when the user selects a certain theme in my application. With the layout it has a webview it has 2 buttons to goback and goforward.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward">
</Button>
<WebView
android:id="#+id/webview01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.12" >
This is not the entire layout file, but here is the bit of stuff i am working with for the webview and the 2 buttons, now inside my main activity here is my button code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else if (Prefs.theme.equals("Theme2"))
setContentView(R.layout.main2);
else if (Prefs.theme.equals("Theme3"))
setContentView(R.layout.main3);
else setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.button1);
btnBackward=(Button) findViewById (R.id.button2);
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// more code within on create .....
// Later in the code
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.button2:
WebViewClientDemoActivity.web.goForward();
break;
}
//
I problem im having is by default it loads main.xml not main3 (which is were the 2 buttons are)
LogCat Errors
07-27 16:00:34.802: E/AndroidRuntime(547): FATAL EXCEPTION: main
07-27 16:00:34.802: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jaisonbrooks.enlighten/com.jaisonbrooks.enlighten.WebViewClientDemoActivity}: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:123)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:507)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-27 16:00:34.802: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onCreate(WebViewClientDemoActivity.java:75)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-27 16:00:34.802: E/AndroidRuntime(547): ... 11 more
First in your onCreate method find your buttons by ids :
public class YourActivity extends Activity implements OnClickListener {
Button btnForward;
Button btnBackward;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.btnForward);
btnBackward=(Button) findViewById (R.id.btnBackward);
//set listeners
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// your code here ....
}
#Override
public void onClick(View v ) {
switch(v.getId()) {
case R.id.btnBackward:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.btnForward:
WebViewClientDemoActivity.web.goForward();
break;
}
}
}
It should work. Make sure you don't have 2 buttons with the same #id.
It's a common issue when you multiplicate your buttons from one. (copy/paste)
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward" />
try this.
then later in java code, do this
Button btnNext = (Button) findViewById(R.id.button2);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goForward();
}
});
Button btnBack = (Button) findViewById(R.id.button1);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goBack();
}
});