NullPointerException using arrays - java

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 :).

Related

Android: Using getResources might be causing my app to crash

I don't know why this is causing my app to crash. I have instantiated 3 class variables to point to the color values I created in my colors.xml file. I have been experimenting, and the code that is commented out here seems to be causing the error "appName has stopped working"
protected int m_nDarkColor = R.color.dark;
//protected int m_nDarkColor = getResources().getColor(R.color.dark);
protected int m_nLightColor = R.color.light;
//protected int m_nLightColor = getResources().getColor(R.color.light);
protected int m_nTextColor = R.color.text;
//protected int m_nTextColor = getResources().getColor(R.color.text);
private boolean isDark = false; //To alternate between colors.
This is the method that is using the class variables on top. If I use the uncommented class variables in the setBackgroundColor() methods, the color is the same gray shade no matter what I change the color values to (that is why I commented those out too), so I tried setBackgroundColor(getResources()get.Color(R.color.dark) and it fixed the problem, but it made my class variables useless. I don't mean to be picky, I am just confused why when I set the class variable to point to my colors values in colors.xml, it causes my app to stop working or the smae gray color, but when I pass it to the setBackgroundColor method it works just fine.
`
protected void addJoke(String strJoke) {
android.widget.TextView display = new android.widget.TextView(this);
display.setText(strJoke); //Sets the text on display.
display.setTextSize(16); //Increases the font size of the text.
display.setTextColor(getResources().getColor(R.color.text));
//display.setTextColor(m_nTextColor);
if(!isDark)
{
display.setBackgroundColor(getResources().getColor(R.color.dark));
//display.setBackgroundColor(m_nDarkColor);
isDark = true;
}
else if(isDark)
{
display.setBackgroundColor(getResources().getColor(R.color.light));
//display.setBackgroundColor(m_nLightColor);
isDark = false;
}
m_vwJokeLayout.addView(display); //Adds the view to the layout.
}
This is all the red from LogCat
09-17 20:47:25.852: E/AndroidRuntime(11212): FATAL EXCEPTION: main
09-17 20:47:25.852: E/AndroidRuntime(11212): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.calpoly.android.lab2/edu.calpoly.android.lab2.SimpleJokeList}: java.lang.NullPointerException
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.os.Looper.loop(Looper.java:137)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.app.ActivityThread.main(ActivityThread.java:5039)
09-17 20:47:25.852: E/AndroidRuntime(11212): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 20:47:25.852: E/AndroidRuntime(11212): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 20:47:25.852: E/AndroidRuntime(11212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-17 20:47:25.852: E/AndroidRuntime(11212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-17 20:47:25.852: E/AndroidRuntime(11212): at dalvik.system.NativeStart.main(Native Method)
09-17 20:47:25.852: E/AndroidRuntime(11212): Caused by: java.lang.NullPointerException
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
09-17 20:47:25.852: E/AndroidRuntime(11212): at edu.calpoly.android.lab2.SimpleJokeList.<init>(SimpleJokeList.java:34)
09-17 20:47:25.852: E/AndroidRuntime(11212): at java.lang.Class.newInstanceImpl(Native Method)
09-17 20:47:25.852: E/AndroidRuntime(11212): at java.lang.Class.newInstance(Class.java:1319)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
09-17 20:47:25.852: E/AndroidRuntime(11212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
09-17 20:47:25.852: E/AndroidRuntime(11212): ... 11 more
Final Note: The way the code is set up now works, the colors come out right and the app runs on my device, the error is only caused when I switch the commented code with the uncommented code. This is my first question on here, so I hope it hasn't been asked before and I formatted it correctly, thanks for help!
Imagine like this: R.color.dark is like a pointer point to the value of color(Certainly not a real pointer like C language). And you use getResources().getColor(R.color.dark) method to get the real value of color.
Do a test:
Log.d("Color", "Value of \"R.color.gray\" is: " + R.color.gray);
Log.d("Color", "Value of \"getResources().getColor(R.color.gray)\" is: " + getResources().getColor(R.color.gray));
Here is the output in logCat:
09-18 11:07:17.458 32359-32359/com.ch.summerrunner D/Color﹕ Value of "R.color.gray" is: 2131099651
09-18 11:07:17.458 32359-32359/com.ch.summerrunner D/Color﹕ Value of "getResources().getColor(R.color.gray)" is: -2144128205
the value -2144128205 means the real color in Android system.
May this help you.
-----------add--------------
I understand what you mean.
Why your app crashes?
Because getResources() method return null.
But why it return null when you call it to set the class variable?
Long Story.
getResource() is a interface of Context and implement by ContextWrapper. Like this :
#Override
public Resources getResources()
{
return mBase.getResources();
}
But the Constructor of ContextThemeWrapper(Subclasses of ContextWrapper) is :
public ContextThemeWrapper() {
super(null);
}
and
public ContextThemeWrapper(Context base, int themeres) {
super(base);
mBase = base;
mThemeResource = themeres;
}
You can see that the mBase of ContextWrapper may be null.
This is the Constructor of Activity(Subclasses of ContextThemeWrapper):
public Activity ()
Activity Constructor
We did not pass a context to Activity, so when did ContextWrapper initialize mBase ?
When system create/start an Activity, system pass a mBase instance to Activity by attachBaseContext(Context base) method of Activity.
This is why you get NULL when you call getResources() to set the class variable.
You call the method too early. Call getResources() in onCreate() of later.
There may be some mistakes in this answer, if you wanna know more, read the source:
frameworks/base/core/java/android/app/ActivityThread.java
You are calling getResources() too early in the activity lifecycle.
The activity is usable as a Context only in onCreate() or later. Instance initialization (<init> in your stacktrace) is too early. Move the getResources() and such to onCreate() or later.
I added iconics to app then after remove that from application I comment the code on attachBaseContext. App started to give this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
After I remove attachBaseContext completely, everything is fine.
#Override
protected void attachBaseContext(Context newBase)
{
//super.attachBaseContext(IconicsContextWrapper.wrap(newBase));
}

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.

android app parse values through intent

I am trying to parse a value through intent while switching between activities.
I know I should read values from last intent with getExtra but I don't know why it doesn't work.
Also when I switch between activities on button click, application crashes.
In activity main I read text from editText and put it in Intent:
public void schimba(View view){
int value = Integer.parseInt(instances.getText().toString());;
Intent intent = new Intent(this, Tabel.class);
intent.putExtra("max", value);
startActivity(intent);
}
When it switch to activity 2 I have this:
Intent intentObject = getIntent();
int value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
value = intentObject.getIntExtra("max", 0);
/*
for(i=0;i<=value;i++)
{
LayoutInflater layoutinflate = null;
layoutinflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowview = layoutinflate.inflate( R.layout.activity_tabel, null);
}
*/
setContentView(R.layout.activity_tabel);
TextView showvalue;
showvalue = (TextView) findViewById(R.id.ShowValue);
showvalue.setText(""+value);
The idea is that I want to use this value in a for loop, I already know how to display the value in a textView but I don't need it, I wanna use it in for.
Logcat:
04-23 10:40:52.550: E/AndroidRuntime(1010): FATAL EXCEPTION: main
04-23 10:40:52.550: E/AndroidRuntime(1010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instances_temperature/com.example.instances_temperature.Tabel}: java.lang.NullPointerException
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.os.Looper.loop(Looper.java:123)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-23 10:40:52.550: E/AndroidRuntime(1010): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 10:40:52.550: E/AndroidRuntime(1010): at java.lang.reflect.Method.invoke(Method.java:521)
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-23 10:40:52.550: E/AndroidRuntime(1010): at dalvik.system.NativeStart.main(Native Method)
04-23 10:40:52.550: E/AndroidRuntime(1010): Caused by: java.lang.NullPointerException
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.example.instances_temperature.Tabel.onCreate(Tabel.java:26)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-23 10:40:52.550: E/AndroidRuntime(1010): ... 11 more
line 26 would be this:
value = intentObject.getIntExtra("max", 0);
You have to use the code below
int maxValue = getIntent().getExtras().getInt("max");
inside onCreate().
Hope it will solve your problem..
You have not declared intentObject...
Use this:
value = getIntent().getIntExtra("max", 0);
use this
value = getIntent().getStringExtra("max");
instead of this
value = intentObject.getIntExtra("max", 0);

Android:Runtime error

I am trying to accept a phrase and pass it to other class called Animation.And I am using a self defined class called Error to store if error happens and what word causes the error.But when the button representing the class is clicked it displays "unfortunately the application has stopped".Check out partial code of the class below. Thank you for your help in advance.
public class Convert extends Activity implements OnClickListener {
EditText inputConvert;
TextView correct;
String correction;
String[] message = getResources().getStringArray(R.array.basic_words);
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convert);
View convert = findViewById(R.id.conv_button);
convert.setOnClickListener(this);
}
public void onClick(View v)
{
inputConvert=(EditText) findViewById(R.id.inputConvert);
String phrase = inputConvert.getText().toString();
if(v.getId()==R.id.conv_button)
{
Error obj1= new Error(false ,"initial");
obj1=check(phrase);
if(obj1.flag)
startAnim(phrase);
else
{
correction = build(obj1.word);
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.error);
Button dialogButton = (Button)dialog.findViewById(R.id.dialogButtonOK);
correct= (TextView) findViewById(R.id.error_content);
correct.setText(correction);
dialog.setTitle(R.string.help);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
}
public String build(String word)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("the word \"");
stringBuilder.append(word);
stringBuilder.append(" \" is not found please try to use a synomym");
String finalString = stringBuilder.toString();
return finalString;
}
public Error check(String word)
{
Error obj2 = new Error(false ,"initial");
obj2.word=word;
String[] words = word.split(" ");
for(int i=0; i< words.length; i++)
{
for(int j=0; i< message.length; j++)
{
if(words[i]==message[j])
obj2.flag=true;
break;
}
if(!obj2.flag)
obj2.word=words[i];
break;
}
return obj2;
}
public void startAnim(String phrase)
{
Intent j = new Intent(this, Animation.class);
j.putExtra("phrase",phrase);
startActivity(j);
}
Here is the code of the Animation activity.
public class Animation extends Activity
{
AnimationDrawable animation;
TextView errorMessage= (TextView)findViewById(R.id.testex);
#SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
Intent j= getIntent();
CharSequence message = j.getStringExtra("phrase");
String phrase=message.toString();
String[] words = phrase.split(" ");
animation = new AnimationDrawable();
int imid=0;
for(int i=0; i< words.length; i++)
{
imid=getResources().getIdentifier(words[i], "drawable", getPackageName());
animation.addFrame(getResources().getDrawable(imid), 700);
}
animation.setOneShot(false);
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundDrawable(animation);
animation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
animation.start();
}
}
Logcat.
12-12 08:32:04.962: E/AndroidRuntime(2273): FATAL EXCEPTION: main
12-12 08:32:04.962: E/AndroidRuntime(2273): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.ihear/org.example.ihear.Convert}: java.lang.NullPointerException
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.os.Looper.loop(Looper.java:137)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.reflect.Method.invoke(Method.java:525)
12-12 08:32:04.962: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-12 08:32:04.962: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-12 08:32:04.962: E/AndroidRuntime(2273): at dalvik.system.NativeStart.main(Native Method)
12-12 08:32:04.962: E/AndroidRuntime(2273): Caused by: java.lang.NullPointerException
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
12-12 08:32:04.962: E/AndroidRuntime(2273): at org.example.ihear.Convert.(Convert.java:19)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.Class.newInstanceImpl(Native Method)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.Class.newInstance(Class.java:1130)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-12 08:32:04.962: E/AndroidRuntime(2273): ... 11 more
Caused by: java.lang.NullPointerException
at org.example.ihear.Convert.(Convert.java:19)
here is your real error.please check it.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo:{....}
means that Android OS could not create your activity.
The most probable error is that you forgot to add it to your AndroidManifest. I suspect that it is related to the Animation activity.
Try to declare
<activity android:name="**YOUR.PACKAGE**.Animation" />
See : java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
If the declaration of the activity in the manifest was not the problem, try to inspect the constructor / onCreate methods of your Animation activity.
R.layout.convert is this layout right?
You can check it and make sure you have not used any element can not be found

Android Error: Unable To Start Activity

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);

Categories