Parsing data makes my app crash--Why? - java

So I am making an app that takes data from a webpage as text, parses it into data structures, and then displays it in a table. The trouble is, I wrote the code that creates the data structures and places the data in a table, but now, the app crashes when I run it. Here is the relevant code:
protected void onPostExecute(String result) {
String s1 = makeReadable(result);
ArrayList <OfferRequest> al = makeStructures(s1);
makeRows(al);
}
}
public String makeReadable(String input){
String result = input.replace("~~", "[empty]");
String result2 = result.replace("~", "");
String result3 = result2.replace("<", "");
String result4 = result3.replace(">", "");
return result4;
}
public ArrayList <OfferRequest> makeStructures(String input){
OfferRequest or;
ArrayList <OfferRequest> orList = new ArrayList <OfferRequest>();
String [] items = input.split ("\n");
String [] splitItems;
for(int i = 0; i < items.length; i++){
splitItems = items[i].split("|");
int id = Integer.parseInt(splitItems[0]);
int isOffer = Integer.parseInt(splitItems[1]);
boolean isOffer2 = (isOffer != 0);
int units = Integer.parseInt(splitItems[2]);
double price = Double.parseDouble(splitItems[3]);
String currency = splitItems[4];
String created = splitItems[5];
String specs = splitItems[6];
String comment = splitItems[7];
String companyName = splitItems[8];
String category = splitItems[9];
String itemName = splitItems[10];
String user = splitItems[11];
String toBeDelivered = splitItems[12];
String lastUpdated = splitItems[13];
or = new OfferRequest(id, isOffer2, units, price, currency, created, specs, comment, companyName, category, itemName, user, toBeDelivered, lastUpdated);
orList.add(or);
}
return orList;
}
public void makeRows(ArrayList <OfferRequest> input){
TableRow t;
TextView t1, t2, t3, t4, t5, t6, t7, t8;
for (int i = 0; i < input.size(); i++){
t1 = new TextView(this);
t2 = new TextView(this);
t3 = new TextView(this);
t4 = new TextView(this);
t5 = new TextView(this);
t6 = new TextView(this);
t7 = new TextView(this);
t8 = new TextView(this);
t1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t5.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t1.setText(input.get(i).getCategory());
t2.setText(input.get(i).getItemName());
t3.setText(Integer.toString(input.get(i).getUnits()));
t4.setText(input.get(i).getCurrency());
t5.setText(Double.toString(input.get(i).getPrice()));
t6.setText(input.get(i).getUser());
t7.setText("Unknown");
t8.setText(input.get(i).getCreated());
t = new TableRow(this);
t.addView(t1);
t.addView(t2);
t.addView(t3);
t.addView(t4);
t.addView(t5);
t.addView(t6);
t.addView(t7);
t.addView(t8);
table.addView(t);
}
}
Am I just overloading the device's processor or something? The app ran, albeit slowly, when I just had it printing unformatted data in a table.
Here's the logcat stuff:
04-29 12:11:37.570: E/AndroidRuntime(1763): FATAL EXCEPTION: main
04-29 12:11:37.570: E/AndroidRuntime(1763): java.lang.NumberFormatException: Invalid int: ""
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.Integer.invalidInt(Integer.java:138)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.Integer.parseInt(Integer.java:359)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.Integer.parseInt(Integer.java:332)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity.makeStructures(MainActivity.java:263)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity.makeReadable(MainActivity.java:253)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity$DownloadWebpageTask.onPostExecute(MainActivity.java:203)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity$DownloadWebpageTask.onPostExecute(MainActivity.java:1)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.Looper.loop(Looper.java:137)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.app.ActivityThread.main(ActivityThread.java:4895)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-29 12:11:37.570: E/AndroidRuntime(1763): at dalvik.system.NativeStart.main(Native Method)

