Android Error: Unable To Start Activity - java

Here is the code I use to bring up the activity:
startActivity(new Intent(getApplicationContext(), Giveaway.class));
Here is the Activity that I am bringing up:
public class Giveaway extends Activity implements OnClickListener{
static SharedPreferences settings;
SharedPreferences.Editor prefEditor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.giveaway);
settings = getSharedPreferences("firsttime", 0);
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
facebook.setOnClickListener(this);
later.setOnClickListener(this);
dontshowagain.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.facebooklayout:
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
startActivity(new Intent("android.intent.action.VIEW", localuri));
break;
case R.id.later:
finish();
break;
case R.id.dontshowagain:
finish();
prefEditor = settings.edit();
prefEditor.putBoolean("showgiveaway", false);
prefEditor.commit();
break;
}
}
I have declared the Activity in my manifest folder:
<activity
android:name=".Giveaway"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:screenOrientation="portrait"/>
But I keep getting a java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException error. Here is my logcat:
07-24 12:43:59.082: E/AndroidRuntime(7039): FATAL EXCEPTION: main
07-24 12:43:59.082: E/AndroidRuntime(7039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brightdesign.blackops2/com.brightdesign.blackops2.Giveaway}: java.lang.NullPointerException
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.os.Looper.loop(Looper.java:123)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-24 12:43:59.082: E/AndroidRuntime(7039): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:43:59.082: E/AndroidRuntime(7039): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-24 12:43:59.082: E/AndroidRuntime(7039): at dalvik.system.NativeStart.main(Native Method)
07-24 12:43:59.082: E/AndroidRuntime(7039): Caused by: java.lang.NullPointerException
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

You need to post your layout code, but what is most likely happening is that one of these three lines is returning a null value:
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
In the debugger step through those lines and if one is null, there is your problem because as soon as you try to set the on click listener it is going to fail.

Caused by: java.lang.NullPointerException
at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)
Tells us that there is a NullPointerException in Giveaway.onCreate(), specifically Giveaway.java line 29. Odds are facebook, later, and/or dontshowagain is really null. Do you have all three of these defined in giveaway.xml?

Try this...
1.
Intent i = new Intent(Your_Activity.this, Another_Activity.class);
startActivity(i);
2. This below lines points to the class and the lines which are null.
Class:
com.brightdesign.blackops2/com.brightdesign.blackops2.Giveaway}: java.lang.NullPointerException
Lines:
Please check the below four lines, i think you are getting null value here.
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);

Related

Fragment cannot be cast to android.content.Context

I want to connect my login page to MySQL PHP but I got some error here.
This is my logcat:
807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807
java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context
at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284)
at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
and this is the error line in my loginFragment.java:
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData);
task.execute("http://10.0.3.2/mlibrary/HTML/login.php");
}
I need your help guys!
Thank you.
Replace LoginFragment.this with getActvity()
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);
Replace LoginFragment.this with getContext() :
PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);

how to set listadapter for fragment

I have two framelayout in my main.xml file. I add framelayouts to the class that extends Fragment. my main class extends FragmentActivity and this is Oncreate method of it:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm =getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=new Freg1();
Fragment f2=new Freg1();
ft.add(R.id.frame1, f);
ft.add(R.id.frame2, f2);
ft.commit();
tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
tv1=(TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
Log.i(TAG,"1");
lv1=(ListView) findViewById(R.id.listView1);
lv2=(ListView) findViewById(R.id.listView2);
Log.i(TAG,"2");
List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
Log.i(TAG,"3");
ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
Log.i(TAG,"4");
lv1.setAdapter(listAdapter);
Log.i(TAG,"5");
lv2.setAdapter(listAdapter);
Log.i(TAG,"6");
}
when i run the codes, it crashed after LOG no4. that mean setAdapter() method do not work. how can i resolve this problem?
this is my logcat resource:
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: 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 com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
thank for you that help me to resolve problem.
Your ListView lv1 is null as you can see here:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter);
Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it.

NullPointerException: LinearLayout is Null

