public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
System.loadLibrary("detection_based_tracker");
try {
InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
if (mJavaDetector.empty()) {
Log.e(TAG, "Failed to load cascade classifier");
mJavaDetector = null;
} else
Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());
cascadeDir.delete();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
}
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
if (mAbsoluteFaceSize == 0) {
int height = mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0) {
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
}
MatOfRect faces = new MatOfRect();
if (mDetectorType == JAVA_DETECTOR){
if (mJavaDetector != null)
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2,
new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
}
else {
Log.e(TAG, "Detection method is not selected!");
}
Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++)
{
Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 5);
}
return mRgba;
}
While executing this code for face detection. I got this, application stopped unexpectedly.I decided to do face detection without any options and all. So can anybody suggest me what's wrong with my code.
Thanks in advance.
logcat
01-24 09:50:04.605: D/dalvikvm(335): newInstance failed: p0 i0 [0 a1
01-24 09:50:04.605: D/AndroidRuntime(335): Shutting down VM
01-24 09:50:04.605: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-24 09:50:04.625: E/AndroidRuntime(335): FATAL EXCEPTION: main
01-24 09:50:04.625: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.faces/com.example.faces.MainActivity}: java.lang.InstantiationException: com.example.faces.MainActivity
01-24 09:50:04.625: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-24 09:50:04.625: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 09:50:04.625: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
01-24 09:50:04.625: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 09:50:04.625: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 09:50:04.625: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
01-24 09:50:04.625: E/AndroidRuntime(335): Caused by: java.lang.InstantiationException: com.example.faces.MainActivity
01-24 09:50:04.625: E/AndroidRuntime(335): at java.lang.Class.newInstanceImpl(Native Method)
01-24 09:50:04.625: E/AndroidRuntime(335): at java.lang.Class.newInstance(Class.java:1409)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-24 09:50:04.625: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
01-24 09:50:04.625: E/AndroidRuntime(335): ... 11 more
Caused by: java.lang.InstantiationException: com.example.faces.MainActivity
Make sure your MainActivity class can be instantiated. Some common causes for this:
The class is abstract.
There is an explicit constructor an it takes arguments. Activities shouldn't really need an explicit constructor. If one is provided, it should take no arguments.
There's nothing wrong with the code you have attached here as I have a working version of the same sample code. Have you added the activity to the AndroidManifest.xml file?
I have been having the same issue with this all types of applications said as unexpectedly stopped/please force close. what I did is I totally restored my phone and it helped me avoid this issue again.
Before restoring be sure that all your needed docs and files are safe, transferred to other device or you can download one program from google market which helps in shifting files from file to the other new file in it.
And finally to restore have to go to settings-applications-restore SD card and phone, phone will be switched off and need to start again uploading your gmail account and connected to internet or WiFi.
Related
I was trying to programmatically add to my activity a list of fragments which implement some CardViews. These CardViews have some TextViews that I want to set from my Activity, and it works setting them on onStart or onResume events. Or no... is better to say, almost works. When there's a screen rotation (and anything that would force the restart of an Activity), trying to do anything to any fragment on onStart and onRestart results on a NullPointerException. Basically I have:
MyActivity.java
private List<MyFragment> cards = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rucardapio);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
/* Here I create 10 fragment cards */
for(int i = 0; i < 10; i++) {
cards.add(new MyFragment());
transaction.add(R.id.fragment_container, cards.get(i));
}
transaction.commit();
}
}
After some tests I figured out that the problem possibly is on if (savedInstanceState != null) { return; }. I tried these "solutions", however, which didn't helped me: NullPointerException when accessing a Fragment's textView in an Activity ,
NPE thrown when trying to Find Button ID in Android Fragment ,
How would i implement a Generic ArrayList of Fragments in Android? and much more...
Logcat:
01-24 09:07:21.848 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 43
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 46
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.888 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 48
01-24 09:07:40.088 30751-30751/io.github.mths0x5f.guiaufu E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: io.github.mths0x5f.guiaufu, PID: 30751
java.lang.NullPointerException
at io.github.mths0x5f.guiaufu.ru.MyActivity$2.run(MyActivity.java:161)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
at dalvik.system.NativeStart.main(Native Method)
Piece of code that work on cold start but not on "reload":
#Override
protected void onStart() {
super.onStart();
((TextView) cards.get(0).getView().findViewById(R.id.textView)).setText("Test");
}
I have an Intent to share a image from an view. It works, but once I canceled it to upload it on Instagram and I received an exception.
Google didn't help...
Intent
if(!getStringName().equals(" ")) {//check if it is usefull
LinearLayout table=(LinearLayout) findViewById(R.id.table_linear);
table.setDrawingCacheEnabled(true);
Bitmap bitmap = table.getDrawingCache();
String imagepath=saveBitmap(bitmap);
//sv.setDrawingCacheEnabled(false);
Intent shareImage=new Intent();
shareImage.setAction(Intent.ACTION_SEND);
shareImage.setType("image/png");
shareImage.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
shareImage.putExtra(Intent.EXTRA_TEXT, getStringName());
shareImage.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagepath)));
startActivity(shareImage);
//new RalaAlertToast(context, getString(R.string.share_success), false);
}else {
//show message, that it's not useful
}
Stack Trace
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.ralaweb.ralaprogramme.subnetztabelle/at.ralaweb.ralaprogramme.subnetztabelle.ActivityMain}: java.lang.RuntimeException: Parcel android.os.Parcel#41f7d760: Unmarshalling unknown type code 2131558402 at offset 200
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel#41f7d760: Unmarshalling unknown type code 2131558402 at offset 200
at android.os.Parcel.readValue(Parcel.java:2080)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:2363)
at android.os.Parcel.readSparseArray(Parcel.java:1735)
at android.os.Parcel.readValue(Parcel.java:2070)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2314)
at android.os.Bundle.unparcel(Bundle.java:249)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:1273)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1794)
at android.app.Activity.onRestoreInstanceState(Activity.java:948)
at android.app.Activity.performRestoreInstanceState(Activity.java:920)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
... 12 more
another thing: should I disable the drawing cache after the export?
I am able to compile, build, and install the project successfully And the project is running well in portrait, But In landscape mode, When I click in button, the app says: Unfortunately has stopped. Could you please help me figure this out?
This is The app Logcat In Landscape mode:
01-24 09:58:19.936: W/dalvikvm(6847): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-24 09:58:19.986: E/AndroidRuntime(6847): FATAL EXCEPTION: main
01-24 09:58:19.986: E/AndroidRuntime(6847): java.lang.NullPointerException
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.Divani.Marzieh.ExamActivity.addItemList(ExamActivity.java:79)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.Divani.Marzieh.ExamActivity$1.onClick(ExamActivity.java:71)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.view.View.performClick(View.java:3511)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.view.View$PerformClick.run(View.java:14105)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Handler.handleCallback(Handler.java:605)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Looper.loop(Looper.java:137)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-24 09:58:19.986: E/AndroidRuntime(6847): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 09:58:19.986: E/AndroidRuntime(6847): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-24 09:58:19.986: E/AndroidRuntime(6847): at dalvik.system.NativeStart.main(Native Method)
This is The examActivity Code:
public class ExamActivity extends Activity {
private EditText etInput1;
private EditText etInput2;
private Button btnAdd;
private ListView lvItem;
private ArrayList<Item> itemArrey;
private ArrayAdapter<Item> itemAdapter;
private static TabHost tabHost;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("LIST");
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("DETAILS");
spec2.setContent(R.id.tab2);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setCurrentTab(id.tab1);
setUpView();
}
public static TabHost getCurrentTabHost(){
return tabHost;
}
private void setUpView() {
// TODO Auto-generated method stub
etInput1 = (EditText)this.findViewById(R.id.editText1);
etInput2 = (EditText)this.findViewById(R.id.editText2);
btnAdd = (Button)this.findViewById(R.id.button1);
lvItem = (ListView)this.findViewById(R.id.list);
itemArrey = new ArrayList<Item>();
itemArrey.clear();
itemAdapter = new CustomlistActivity(this, android.R.layout.simple_list_item_1,R.id.textView1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
if (isInputValid(etInput1) && isInputValid(etInput2)) {
if(((RadioButton)findViewById(R.id.radio0)).isChecked())
itemArrey.add(new Item(R.drawable.t,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
else if(((RadioButton)findViewById(R.id.radio1)).isChecked())
itemArrey.add(new Item(R.drawable.s,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
else if(((RadioButton)findViewById(R.id.radio2)).isChecked())
itemArrey.add(new Item(R.drawable.d,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
itemAdapter.notifyDataSetChanged();
ExamActivity.getCurrentTabHost().setCurrentTab(0);
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Item");
return false;
} else {
return true;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater .inflate(R.menu.mymenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
EditText e = (EditText)findViewById(R.id.editText3);
switch (item.getItemId()) {
case R.id.item1:
Toast toast = Toast.makeText(ExamActivity.this, e.getText().toString(), 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
whenever you change the orientation of the screen the activity gets restarted after that the onStart() method is called.
Initialize your code that you are using at ExamActivity.addItemList(ExamActivity.java:79) in your onStart() method ex: your list or array
go through the site http://www.vogella.com/tutorials/AndroidLifeCycle/article.html#configurationchange
Do you have a separate layout main.xml for landscape, ie layout/main.xml and layout-land/main.xml ? If so then check that layout-land/main.xml is not missing some or all of the radio buttons radio0/1/2.
I'm trying to build a small app that takes a data file in external storage and emails it. I keep getting 'null pointer exceptions' right away in logcat and then my app dies. I can't seem to locate my exception and I am wondering if there is a way to determine the line of code that is causing this. I have the MainActivity class and then a class called SendData- the code is below. I'm new at this so any help would be greatly appreciated- thank you.
private static final String TAG = "MainActivity_ErrorLog";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// Create the getData intent
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
public void onStart()
{
{
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
// verify the paths
String currentDBPath = "TLC_COMMON/database.db";
String backupDBPath = "database.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists())
{
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
}
catch (Exception e)
{
// change the V below to E when complete
Log.v(TAG,"ERROR: Database file not created");
}
startActivity(intentgetData);
}
}
}
--new class
public class SendData extends Activity
{
private static final String TAG = "MainActivity";
/* Checks if external storage is available to read */
public boolean isExternalStorageReadable()
{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
return true;
}
return false;
}
/** Called when the user clicks the Send My Data button */
public SendData(View view)
{
// Send data by email
{
File root = Environment.getExternalStorageDirectory();
// verify it is saving as this file name; also path in previous class
String fileName = "database.db";
if (root.canWrite())
{
File attachment = new File(root, fileName);
Intent email = new Intent(Intent.ACTION_SENDTO);
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Exercise data");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"});
// is the Uri necessary?
email.putExtra(android.content.Intent.EXTRA_TEXT, Uri.fromFile(attachment));
// look at this VVV
startActivity(Intent.createChooser(email, "Send the data via Email"));}
else
{
// Change the V below to E when complete
Log.v(TAG, "Email send failed");
}
}
}
public void finish()
{
}
}
11-13 13:29:37.343: W/dalvikvm(3319): threadid=1: thread exiting with uncaught exception (group=0x418e3300)
11-13 13:29:37.343: E/AndroidRuntime(3319): FATAL EXCEPTION: main
11-13 13:29:37.343: E/AndroidRuntime(3319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.va.datasender/com.example.va.datasender.MainActivity}: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.os.Looper.loop(Looper.java:137)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 13:29:37.343: E/AndroidRuntime(3319): at dalvik.system.NativeStart.main(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): Caused by: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.ComponentName.<init>(ComponentName.java:75)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.Intent.<init>(Intent.java:3301)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.Class.newInstanceImpl(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.Class.newInstance(Class.java:1319)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-13 13:29:37.343: E/AndroidRuntime(3319): ... 11 more
Find the lowest "Caused by" and continue down until you reach a line from your code:
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3301)
at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
The <init> means you are doing something as a class variable or inside a constructor before the Activity has a valid Context... But the specific problem is on line 36.
I would guess that you are trying to create an Intent with this. Initialize your Intent inside onCreate() instead.
Found it. Change this:
// Create the getData intent
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
to:
Intent intentgetData;
and inside onCreate() (or onStart() just before calling startActivity()) add:
intentgetData = new Intent(MainActivity.this, SendData.class);
Your problem comes from line 36 on your MainActivity. Check there for the problem.
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
The exeption from line number 36: at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36).
Since the error log also shows android.content.Intent.<init> after the MainActivity.java:36, I would check the value for the first parameter in the following line, when instantiating intentgetData:
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
Instead of having the instatiation done there, try doing it in the onStart() method just before calling startActivity(intentgetData);:
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
startActivity(intentgetData);
i cannot find the null pointer exception in my code, i was wondering if anyone could help me find what is giving an error.
code:
package com.dingle.ubat;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.Toast;
public class UltraBrightAndroidTorchActivity extends Activity {
/** Called when the activity is first created. */
PowerManager pm;
PowerManager.WakeLock wl;
public boolean flash = false;
public boolean enableFlash = false;
public boolean dimDisplay = false;
Display display = getWindowManager().getDefaultDisplay();
public int windowWidth = display.getWidth();
public int windowHeight = display.getHeight();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(flash == true && enableFlash == true){
try {
DroidLED led = new DroidLED();
led.enable(!led.isEnabled());
}
catch(Exception e) {
Toast.makeText(this, "Error interacting with LED.", Toast.LENGTH_SHORT).show();
throw new RuntimeException(e);
} }
wl.acquire();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//int tx = (int) event.getRawX();
int ty = (int) event.getRawY();
if (ty <= (windowHeight/2)){
dimDisplay = !dimDisplay;
}
if (ty >= (windowHeight/2)){
enableFlash = !enableFlash;
}
return super.onTouchEvent(event);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
wl.release();
}
#Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
wl.release();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
wl.release();
}
}
Error list:
12-10 20:40:42.224: W/dalvikvm(274): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-10 20:40:42.232: E/AndroidRuntime(274): Uncaught handler: thread main exiting due to uncaught exception
12-10 20:40:42.282: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dingle.ubat/com.dingle.ubat.UltraBrightAndroidTorchActivity}: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
12-10 20:40:42.282: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-10 20:40:42.282: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-10 20:40:42.282: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): Caused by: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274): at com.dingle.ubat.UltraBrightAndroidTorchActivity.<init>(UltraBrightAndroidTorchActivity.java:20)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.Class.newInstanceImpl(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.Class.newInstance(Class.java:1479)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
12-10 20:40:42.282: E/AndroidRuntime(274): ... 11 more
any help and criticism is greatly appreciated
code is being compiled for android 2.1. code is a basic, crappily coded torch app.
thanx
The code is bailing out on one of these two lines:
Display display = getWindowManager().getDefaultDisplay();
public int windowWidth = display.getWidth();
Either getWindowManager() is returning a null and that first initializer will fail, or getDefaultDisplay() is, and the second one will.
You should probably move these initializations to the onCreate handler. Having them at instance initialization sounds a bit risky - the activity's "environment" might not be completely set up yet at that point.
(And check the return values.)
Activity is full available only after it has been created. This line does not do the intended purpose:
Display display = getWindowManager().getDefaultDisplay();
Move this line and subsequent ones inside onCreate method.