Nothing to do with 'overloading the processor', it's a data problem:
12:11:37.570: E/AndroidRuntime(1763): java.lang.NumberFormatException: Invalid int: ""
You're code will need to check for dodgy data if you want it to be robust.

Yes you are getting .. java.lang.NumberFormatException
Try like this if number is not in int format ...
Integer.parseInt(string);

Related

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 app java.lang.RuntimeException error

Hi I'm trying to build a calculator to calculate compound interest, I'm keep getting a java.lang.RuntimeException error. I've read some topics on stackoverflow and their problems seems something wrong with their AndroidManifest.xml. I didn't edit that file at all but I'm still getting this error.
Here's the code:
package com.hychentsa.compoundinterest_v001;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.lang.Math;
public class MainActivity extends Activity {
private Button btn1;
private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
edt1 = (EditText)findViewById(R.id.edt1);
edt2 = (EditText)findViewById(R.id.edt2);
edt3 = (EditText)findViewById(R.id.edt3);
edt4 = (EditText)findViewById(R.id.edt4);
edt5 = (EditText)findViewById(R.id.edt5);
edt6 = (EditText)findViewById(R.id.edt6);
btn1.setOnClickListener(btn1listener);
}
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
private Button.OnClickListener btn1listener = new Button.OnClickListener()
{
public void onClick (View v)
{
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
double interestperyear = capital * Math.pow(1 + interest, year) +
everyyear * (1 - Math.pow(1 + interest, year)) / interest * -1;
edt5.setText(String.valueOf(interestperyear));
edt6.setText(String.valueOf(interestperyear / 12));
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here's the logcat:
07-15 13:45:23.640: D/AndroidRuntime(1763): Shutting down VM
07-15 13:45:23.650: W/dalvikvm(1763): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-15 13:45:23.660: E/AndroidRuntime(1763): FATAL EXCEPTION: main
07-15 13:45:23.660: E/AndroidRuntime(1763): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hychentsa.compoundinterest_v001/com.hychentsa.compoundinterest_v001.MainActivity}: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.os.Looper.loop(Looper.java:137)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 13:45:23.660: E/AndroidRuntime(1763): at dalvik.system.NativeStart.main(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): Caused by: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.hychentsa.compoundinterest_v001.MainActivity.<init>(MainActivity.java:30)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.Class.newInstanceImpl(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.Class.newInstance(Class.java:1319)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-15 13:45:23.660: E/AndroidRuntime(1763): ... 11 more
double capital = Double.parseDouble(edt1.getText().toString());
Happens at the init of your activity, which is much before you call edt1 = (EditText)findViewById(R.id.edt1);.
You can declare your double capital ... in the implicit init of the class, but the reference to edt1 must be after onCreate is called.
Also, the actual content of edt1.getText is not relevant even at this point as it is empty, hence will also cause an Exception.
Note that declaring the variable under the onCreate method doesn't make it be called after onCreate.
I don't think you need those 4 lines al all:
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
Remove the below which is before button listener. This causes NPE. Its outside of the method. Also you have the same inside button click so you can remove the ones outside the methods.
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());

Creating a ListView with custom list items programmatically in Android - no xml list item layout

As I have seen on previously asked questions, inside the custom adapter class (say, MyAdapter extends ArrayAdapter) they always use an inflated xml list-item layout. What I am hoping to do is create everything entirely using Java and no XML...
// for example
String[] wordlist = new String[] {a, b, c};
LinearLayout list_item_layout = new LinearLayout(this);
list_item_layout.setId(5000);
TextView listText = new TextView(this);
listText.setId(5001);
listLayout.addView(listText);
ListView list = new ListView(this);
// ** QUESTION ** do I declare a programmatic .setAdapter() like this?
// ** TAKE NOTE ** I passed 'wordlist' here..
list.setAdapter(new MyAdapter(this, list_item_layout.getId(), listText.getId(), wordlist));
and then for MyAdapter...
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView; //is this the list_item_layout that I passed??
TextView tv = (TextView) v.findViewById(5001);
// ** QUESTION ** do I pass 'wordlist' again here?
tv.setText( wordlist[position] );
return v;
}
}
What happens when I run this on my device is I get the following errors...
10-08 23:11:19.775: E/AndroidRuntime(18276): FATAL EXCEPTION: main
10-08 23:11:19.775: E/AndroidRuntime(18276): android.content.res.Resources$NotFoundException: String resource ID #0x0
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.content.res.Resources.getText(Resources.java:222)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.widget.TextView.setText(TextView.java:3011)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.turista.client.TuristaClientMain.onClick(TuristaClientMain.java:113)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View.performClick(View.java:2538)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View$PerformClick.run(View.java:9152)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.handleCallback(Handler.java:587)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Looper.loop(Looper.java:123)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invoke(Method.java:507)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-08 23:11:19.775: E/AndroidRuntime(18276): at dalvik.system.NativeStart.main(Native Method)
Can anyone explain how to do this programmatically?
* EDITED * October 9, 2012
Ok, as I am still stuck in this issue I think I've made some improvements but still get error messages. The improved code is as follows..
//wordlist is a global variable
String[] wordlist = new String[] {a, b, c};
// ..
// .. inside onCreate...
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, R.layout.listitem, R.id.mLargeTextView, wordlist));
// since the ArrayAdapter class needs XML parameters to inflate, I created a dummy layout
now inside MyAdapter class..
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(Main.this);
listLayout.setLayoutParams(new LayoutParams(wrapContent, wrapContent));
listLayout.setId(5000);
TextView listText = new TextView(Main.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(wordlist[position]);
return listLayout;
}
}
As you can see, I think I have to override getView in order to display my custom view so this is what I did. Unfortunately I think I misunderstood it. Here are the errors..
10-09 09:24:10.095: E/AndroidRuntime(2517): FATAL EXCEPTION: main
10-09 09:24:10.095: E/AndroidRuntime(2517): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureScrapChild(ListView.java:1183)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureHeightOfChildren(ListView.java:1266)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.onMeasure(ListView.java:1175)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Looper.loop(Looper.java:123)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invoke(Method.java:507)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-09 09:24:10.095: E/AndroidRuntime(2517): at dalvik.system.NativeStart.main(Native Method)
ListView extends AbsListView which in turn extends AdapterView which extends view group. So all the child of ListView would be added to ViewGroup. So to set the layout param, you can
use ViewGroup.LayoutParam while setting layout param for listLayout.
Or try AbsListView.LayoutParams for setting layout param for listLayout
Please try it and let me know if it worked.
Following code is working fine..
public class MainActivity1 extends Activity {
String[] wordlist = new String[] { "a", "b", "c" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, wordlist));
setContentView(list);
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] strings) {
super(context, -1, -1, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(MainActivity1.this);
listLayout.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.WRAP_CONTENT,
AbsListView.LayoutParams.WRAP_CONTENT));
listLayout.setId(5000);
TextView listText = new TextView(MainActivity1.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(super.getItem(position));
return listLayout;
}
}
}
The problem was that by default Eclipse imports ViewGroup.LayoutParams which are incompatible with the AbsListView.LayoutParams which need to be used for setting layout param for view returned from the getView() method.
Please check and let me know how it went..
To add to the above answer, setting id with a constant number is not a good idea.
Replace, listLayout.setId(5000); with,listLayout.setId(View.generateViewId());
In case you are targeting API 16 and below, refer this.