Please have a look at the following code
private class OpenFileEvent implements OnClickListener
{
#Override
public void onClick(View arg0) {
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
// TODO Auto-generated method stub
final Dialog openFileDialog = new Dialog(VoiceNotes.this);
openFileDialog.setTitle("Open File");
openFileDialog.setContentView(R.layout.open_dialog);
//First, list all the available Files
File folder = new File(Environment.getExternalStorageDirectory()+"/Voice/Notes/");
File file = new File(folder.getAbsolutePath());
File[] fileNameList = file.listFiles();
if(fileNameList != null && fileNameList.length>0)
{
for(int i=0;i<fileNameList.length;i++)
{
//Get the sub views first
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View openThisFileView = inflater.inflate(R.layout.open_dialog_file, null);
Button openThisFileButton = (Button)openThisFileView.findViewById(R.id.open_this_file_button);
Button appendThisFileButton = (Button)openThisFileView.findViewById(R.id.append_note_this_file);
TextView openThisFileNameTxt = (TextView)openThisFileView.findViewById(R.id.open_this_file_name);
//Set the Text
openThisFileNameTxt.setText(fileNameList[i].getName());
//Set the Listeners
//Add the View
openFileDialogView.addView(openThisFileView);
}
}
//Show the Dialog
openFileDialog.show();
}
}
As soon as this code in running, I get the following error message, which is NullPointerException
11-18 16:24:20.832: E/AndroidRuntime(1019): FATAL EXCEPTION: main
11-18 16:24:20.832: E/AndroidRuntime(1019): java.lang.NullPointerException
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.x.xxx.VoiceNotes$OpenFileEvent.onClick(VoiceNotes.java:227)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.view.View.performClick(View.java:4204)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.view.View$PerformClick.run(View.java:17355)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Handler.handleCallback(Handler.java:725)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Looper.loop(Looper.java:137)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-18 16:24:20.832: E/AndroidRuntime(1019): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 16:24:20.832: E/AndroidRuntime(1019): at java.lang.reflect.Method.invoke(Method.java:511)
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-18 16:24:20.832: E/AndroidRuntime(1019): at dalvik.system.NativeStart.main(Native Method)
The error is targetting to here
openFileDialogView.addView(openThisFileView);
Since it says that LinearLayout is null, here is the XML file where that layout belongs to.
open_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/open_file_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
</LinearLayout>
This is the same layout which acts as the Content View for the Dialog.
Why I think that LinearLayout is null, and not the layout which got inflated inside the class? Because the below code also generated NullPointerException
openFileDialogView.addView(new Button(Notes.this));
Why I am getting this error?
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
Here findViewById() attempts to find the specified view in the activity's view hierarchy (as set by setContentView() but you're inflating a layout with that id only later on. findViewById() returns null and attempting to invoke a method on null causes NPE.
Try moving this line
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
after
openFileDialog.setContentView(R.layout.open_dialog);
in your code.

Java app crashes (NullPointerException)

i'm just programming a password manager but every time I run it, it crashed in cause of NullPointerException. I checked the code but i'm a new developer. Someone here who knows why? :(
Code of MainActivity.java:
package de.lennartschoch.passwordmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button login;
EditText masterpassfield;
static String masterpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
masterpass = "";
masterpassfield = (EditText) findViewById(R.id.masterpass);
if(masterpass.equals("")) {
Intent intent = new Intent(this,NewMasterpass.class);
startActivity(intent);
return;
}
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(masterpassfield.getText().toString().equals(masterpass)) {
Intent success = new Intent(view.getContext(), Passwords.class);
startActivityForResult(success, 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;
}
}
Code of NewMasterpass.java:
package de.lennartschoch.passwordmanager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NewMasterpass extends Activity {
EditText masterpasscreate;
EditText repeat;
Button finish;
TextView mistake;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_masterpass);
masterpasscreate = (EditText) findViewById(R.id.masterpasscreate);
repeat = (EditText) findViewById(R.id.repeatcreate);
finish = (Button) findViewById(R.id.finish);
mistake = (TextView) findViewById(R.id.mistake);
finish.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(!masterpasscreate.getText().toString().equals(repeat.getText().toString())) {
mistake.setText("The passwords are different.");
mistake.setTextColor(Color.RED);
}
else if(masterpasscreate.getText().toString().equals("") || repeat.getText().toString().equals("")) {
mistake.setText("Both textfields must be filled in");
mistake.setTextColor(Color.RED);
}
else {
MainActivity.masterpass = masterpasscreate.getText().toString();
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
}});
}
}
LogCat error:
01-20 17:17:52.629: D/AndroidRuntime(561): Shutting down VM
01-20 17:17:52.629: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:17:52.639: E/AndroidRuntime(561): FATAL EXCEPTION: main
01-20 17:17:52.639: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivit y}: java.lang.NullPointerException
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.os.Looper.loop(Looper.java:137)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:17:52.639: E/AndroidRuntime(561): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:17:52.639: E/AndroidRuntime(561): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:17:52.639: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:17:52.639: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:17:52.639: E/AndroidRuntime(561): at dalvik.system.NativeStart.main(Native Method)
01-20 17:17:52.639: E/AndroidRuntime(561): Caused by: java.lang.NullPointerException
01-20 17:17:52.639: E/AndroidRuntime(561): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:26)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:17:52.639: E/AndroidRuntime(561): ... 11 more
01-20 17:17:57.129: I/Process(561): Sending signal. PID: 561 SIG: 9
01-20 17:21:37.499: D/AndroidRuntime(608): Shutting down VM
01-20 17:21:37.499: W/dalvikvm(608): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:21:37.511: E/AndroidRuntime(608): FATAL EXCEPTION: main
01-20 17:21:37.511: E/AndroidRuntime(608): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.os.Looper.loop(Looper.java:137)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:21:37.511: E/AndroidRuntime(608): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:21:37.511: E/AndroidRuntime(608): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:21:37.511: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:21:37.511: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:21:37.511: E/AndroidRuntime(608): at dalvik.system.NativeStart.main(Native Method)
01-20 17:21:37.511: E/AndroidRuntime(608): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.startActivity(Activity.java:3297)
01-20 17:21:37.511: E/AndroidRuntime(608): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:30)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:21:37.511: E/AndroidRuntime(608): ... 11 more
01-20 17:22:30.769: D/AndroidRuntime(655): Shutting down VM
01-20 17:22:30.769: W/dalvikvm(655): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:22:30.779: E/AndroidRuntime(655): FATAL EXCEPTION: main
01-20 17:22:30.779: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.os.Looper.loop(Looper.java:137)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:22:30.779: E/AndroidRuntime(655): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:22:30.779: E/AndroidRuntime(655): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:22:30.779: E/AndroidRuntime(655): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:22:30.779: E/AndroidRuntime(655): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:22:30.779: E/AndroidRuntime(655): at dalvik.system.NativeStart.main(Native Method)
01-20 17:22:30.779: E/AndroidRuntime(655): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.startActivity(Activity.java:3297)
01-20 17:22:30.779: E/AndroidRuntime(655): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:30)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:22:30.779: E/AndroidRuntime(655): ... 11 more
01-20 17:22:33.180: I/Process(655): Sending signal. PID: 655 SIG: 9
It asks:
01-20 17:22:30.779: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass};
have you declared this activity in your AndroidManifest.xml?
Have you?
Declare the NewMasterPass activity in your manifest xml as clearly mentioned in the logcat!
Try to add this to your AndroidManifest.xml
<activity android:name="de.lennartschoch.passwordmanager.NewMasterpass" />

