thread exiting with uncaught nullpointerexception - java

This app works fine in Note 2 running 4.1 but i get a "Thread exiting with uncaught exception" and NullPointerexception in 4.2 running htc one.I assure you there are no faults in id matching(I've cross checked several times).The app basically outputs the highest resoulution image from Google.
here are the logcats
11-15 12:18:54.589: D/dalvikvm(14843): Late-enabling CheckJNI
11-15 12:18:54.700: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.700: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.710: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.710: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.750: D/TAG(14843): MainActivity has started.
11-15 12:18:54.800: D/libEGL(14843): loaded /system/lib/egl/libEGL_adreno200.so
11-15 12:18:54.800: D/libEGL(14843): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-15 12:18:54.810: D/libEGL(14843): loaded /system/lib/egl/libGLESv2_adreno200.so
11-15 12:18:54.810: I/Adreno200-EGL(14843): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051_msm8960_JB_VANILLA_CL2997615_release_AU (CL2997615)
11-15 12:18:54.810: I/Adreno200-EGL(14843): Build Date: 04/11/13 Thu
11-15 12:18:54.810: I/Adreno200-EGL(14843): Local Branch:
11-15 12:18:54.810: I/Adreno200-EGL(14843): Remote Branch: quic/mako_jb_mr1
11-15 12:18:54.810: I/Adreno200-EGL(14843): Local Patches: NONE
11-15 12:18:54.810: I/Adreno200-EGL(14843): Reconstruct Branch: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051 + NOTHING
11-15 12:18:54.850: D/OpenGLRenderer(14843): Enabling debug mode 0
11-15 12:18:58.704: D/TAG(14843): In button listener
11-15 12:18:58.704: D/TAG(14843): String has been added!!!
11-15 12:18:58.714: D/TAG(14843): Making the fragment now!!
11-15 12:18:58.714: D/TAG(14843): Arguments passed!!
11-15 12:18:58.714: D/TAG(14843): Fragment made!!
11-15 12:18:58.724: D/AndroidRuntime(14843): Shutting down VM
11-15 12:18:58.734: W/dalvikvm(14843): threadid=1: thread exiting with uncaught exception (group=0x41bba930)
11-15 12:18:58.754: E/AndroidRuntime(14843): FATAL EXCEPTION: main
11-15 12:18:58.754: E/AndroidRuntime(14843): java.lang.NullPointerException
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.makeAndAddView(ListView.java:1831)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.fillDown(ListView.java:674)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.fillFromTop(ListView.java:735)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.layoutChildren(ListView.java:1652)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.os.Handler.handleCallback(Handler.java:725)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.os.Looper.loop(Looper.java:137)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.app.ActivityThread.main(ActivityThread.java:5227)
11-15 12:18:58.754: E/AndroidRuntime(14843): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:18:58.754: E/AndroidRuntime(14843): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 12:18:58.754: E/AndroidRuntime(14843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-15 12:18:58.754: E/AndroidRuntime(14843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-15 12:18:58.754: E/AndroidRuntime(14843): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TAG","MainActivity has started.");
drawer_enter=(EditText)findViewById(R.id.drawer_enter);
list=(ListView)findViewById(R.id.left_drawer);
//image_button=(Button)findViewById(R.id.image_button);
//image_button.setOnClickListener(button);
drawer_enter.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(drawer_enter.getWindowToken(), 0);
Log.d("TAG","In button listener");
inflateString(drawer_enter.getText().toString());
MakeFrag();
return true;
}
return false;
}
});
drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
listen=new ActionBarDrawerToggle(this,drawer,R.drawable.ic_launcher,0,0){
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("History");
}
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle("Best Google Image");
}
};
drawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawer.setDrawerListener(listen);
list.setOnItemClickListener(new DrawerItemClickListener());
}
/*private OnClickListener button=new OnClickListener() {
#Override
public void onClick(View theView) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(drawer_enter.getWindowToken(), 0);
Log.d("TAG","In button listener");
//int num=inflateString(drawer_enter.getText().toString());
MakeFrag();
}
};*/
private class DrawerItemClickListener implements ListView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
MakeDrawerFrag(position);
//drawer.closeDrawer(arg0);
}
}
public void MakeFrag(){
Log.d("TAG", "Making the fragment now!!");
Fragment fragment=new ImageFragment();
Bundle args=new Bundle();
args.putString(ImageFragment.ARG_IMAGE_SEARCH,drawer_enter.getText().toString());
fragment.setArguments(args);
FragmentManager fragmentManager=getFragmentManager();
Log.d("TAG", "Arguments passed!!");
fragmentManager.beginTransaction().replace(R.id.frame_layout, fragment).addToBackStack(null).commit();
Log.d("TAG", "Fragment made!!");
}
private void inflateString(String name)
{
int i=0;
for(i=0;i<names.length;i++){
if(names[i]==null){
names[i]=name;
break;
}
}
list.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,names));
Log.d("TAG","String has been added!!!");
}
ImageFragment.java
public class ImageFragment extends Fragment{
public static final String ARG_IMAGE_SEARCH = "image_search";
public Bitmap done=null;
private String search="",newSearch="";
private ImageView photo=null;
/*public ImageFragment() {
// Empty constructor required for fragment subclasses
}*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("TAG","making the view of the Fragment!!!");
search=getArguments().getString(ARG_IMAGE_SEARCH);
int i;
for(i=0;i<search.length();i++){
if(search.charAt(i)==' '){
newSearch+="%20";
}
else{
newSearch+=search.charAt(i);
}
}
Log.d("TAG",newSearch);
new ImageLoader().execute(newSearch);
View rootView = inflater.inflate(R.layout.image_view, container, false);
photo=(ImageView) rootView.findViewById(R.id.image_view);
photo.setImageBitmap(done);
getActivity().setTitle(getArguments().getString(ARG_IMAGE_SEARCH));
return rootView;
}
private class ImageLoader extends AsyncTask<String,Void,Void>{
#Override
protected Void doInBackground(String... args) {
// TODO Auto-generated method stub
try{
Log.d("TAG", "Background thread has started!!");
URL url=new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
"v=1.0&q="+args[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
String urlData;
StringBuilder builder=new StringBuilder();
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((urlData=reader.readLine()) != null){
builder.append(urlData);
}
JSONObject first=new JSONObject(builder.toString());
int i,larg = 0;
for(i=0;i<2;i++){
if((first.getJSONObject("responseData").getJSONArray("results").getJSONObject(i).getInt("width"))>first.getJSONObject("responseData").getJSONArray("results").getJSONObject(i+1).getInt("width")){
larg=i;
}
else larg=i+1;
}
String image=first.getJSONObject("responseData").getJSONArray("results").getJSONObject(larg).getString("url");
URL imgUrl = new URL(image);
HttpURLConnection imgConnection=(HttpURLConnection)imgUrl.openConnection();
InputStream input=imgConnection.getInputStream();
done=BitmapFactory.decodeStream(input);
Log.d("TAG","Bitmap has been formed!!");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
photo.setImageBitmap(done);
}

I believe your problem is in the ImageFragment onCreate here:
search=getArguments().getString(ARG_IMAGE_SEARCH);
I believe you're getting a null pointer because you aren't getting any arguments -- you aren't getting any arguments because the Fragment isn't receiving them -- I'll show you how to fix it at the end.
Just to make sure it's not getting any arguments, do this -- Comment out everything in the fragment's onCreateView, then make it like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.image_view, container, false);
try {
search=getArguments().getString(ARG_IMAGE_SEARCH);
Log.d("onCreateView",search); // this will print out the search string to logcat
} catch (Exception e) {
e.printStackTrace(); // or throw an exception and print it to logcat
}
return rootView;
}
This will either verify that search is getting the arguments (which it probably isn't) and print our the search string, or throw an exception. If it is throwing an exception, then proceed...
To solve the problem, you first need to add this to your Fragment:
static ImageFragment newInstance(String string){
ImageFragment fragment = new ImageFragment();
Bundle args = new Bundle();
args.putString(ImageFragment.ARG_IMAGE_SEARCH, string);
fragment.setArguments(args);
return fragment;
}
Then in your activity, start a new ImageFragment, you call it like this:
Fragment newImageFrag = ImageFragment.newInstance(WHATEVER_STRING_ARGUMENT_YOU_NEED_TO_PASS);
// from here add or replace the fragment as normal
After you do these two things, when the newImageFrag gets created via the newInstance and the passed string, you will be able to call
search=getArguments().getString(ARG_IMAGE_SEARCH);
and actually get the argument. I hope I understood your question correctly, and I hope this helps.

Related

Null Pointer Exception while setting value of TextView in an Array Adapter class - Android

I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file

java.lang.runtimeexception unable to start activity componentinfo java.lang.nullpointerexception android

im making a simple calories calculator and it gives me that error, i already reviewed the code and searched here for the solution but im not able to see why is not running.
I thought it was because i had not initialized the variable, so i did it and still got the same error, maybe is something with the spinners, im new on using spinners
here is the code:
public class CaloriesCalculator extends ActionBarActivity {
EditText etAge, etWeight, etHeight;
Button btnCalculate;
TextView tvResult;
Spinner spinnerGender, spinnerActivity;
String itemGender, itemActivity;
int Height=0;
int Weight=0;
int Age=0;;
double bmr=0.0;
double tdee=0.0;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caloriescalculator);
spinnerGender=(Spinner)findViewById(R.id.spinnerGender);
spinnerActivity=(Spinner)findViewById(R.id.spinnerActivity);
etAge=(EditText)findViewById(R.id.etAge);
etWeight=(EditText)findViewById(R.id.etWeight);
etHeight=(EditText)findViewById(R.id.etHeight);
tvResult=(TextView)findViewById(R.id.tvResult);
List<String> list = new ArrayList<String>();
list.add("Male");
list.add("Female");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(dataAdapter);
List<String> list2 = new ArrayList<String>();
list.add("Sedentary");
list.add("Lightly Active");
list.add("Moderalety Active");
list.add("Very Active");
list.add("Extremely Active");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list2 );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerActivity.setAdapter(dataAdapter2);
btnCalculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinnerGender.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
spinnerActivity.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
itemGender=String.valueOf(spinnerGender.getSelectedItem());
itemActivity=String.valueOf(spinnerActivity.getSelectedItem());
if(itemGender=="Male"){
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.9;
}
}
else if(itemGender=="Female") {
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.9;
}
}
result=Double.toString(tdee);
tvResult.setText(result);
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat:
11-28 17:20:05.501: E/AndroidRuntime(1455): FATAL EXCEPTION: main
11-28 17:20:05.501: E/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Looper.loop(Looper.java:137)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:20:05.501: E/AndroidRuntime(1455): at dalvik.system.NativeStart.main(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): Caused by: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:67)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:20:05.501: E/AndroidRuntime(1455): ... 11 more
11-28 17:24:42.341: D/AndroidRuntime(1507): Shutting down VM
11-28 17:24:42.341: W/dalvikvm(1507): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-28 17:24:42.371: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-28 17:24:42.371: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Looper.loop(Looper.java:137)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:24:42.371: E/AndroidRuntime(1507): at dalvik.system.NativeStart.main(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): Caused by: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:70)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:24:42.371: E/AndroidRuntime(1507): ... 11 more
Yout btnCalculate is null, you should initialize before doing the onclickListener:
btnCalculate = (Button)findViewById(R.id.btnCalculate); //your id
Also note that you are initializing the dataAdapter2 but actually you are using dataAdapert, and list2 dont have any elements, because you declare it but u are filling always list1

How can I implent ArrayLists to a Spinner?

Help me anything is wrong about the whole thing.
I want to make a Spinner with the ArrayLists but it simply does not work!
I think it has something to do with the 'protected void onCreate' part.
I have tried so hard but I allways get the same error.
My Main Class:
package com.example.hvt;
import java.util.ArrayList;
import com.example.hvt.Klassenvertretung.Bezeichnung;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
public Spinner news, klassen;
public Button btnnews, btnklassen, btnzurueck1, btnzurueck2;
Bezeichnung bezeichnung;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> Bezeichnung = new ArrayList<String>();
String listString = "";
for (String s : Bezeichnung)
{
listString += s + "\t";
}
int listInt;
listInt = Integer.parseInt(listString);
Spinner klassen;
ArrayAdapter<String> klassenadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
klassen = (Spinner) findViewById(R.id.klassenspinner);
bezeichnung.Hinzufügen(bezeichnung);
ArrayAdapter<String> klassen_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listInt);
klassen_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
klassen.setAdapter(klassenadapter);
klassen.setSelection(0);
klassen.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
if (pos == 1){
setContentView(R.layout.activity_5aklassen);
}
if (pos <= 1 ){
Toast.makeText(parent.getContext(),
"Deine Auswahl ist: " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
An Class in the same package(this Class picks information form the Internet| it should work because I have tested it in normal JAVA)
package com.example.hvt;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Vertretungsplan {
public Vertretungsplan(String woche)
{
Woche = woche;
Einlesen(woche);
}
public String Woche;
public List<Klassenvertretung> Klassen = new ArrayList<Klassenvertretung>();
private void Hinzufügen(Klassenvertretung neuesElement)
{
Klassen.add(neuesElement);
}
private void Einlesen(String woche)
{
try
{
for (int webseite = 1; webseite < 10000; webseite++)
{
Klassenvertretung klassenvertretung = new Klassenvertretung();
String teilseite = "w000";
if (webseite < 10)
teilseite = teilseite + "0";
teilseite = teilseite + webseite;
Document doc = Jsoup.connect("http://www.hh.shuttle.de/hh/gyhr/Vertretungsplan/Vertretungsplan_Internet/" + woche + "/w/" + teilseite + ".htm").get();
Element h2 = doc.select("h2").get(0);
klassenvertretung.Bezeichnung = h2.text();
Element table = doc.select("table").get(1);
Element[] elemente = table.select("tr").toArray(new Element[0]);
for (int i = 1; i < elemente.length; i++)
{
Element[] tds = elemente[i].select("td").toArray(new Element[0]);
Vertretung vertretung = new Vertretung();
vertretung.Klasse = tds[0].text();
vertretung.Stunde = tds[1].text();
vertretung.Art = tds[2].text();
vertretung.Fach = tds[3].text();
vertretung.Raum = tds[4].text();
vertretung.stattFach = tds[5].text();
vertretung.stattRaum = tds[6].text();
vertretung.Informationen = tds[7].text();
klassenvertretung.Hinzufügen(vertretung);
}
Hinzufügen(klassenvertretung);
}
}
catch (IOException io)
{
}
finally
{
}
}
}
Another Class in the same package(this code should combine all the info too two ArrayLists (tested it in normal JAVA as well so there shouldn't be a mistake))
package com.example.hvt;
import java.util.ArrayList;
import java.util.List;
public class Klassenvertretung
{
public String Bezeichnung;
public static class Bezeichnung {
public List<Bezeichnung> Bezeichnung = new ArrayList<Bezeichnung>();
public void Hinzufügen(Bezeichnung neuesElement)
{
Bezeichnung.add(neuesElement);
}
}
public List<Vertretung> Vertretungen = new ArrayList<Vertretung>();
public void Hinzufügen(Vertretung neuesElement)
{
Vertretungen.add(neuesElement);
}
}
My last Class: (as you can see this is just a class where I defined some Object(they belong to the Class above))
package com.example.hvt;
public class Vertretung
{
public String Klasse;
public String Stunde;
public String Art;
public String Fach;
public String Raum;
public String stattFach;
public String stattRaum;
public String Informationen;
}
The Chat-Log:
11-15 00:26:26.160: I/dalvikvm(714): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
11-15 00:26:26.160: W/dalvikvm(714): VFY: unable to resolve virtual method 11377: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
11-15 00:26:26.160: D/dalvikvm(714): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:26.160: I/dalvikvm(714): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
11-15 00:26:26.160: W/dalvikvm(714): VFY: unable to resolve virtual method 11383: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
11-15 00:26:26.160: D/dalvikvm(714): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:26.170: I/dalvikvm(714): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
11-15 00:26:26.170: W/dalvikvm(714): VFY: unable to resolve virtual method 8956: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
11-15 00:26:26.170: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x000e
11-15 00:26:26.190: I/dalvikvm(714): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-15 00:26:26.190: W/dalvikvm(714): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-15 00:26:26.190: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:26.190: I/dalvikvm(714): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-15 00:26:26.190: W/dalvikvm(714): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
11-15 00:26:26.190: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:26.320: D/AndroidRuntime(714): Shutting down VM
11-15 00:26:26.320: W/dalvikvm(714): threadid=1: thread exiting with uncaught exception (group=0xb1a90d70)
11-15 00:26:26.330: E/AndroidRuntime(714): FATAL EXCEPTION: main
11-15 00:26:26.330: E/AndroidRuntime(714): Process: com.example.hvt, PID: 714
11-15 00:26:26.330: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hvt/com.example.hvt.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.os.Looper.loop(Looper.java:136)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.main(ActivityThread.java:5026)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 00:26:26.330: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 00:26:26.330: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-15 00:26:26.330: E/AndroidRuntime(714): at dalvik.system.NativeStart.main(Native Method)
11-15 00:26:26.330: E/AndroidRuntime(714): Caused by: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.Integer.invalidInt(Integer.java:137)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.Integer.parseInt(Integer.java:358)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.Integer.parseInt(Integer.java:331)
11-15 00:26:26.330: E/AndroidRuntime(714): at com.example.hvt.MainActivity.onCreate(MainActivity.java:39)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.Activity.performCreate(Activity.java:5242)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
11-15 00:26:26.330: E/AndroidRuntime(714): ... 11 more
11-15 00:26:29.320: I/Process(714): Sending signal. PID: 714 SIG: 9
11-15 00:26:38.220: I/dalvikvm(803): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
11-15 00:26:38.220: W/dalvikvm(803): VFY: unable to resolve virtual method 11377: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
11-15 00:26:38.220: D/dalvikvm(803): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:38.220: I/dalvikvm(803): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
11-15 00:26:38.220: W/dalvikvm(803): VFY: unable to resolve virtual method 11383: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
11-15 00:26:38.230: D/dalvikvm(803): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:38.250: I/dalvikvm(803): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
11-15 00:26:38.250: W/dalvikvm(803): VFY: unable to resolve virtual method 8956: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
11-15 00:26:38.250: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x000e
11-15 00:26:38.270: I/dalvikvm(803): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-15 00:26:38.270: W/dalvikvm(803): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-15 00:26:38.270: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:38.280: I/dalvikvm(803): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-15 00:26:38.280: W/dalvikvm(803): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
11-15 00:26:38.280: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:38.400: D/AndroidRuntime(803): Shutting down VM
11-15 00:26:38.400: W/dalvikvm(803): threadid=1: thread exiting with uncaught exception (group=0xb1a90d70)
11-15 00:26:38.410: E/AndroidRuntime(803): FATAL EXCEPTION: main
11-15 00:26:38.410: E/AndroidRuntime(803): Process: com.example.hvt, PID: 803
11-15 00:26:38.410: E/AndroidRuntime(803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hvt/com.example.hvt.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.os.Looper.loop(Looper.java:136)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.main(ActivityThread.java:5026)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 00:26:38.410: E/AndroidRuntime(803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 00:26:38.410: E/AndroidRuntime(803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-15 00:26:38.410: E/AndroidRuntime(803): at dalvik.system.NativeStart.main(Native Method)
11-15 00:26:38.410: E/AndroidRuntime(803): Caused by: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.Integer.invalidInt(Integer.java:137)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.Integer.parseInt(Integer.java:358)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.Integer.parseInt(Integer.java:331)
11-15 00:26:38.410: E/AndroidRuntime(803): at com.example.hvt.MainActivity.onCreate(MainActivity.java:39)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.Activity.performCreate(Activity.java:5242)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
11-15 00:26:38.410: E/AndroidRuntime(803): ... 11 more
the error is bcz of "nothing there to parse!!"
you are creating the ArrayList but not assaigning or adding any values to List at all..!!
your List is empty when it reaches the for loop: so it will return runtime exception as the Logcat shows.!
ArrayList<String> Bezeichnung = new ArrayList<String>();
// populate your List before using it, otherwise it is null/"";
// need some statements like
// Bezeichnung.add("string1");
// Bezeichnung.add("string1");
String listString = "";
for (String s : Bezeichnung)
{
listString += s + "\t";
}
also have some suggestions for you,
you have to follow Java Naming Conventions to avoid mistakes by naming conflict!
make sure you named them correct before running, it may cause generating some unexpected results, which you may not figure out easily!

java.lang.runtime Exception : unable to start component

I am learning java through a video tutorial but I am having a strange problem I create a class named OpenedClass in java and implements two classes. One is for OnClickListener and 2nd is OnCheckChangeListener but when i run the application on emulator it always give me the error for the calling OnClickListener. The code for my java class is:
package com.thenewboston.thenewboston;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class OpenedClass extends Activity implements View.OnClickListener,
OnCheckedChangeListener {
TextView question, test;
Button returnData;
RadioGroup selectionList;
String gotBread;
String setData;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
initialize();
private void initialize() {
// TODO Auto-generated method stub
question = (TextView) findViewById(R.id.tvQuestion);
test = (TextView) findViewById(R.id.tvTest);
returnData = (Button) findViewById(R.id.bResults);
selectionList = (RadioGroup) findViewById(R.id.rgAnswers);
selectionList.setOnCheckedChangeListener(this);
returnData.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent person = new Intent();
Bundle backpack = new Bundle();
backpack.putString("answer", setData);
person.putExtras(backpack);
setResult(RESULT_OK,person);
finish();
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rCrazy:
setData = "Probably Right !";
break;
case R.id.rSexy:
setData = "Definitely Right !";
break;
case R.id.rBoth:
setData = "Spot On !";
break;
}
test.setText(setData);
}
}
and this is the output for logcat:
01-05 15:59:54.605: E/AndroidRuntime(313): FATAL EXCEPTION: main
01-05 15:59:54.605: E/AndroidRuntime(313): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewboston.thenewboston/com.thenewboston.thenewboston.OpenedClass}: java.lang.NullPointerException
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.os.Looper.loop(Looper.java:123)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-05 15:59:54.605: E/AndroidRuntime(313): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 15:59:54.605: E/AndroidRuntime(313): at java.lang.reflect.Method.invoke(Method.java:507)
01-05 15:59:54.605: E/AndroidRuntime(313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-05 15:59:54.605: E/AndroidRuntime(313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-05 15:59:54.605: E/AndroidRuntime(313): at dalvik.system.NativeStart.main(Native Method)
01-05 15:59:54.605: E/AndroidRuntime(313): Caused by: java.lang.NullPointerException
01-05 15:59:54.605: E/AndroidRuntime(313): at com.thenewboston.thenewboston.OpenedClass.initialize(OpenedClass.java:44)
01-05 15:59:54.605: E/AndroidRuntime(313): at com.thenewboston.thenewboston.OpenedClass.onCreate(OpenedClass.java:25)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-05 15:59:54.605: E/AndroidRuntime(313): ... 11 more
as you guys can see that it tells me that the error is on line 44 and the line 44 in my code is
returnData.setOnClickListener(this);
I did not understand it at all when I comment this line it will show me the layout when I don't it didn't. Any idea why is this happening ?
From your comments below marcin_j post
<Button android:id="#+id/bReturn" // id is bReturn
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return" >
</Button>
Change this
returnData = (Button) findViewById(R.id.bResults);
to
returnData = (Button) findViewById(R.id.bReturn);

Twitter search method using Twitter4J

I am doing an application where u write a word, a new activity opens up then all the tweets SHOULD show up, but i'm getting an error.
I am calling the method printTweets inside the main of the new activity:
And don't worry all my credentials work, i have tried the code on a java application ( not android) and it worked fine and I was getting results.
This is my code:
public void printTweets(String q) {
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxxxx")
.setOAuthConsumerSecret("xxxxxx")
.setOAuthAccessToken("xxxxxx")
.setOAuthAccessTokenSecret("xxxxx");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Query query = new Query(q);
QueryResult result = null;
do{
try {
result = twitter.search(query);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
TextView tv=new TextView(getApplicationContext());
tv.setText("#" + tweet.getUser().getScreenName() + ":" + tweet.getText());
layout.addView(tv);
}
} while ((query = result.nextQuery()) != null);
}
This is my error:
11-15 01:07:30.890: E/AndroidRuntime(12926): FATAL EXCEPTION: main
11-15 01:07:30.890: E/AndroidRuntime(12926): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sentimentmining/com.example.sentimentmining.DisplayResults}: android.os.NetworkOnMainThreadException
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.os.Looper.loop(Looper.java:137)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-15 01:07:30.890: E/AndroidRuntime(12926): at dalvik.system.NativeStart.main(Native Method)
11-15 01:07:30.890: E/AndroidRuntime(12926): Caused by: android.os.NetworkOnMainThreadException
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection.(HttpConnection.java:71)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:133)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpResponseImpl.(HttpResponseImpl.java:34)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.TwitterImpl.get(TwitterImpl.java:1929)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.TwitterImpl.search(TwitterImpl.java:306)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.example.sentimentmining.DisplayResults.printTweets(DisplayResults.java:83)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.example.sentimentmining.DisplayResults.onCreate(DisplayResults.java:34)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.Activity.performCreate(Activity.java:4465)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-15 01:07:30.890: E/AndroidRuntime(12926): ... 11 more
If you need anything more please tell me and I will give it to you
NetworkOnMainThreadException
set this permission in manifest
<uses-permission android:name="android.permission.INTERNET" />
Use asyntask
The reason why you are getting this exception is because you are attempting to perform an expensive operation on the UI thread, which can significantly slow your app down and cause it to force close. You should wrap your code in an AsyncTask (or Thread) instead.
or
use this before doing network operations
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Categories