Unable to getText from editText

public class screen2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
final Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//AInteger.parseInt(string)
System.out.println("1");
EditText et = (EditText)findViewById(R.id.editText1);
System.out.println("2");
int zipCode = Integer.parseInt(et.getText().toString());
System.out.println("3");
System.out.println(zipCode);
System.out.println("dude...5");
}
});
}
}
This code shows the errror in the logcat :
11-13 19:58:06.806: W/KeyCharacterMap(281): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-13 19:58:09.471: I/System.out(281): 1
11-13 19:58:09.471: I/System.out(281): 2
11-13 19:58:09.477: D/AndroidRuntime(281): Shutting down VM
11-13 19:58:09.477: W/dalvikvm(281): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-13 19:58:09.527: E/AndroidRuntime(281): FATAL EXCEPTION: main
11-13 19:58:09.527: E/AndroidRuntime(281): java.lang.NullPointerException
11-13 19:58:09.527: E/AndroidRuntime(281): at com.example.andtwi.screen2$1.onClick(screen2.java:23)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.view.View.performClick(View.java:2408)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.view.View$PerformClick.run(View.java:8816)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.os.Handler.handleCallback(Handler.java:587)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-13 19:58:09.527: E/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:58:09.527: E/AndroidRuntime(281): at java.lang.reflect.Method.invoke(Method.java:521)
11-13 19:58:09.527: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-13 19:58:09.527: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-13 19:58:09.527: E/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method)
Though it looks like an Null Pointer exception , i think i am doing it things exactly i found on forums. Here is code of my related xml file as well. Can anyone suggest where am i doing it wrong?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Zipcode" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="5" >
<requestFocus />
</EditText>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get movies from Flixster" />
</LinearLayout>
The code breaks at this as well :
int zipCode = Integer.parseInt(et.getText().toString());
Try this:
Button button1 = (Button)findViewById(R.id.button1);
EditText et = (EditText)findViewById(R.id.editText1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("1");
System.out.println("2");
String zipCode = et.getText().toString();
System.out.println("3");
System.out.println(zipCode);
}
}
I think,you don't need to write String zipCode = (String)et.getText().toString(); there as it is already returns a String object.
And hope,you don't miss to include setContentView(R.layout.your_xml_file); before declaring a Button and EditText.
Edit 1:
You don't seem to get problem with
int zipCode=Integer.parseInt(et.getText().toString()); there.Try this with above code modification.
You might be getting null pointer exception because you are getting String s=et.getText().toString() as null.Please check it for null before casting it to int.
Button button1 = (Button)findViewById(R.id.button1);
EditText et = (EditText)findViewById(R.id.editText1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("1");
System.out.println("2");
String check = et.getText().toString();
int zipCode=0;
if(!check.equals(""))
zipCode=Integer.parseInt(check);
System.out.println("3");
System.out.println(zipCode);
}
}
Could you post your onCreate method, as well as line 23 on its own? The most likely problem is that you neglect to call setContentView and that is why findViewById returns null.