NullPointerException using arrays

I have in one activity:
...
double []mylab=new double [100];
public void compute(){
...
double mytime=Double.parseDouble(timing.getText().toString().trim());
//fill array
for (int i=0;i<=mytime;i++){
mylab[i]=Math.exp(i);
//Arrays.fill(mylab,Math.exp(i));
}
...
i.putExtra("mylab",mylab);
startActivity(i);
}
and in the linegraph activity:
...
private double [] mylab =new double [100];
public double [] getmylab(){ return this.mylab;}
public void setmylab(double [] mylab){ this.mylab=mylab;}
...
public void onCreate(Bundle savedInstanceState){
Bundle extras=getIntent().getExtras();
double [] mylab=extras.getDoubleArray("mylab");
setmylab(mylab);
..
public Intent getIntent(Context context){
double []mylab=getmylab();
ArrayList<Double> x =new ArrayList<Double>();
ArrayList<Double> y =new ArrayList<Double>();
//fill x,y values
for (int i=0;i<=20;i++){
x.add(mytime/i);
}
for (int i=0;i<=20;i++){
y.add(mylab[i]);
}
...
I suppose the error lies where i fill the array?
-------------Logcat------------------------------
FATAL EXCEPTION: main E/AndroidRuntime(461):
java.lang.RuntimeException: Unable to start activity ComponentInfo
java.lang.NullPointerException E/AndroidRuntime(461): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(461): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(461): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(461): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(461): at
android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(461): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(461): at
android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(461): at
java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(461): at
java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(461): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(461): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(461): at dalvik.system.NativeStart.main(Native
Method) E/AndroidRuntime(461): Caused by:
java.lang.NullPointerException E/AndroidRuntime(461): at
com...LineGraph.getIntent(LineGraph.java:109) E/AndroidRuntime(461):
at com..LineGraph.onCreate(LineGraph.java:80) E/AndroidRuntime(461):
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(461): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
if myTime > the size of the myLab array you will get an ArrayIndexOutOfBounds error
double mytime=Double.parseDouble(timing.getText().toString().trim());
//fill array
for (int i=0;i<=mytime;i++)
mylab[i]=Math.exp(i);
//Arrays.fill(mylab,Math.exp(i));
}
The error occurs because you are accessing an array index which does not exist.
Most likely it's when you are filling the array.
// make sure `mytime` is less or equal than 100
for (int i=0;i<=mytime;i++){
mylab[i]=Math.exp(i);
//Arrays.fill(mylab,Math.exp(i));
}
Are you maybe calling
Intent getIntent()
on a newly created instance of the class? Because that might operate on a mylab that is null since the field is initialized as null at creation of each instance.
My mistake!
I had another activity which was on the middle!I had totally forgotten that!I couldn't see it!
Sorry!
Thank you all for your help and especially L7ColWinters :).

Categories