I've got problem with using setText() method on Android 16 (4.1) app. I'm trying to put programatically anything into TextView tvopis (here's just some test string, later I plan on putting there string from intent).
When I open activity opis, the app crashes. If I comment setText() part, everything works fine.
Here's java code
package com.example.pierwszyandroid;
import android.app.Activity;
import android.app.ActionBar;
import android.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.widget.TextView;
import android.os.Build;
public class OpisActivity extends Activity {
public TextView tvopis;
String txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opis);
tvopis = (TextView) findViewById(R.id.tvotest);
tvopis.setText("txt test");
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
Here's fragment_opis.xml for this 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="com.example.pierwszyandroid.OpisActivity$PlaceholderFragment" >
<TextView
android:id="#+id/tvotest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</RelativeLayout>
and this is activity_opis.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pierwszyandroid.OpisActivity"
tools:ignore="MergeRootFrame" />
and here's logcat with error message:
04-18 10:44:59.665: I/Choreographer(723): Skipped 206 frames! The application may be doing too much work on its main thread.
04-18 10:46:34.016: I/Choreographer(723): Skipped 41 frames! The application may be doing too much work on its main thread.
04-18 10:46:34.135: D/AndroidRuntime(723): Shutting down VM
04-18 10:46:34.135: W/dalvikvm(723): threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
04-18 10:46:34.155: E/AndroidRuntime(723): FATAL EXCEPTION: main
04-18 10:46:34.155: E/AndroidRuntime(723): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pierwszyandroid/com.example.pierwszyandroid.OpisActivity}: java.lang.NullPointerException
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.os.Looper.loop(Looper.java:137)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 10:46:34.155: E/AndroidRuntime(723): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 10:46:34.155: E/AndroidRuntime(723): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 10:46:34.155: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 10:46:34.155: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 10:46:34.155: E/AndroidRuntime(723): at dalvik.system.NativeStart.main(Native Method)
04-18 10:46:34.155: E/AndroidRuntime(723): Caused by: java.lang.NullPointerException
04-18 10:46:34.155: E/AndroidRuntime(723): at com.example.pierwszyandroid.OpisActivity.onCreate(OpisActivity.java:25)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.Activity.performCreate(Activity.java:5008)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-18 10:46:34.155: E/AndroidRuntime(723): ... 11 more
Any help would be really appreciated, because I couldn't find any answer on already posted similar problems.
If you look at the log cat, you will see that you have a null pointer exception when you try to set the text to the text view (line 25 in your OpisActivity).
This means that the txt view is not found in the layout and thus cannot be initialised. Since your textview is in the fragment, you have to first add the fragment to the layout and then find the view.
To solve your problem, move the findById and setText method after the fragment initialisation in your onCreate method, like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opis);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
tvopis = (TextView) findViewById(R.id.tvotest);
tvopis.setText("txt test");
}
Your app crashed because your TextView tvotest belong to fragment_opis.xml layout and you have to set activity_opis.xml file to your activity.
so initialized PlaceholderFragment() first and then initialized your TextView tvotest or change your layout like
setContentView(R.layout.fragment_opis.xm)
You have wrongly set your layout in your onCreate() method. You TextView resides inside layout fragment_opis.xml whereas you have set layout as setContentView(R.layout.activity_opis);.
Just change your setContentView(R.layout.activity_opis); line
to as below
setContentView(R.layout. fragment_opis);
change this setContentView(R.layout.activity_opis);
to this
setContentView(R.layout.fragment_opis.xml);
Sometimes when creating layouts in fragments you copy and paste xml code that has duplicate IDs. When you then call the id of the pasted resources it will try to invoke the first defined resources which is probably null at runtime. Double check if the TextView's id is found only in one fragment's Xml. If not work around "findviewbyid".I hope that helps
try checking out the answer at https://stackoverflow.com/a/20177019/14229871 .I was encountering a similar problem with textView, every time I call set text my app would crash. I think it's a matter of how u call set text.
Related
I have a main activity and I call a second "demo from android studio" activity when I click on a button of the first activity like this :
public void createNetworkButtonClicked (View view) {
Intent intent = new Intent(this, WiFiDirectActivity.class); // that works
startActivity(intent); // that does not work
}
I get this error :
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3698)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17337)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
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:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
Thanks for your help
There can be possibly two reasons for it.
1) Check your method name in onClick attribute of XML file. It should match with the name of the method in java.
2) Create an activity instead of creating individual XML file and Java file for the second class. You can create Activity by right clicking on app folder-->new-->activity-->empty activity.
Im newbie at developing Android apps but even the simpliest app always crashes when I try to add click listener I tried to google my problem but with no success. I also tried to change API to different versions at new project screen. Im able to run Hello World app but when I try to add listener to my app then Im no longer able to run it. Here is my error log.
03-02 21:13:49.153 19700-19700/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.app.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:4538)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ App crashed! Process: com.example.app
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ App crashed! Package: com.example.app v1 (1.0)
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ Application Label: My Application 7
03-02 21:13:51.566 19729-19729/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider
03-02 21:13:56.871 19959-19959/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider
And here only piece of code where I changed something (I added button in design)
public class MainActivity extends ActionBarActivity {
Button btn = null;
#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();
}
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setText("Hi");
}
});
}
According to your comment on the last answer, you do not import any R file.
You need to import it this way:
import com.<package_name>.<app_name>.R;
That will import the ids generated file available into your Java code and the Button id button will be visible.
Look at the line 32 of the file MainActivity.java. Sounds like you're trying to call a method on a null object.
For example, are you sure your layout activity_main.xml contains a Button with an id R.id.button?
The problem is either that R.id.button doesn't exist in your activity_main.xml layout, or that there is something null in this code:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Try them both out separately and figure out which one isn't working. Go from there.
By the way, the NullPointerException occurs on line 32.
This question already has answers here:
Libraries do not get added to APK anymore after upgrade to ADT 22
(7 answers)
Closed 9 years ago.
I'm trying to use fragments to create a simple, persistent navigation bar. The problem is I'm getting the following Error output.
05-23 14:58:02.861: E/AndroidRuntime(882): FATAL EXCEPTION: main
05-23 14:58:02.861: E/AndroidRuntime(882): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {org.childrensmuseum.visittcmindy/org.childrensmuseum.visittcmindy.MainActivity}: java.lang.ClassNotFoundException: org.childrensmuseum.visittcmindy.MainActivity
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.os.Looper.loop(Looper.java:137)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 14:58:02.861: E/AndroidRuntime(882): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-23 14:58:02.861: E/AndroidRuntime(882): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-23 14:58:02.861: E/AndroidRuntime(882): at dalvik.system.NativeStart.main(Native Method)
05-23 14:58:02.861: E/AndroidRuntime(882): Caused by: java.lang.ClassNotFoundException: org.childrensmuseum.visittcmindy.MainActivity
05-23 14:58:02.861: E/AndroidRuntime(882): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
05-23 14:58:02.861: E/AndroidRuntime(882): ... 11 more
My main activity looks like this:
package org.childrensmuseum.visittcmindy;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.ImageButton;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFormat(PixelFormat.RGBA_8888);
findViewById(R.id.NavigationBar).getBackground().setDither(true);
ImageButton homeButton = (ImageButton) findViewById(R.id.home_button);
homeButton.setColorFilter(Color.argb(255, 90, 179, 0));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
My Fragment looks like this:
package org.childrensmuseum.visittcmindy;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainNavigation extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.navigation_main, container, false);
}
}
Lastly my main activity xml file looks like this:
<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"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/HomeBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/homescreen_desc"
android:src="#drawable/homescreen"
android:scaleType="centerCrop"
/>
<fragment android:name="org.childrensmuseum.visittcmindy.MainNavigation"
android:id="#+id/NavigationBar"
android:layout_gravity="bottom" />
</RelativeLayout>
What am I missing?
EDITED to try to make it a general answer to this issue (in this case, the correct answer is provided by #Raghunandan):
Humm... so your MainActivity is "lost" (ClassNotFoundException: org.childrensmuseum.visittcmindy.MainActivity):
1.- May be an Eclipse (or whatever IDE you are using) building issue. Clean your Project and rebuild it and/or restart Eclipse.
2.- It can be a problem with your Manifest file. Add your Activity in your 'AndroidManifest.xml', making sure your Package and Activity names are correctly written (check caps). An example could be:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.childrensmuseum.visittcmindy">
<application>
<activity android:name=".MainActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
3.- Doesn't seem to be your case, but sometimes external libraries are not correctly included in the Build Path. You have to put your third party libraries in a "libs" folder and re-reference them (Right-click, properties, Java Build Path, Libraries, Add Jar...).
Shared libraries like the Maps library, which is not a part of the standard Android library, must be declared in the Android Manifest. Open the AndroidManifest.xml file and add the following as a child of the element:
<uses-library android:name="com.google.android.maps"/>
4.- #Raghunandan gives a solution here in case you've updated to Android SDK Tools Rev 22.
Right click on your project goto properties. Java Build Path. Choose Order export tab. Make sure that Android Private Libraries is selected. If you have referenced library project. do the same for the library project also. Clean and Build.
I am using ActionBarShelock and some time get force close with this error cant find how to handle it .
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sps/com.sps.ui.activities.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v4.app._ActionBarSherlockTrojanHorse.onCreatePanelMenu(_ActionBarSherlockTrojanHorse.java:52)
at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:556)
at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:60)
at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:154)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
at com.android.internal.policy.impl.PhoneWindow.restorePanelState(PhoneWindow.java:1677)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1627)
at android.app.Activity.onRestoreInstanceState(Activity.java:928)
at android.app.Activity.performRestoreInstanceState(Activity.java:900)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
... 11 more
In the r7 support library, the version on which the current release (4.1.0) of ActionBarSherlock is based, the FragmentManager class checked if mActive != null before dispatching menu events to the fragments contained in the mAdded list. This code was copied into ActionBarSherlock since it uses its own MenuItem type and does its own dispatching.
In a subsequent release of the support library, it was found that this check was no longer representative of whether or not mAdded had been instantiated with a list or not. Commit 464b6f3c changed the conditional check to mAdded != null.
ActionBarSherlock has not had a release since this fix was made. However, it was updated on the dev branch in commit 69fe6fd to be included in the next release.
This should really be easy for an experienced android programmer. Unfortunately I don't know anything about the android sdk. I am using PhoneGap to package a web app and I need to override the WebSettings in the cordovaExample.java (right now I am using the cordovaExample as a template for my app). My code looks like this:
package org.apache.cordova.example;
import android.app.Activity;
import android.os.Bundle;
import org.apache.cordova.*;
public class cordovaExample extends DroidGap
{
#Override
public void onCreate(Bundle savedInstanceState)
{
this.appView.getSettings().setUseWideViewPort(true);
this.appView.getSettings().setLoadWithOverviewMode(true);
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
I can run ant debug install without a problem but when I start the app in the emulator, the following errors occur:
E/AndroidRuntime( 549): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.apache.cordova.example/org.apache.cordova.example.cordovaExample}: java.lang.NullPointerException
E/AndroidRuntime( 549): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 549): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime( 549): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime( 549): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime( 549): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 549): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 549): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 549): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 549): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 549): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 549): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 549): at org.apache.cordova.example.cordovaExample.onCreate(cordovaExample.java:12)
E/AndroidRuntime( 549): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 549): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 549): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 549): ... 11 more
W/ActivityManager( 78): Force finishing activity org.apache.cordova.example/.cordovaExample
The errors occur no matter if I package for android sdk 2.1 or 4.3 (on Mac OS X Lion).
Any help will be very much appreciated.
The error says that there is a NullPointerException at line 12:
this.appView.getSettings().setUseWideViewPort(true);
Either this.appView is null of appView.getSettings() is returning null. Add null value checks and see which one it is. It will be easier to find out why there is a null value there once you know which one it is.
Also, nothing to do with the problem and not sure if it is a typo but your class name is camel cased. It is against best practice to do that. It will confuse you in the future to do that since camel case nomenclature is used with instance variables, members, and methods only. Class names are always Initial letter uppercased and package names are always lowercase.
So it should be
CordovaExample
and not
cordovaExample
appView is null ... call super.onCreate(savedInstanceState); first
This was the solution for me when you want the browser to scale the viewport to the device width no matter the pixel width of the device:
Add these lines after super.loadURL(...):
super.appView.getSettings().setLoadWithOverviewMode(true);
super.appView.getSettings().setUseWideViewPort(true);
... in combination with meta viewport attributes that allow for scaling.