How to solve Log cat Error? Project ImageViewExample

Hi Now trying to create folder in my gallery....normally my grid view display images in separately but I need folder in my gallery. so, I am using file class....if i run my project log cat indicate error how to solve these error? please check my coding also.....
ImageViewExample.java
package ImageViewExample.ImageViewExample;
import java.io.File;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class ImageViewExample extends Activity {
/** Called when the activity is first created. */
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
GridView imagegrid;
private int count;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_image_grid();
}
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImAdapterh(this));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
public class ImAdapterh extends BaseAdapter{
File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");
int count=dir.list().length;
String[] fileNames = dir.list();
private Context mContext;
public ImAdapterh(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
for(String bitmapFileName : fileNames)
{
if (convertView == null)
{ // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new Gallery.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" +
bitmapFileName);
System.out.println(dir);
imageView.setImageBitmap(bmp);
}else
{
imageView = (ImageView) convertView;
}
}
return imageView;
}
}}
Logcat Error:
04-29 14:56:23.011: DEBUG/AndroidRuntime(415): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-29 14:56:23.021: DEBUG/AndroidRuntime(415): CheckJNI is ON
04-29 14:56:23.402: DEBUG/AndroidRuntime(415): --- registering native functions ---
04-29 14:56:24.781: DEBUG/AndroidRuntime(415): Shutting down VM
04-29 14:56:24.791: DEBUG/dalvikvm(415): Debugger has detached; object registry had 1
entries
04-29 14:56:24.821: INFO/AndroidRuntime(415): NOTE: attach of thread 'Binder Thread
#3' failed
04-29 14:56:25.761: DEBUG/AndroidRuntime(423): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-29 14:56:25.761: DEBUG/AndroidRuntime(423): CheckJNI is ON
04-29 14:56:26.141: DEBUG/AndroidRuntime(423): --- registering native functions ---
04-29 14:56:27.552: INFO/ActivityManager(66): Starting activity: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000
cmp=ImageViewExample.ImageViewExample/.ImageViewExample }
04-29 14:56:28.182: DEBUG/AndroidRuntime(423): Shutting down VM
04-29 14:56:28.232: DEBUG/dalvikvm(423): Debugger has detached; object registry had 1
entries
04-29 14:56:28.291: INFO/ActivityManager(66): Start proc
ImageViewExample.ImageViewExample for activity
ImageViewExample.ImageViewExample/.ImageViewExample: pid=430 uid=10050 gids={}
04-29 14:56:28.312: INFO/AndroidRuntime(423): NOTE: attach of thread 'Binder Thread
#3' failed
04-29 14:56:29.641: DEBUG/AndroidRuntime(430): Shutting down VM
04-29 14:56:29.641: WARN/dalvikvm(430): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): FATAL EXCEPTION: main
04-29 14:56:29.711: ERROR/AndroidRuntime(430): java.lang.RuntimeException: Unable to
start activity
ComponentInfo{ImageViewExample.ImageViewExample/ImageViewExample.
ImageViewExample.ImageViewExample}: java.lang.NullPointerException
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.os.Looper.loop(Looper.java:123)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.app.ActivityThread.main(ActivityThread.java:4627)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
java.lang.reflect.Method.invokeNative(Native Method)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
java.lang.reflect.Method.invoke(Method.java:521)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
dalvik.system.NativeStart.main(Native Method)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): Caused by:
java.lang.NullPointerException
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
ImageViewExample.ImageViewExample.ImageViewExample$ImAdapterh.<init>
(ImageViewExample.java:73)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
ImageViewExample.ImageViewExample.ImageViewExample.init_phone_image_grid
(ImageViewExample.java:47)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
ImageViewExample.ImageViewExample.ImageViewExample.onCreate
(ImageViewExample.java:36)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): at
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2627)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): ... 11 more
04-29 14:56:29.751: WARN/ActivityManager(66): Force finishing activity
ImageViewExample.ImageViewExample/.ImageViewExample
04-29 14:56:30.292: WARN/ActivityManager(66): Activity pause timeout for
HistoryRecord{4406aab0 ImageViewExample.ImageViewExample/.ImageViewExample}
04-29 14:56:36.701: DEBUG/dalvikvm(189): GC_EXPLICIT freed 162 objects / 7136 bytes in
121ms
04-29 14:56:41.568: WARN/ActivityManager(66): Activity destroy timeout for
HistoryRecord{4406aab0 ImageViewExample.ImageViewExample/.ImageViewExample}
please help me.....
The problem is almost certainly this line:
int count=dir.list().length;
The chain of logic that demonstrates that it has to be that line is follows:
The statement that creates the File object can't throw an exception in the ImAdapterh constructor itself.
This means that dir must be non-null.
This means that dir.list() won't throw an exception in the ImAdapterh constructor.
This just leaves the dereferencing of the String[] reference to get the length value as the culprit.
Finally, the null pointer can be explained by the fact that File.list() returns null if the File (in this case dir) does not exists or doesn't denote a directory ... as per the File.list() javadoc.
Null Pointer exception in your adapter class.
File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");
int count=dir.list().length;
String[] fileNames = dir.list();
check above lines...that a value returning null.

Categories