I was trying to add a new TextView every time I click on a button, in a new activity (already created), but I encountered multiple errors such as Null Pointer Exception and Illegal State Exception too.
This is the code I used in the button activity, and it's activated when I press that button.
In the code I'm recalling the layout activity_position which is the activity I would like to add the TextView to.
RelativeLayout rl = (RelativeLayout)findViewById(R.layout.activity_position);
TextView tv = new TextView(this);
tv.setId(10);
tv.setText(addr);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
Running this code gets me only errors (on rl.addView(tv) line) and I can't figure it out why.
This is the XML of the activity where I would like to add the TextViews dynamically.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".PositionActivity" >
02-18 12:21:47.348: E/AndroidRuntime(4995): FATAL EXCEPTION: main
02-18 12:21:47.348: E/AndroidRuntime(4995): java.lang.IllegalStateException: Could not execute method of the activity
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$1.onClick(View.java:3758)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View.performClick(View.java:4377)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$PerformClick.run(View.java:18044)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Handler.handleCallback(Handler.java:725)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Looper.loop(Looper.java:137)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.app.ActivityThread.main(ActivityThread.java:5306)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-18 12:21:47.348: E/AndroidRuntime(4995): at dalvik.system.NativeStart.main(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.reflect.InvocationTargetException
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$1.onClick(View.java:3753)
02-18 12:21:47.348: E/AndroidRuntime(4995): ... 11 more
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.NullPointerException
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.example.placeholder.LocatingActivity.getCoords(LocatingActivity.java:101)
EDIT: added the LogCat
Can you help me out?
Thank you!
FULL CODE: https://drive.google.com/folderview?id=0B3uaHczyJIVQVXhMR1lyN3JnWjQ&usp=sharing
Currently you are trying to cast R.layout.activity_position to RelativeLayout. assign id to RelativeLayout then use R.id.relatv_layout to in code as:
in xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relatv_layout"
....
tools:context=".PositionActivity" >
In code use R.layout.relatv_layout to initialize rl :
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relatv_layout);
The root exception in your logcat output is in LocatingActivity, line 101. In that activity there are no OnClickListeners, so let's see if I got it right: you're trying to modify contents of an Activity from another Activity (I assume first one is under second one in the stack)?
You can't do that, ever. You can only manipulate views/layouts of the activity that's currently active (i.e. shown), all other activities are paused and may have even been destroyed.
I suggest you read this: http://developer.android.com/guide/components/activities.html along with other basic documentation on the site.
Full answer:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Test extends Activity {
private LinearLayout LL;
private RelativeLayout RL;
private Button btn;
int click_counter = 0;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_test);
RL = (RelativeLayout)findViewById(R.id.relative_layout);
btn = (Button)findViewById(R.id.btn);
LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.VERTICAL);
RL.addView(LL);
// Listener
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(getApplicationContext());
tv.setText("Click: " + ++click_counter);
LL.addView(tv);
}
});
} // onCreate()
}
and layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
Related
This question already exists:
android.view.InflateException: Binary XML file line #16: Error inflating class fragment
Closed 6 years ago.
I made an app, it works well in virtual device and real device, but today my app is throwing exceptions, maybe it is the same with some old errors in here, but I can not find my own answer for this. So, please tell me how to solve it, thanks so much
here is my logcat
FATAL EXCEPTION: main
Process: com.gvc.tvschedule, PID: 1918
android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 48 more
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f0b0068}
at android.content.res.Resources.loadDrawable(Resources.java:2073)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.<init>(TextView.java:806)
at android.widget.TextView.<init>(TextView.java:618)
... 51
here under is Layout XML: getprogram_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relativeLayoutProgram"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayoutProgram"
android:orientation="vertical"
android:layout_marginTop="40dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</ListView>
<!-- #android:id/list or #id/android:list -->
</RelativeLayout>
</RelativeLayout>
and: view_program_entry.xml
<?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="horizontal" >
<TextView
android:id="#+id/programtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="15sp"
android:paddingLeft="6sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/programtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:drawableLeft="#+id/programtime" />
<!-- android:background="#color/blue2" -->
</LinearLayout>
and Java : ProgramPickerActivity.java
package com.gvc.tvschedule;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.gvc.service.DBController;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ProgramPickerActivity extends Activity {
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
private static String titleTextFromView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.getprogram_main);
ArrayList<HashMap<String, String>> programList = controller.generalProgram(MainActivity.getNameOfChannel(), DatePickerActivity.getDate());
if (programList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), programList, R.layout.view_program_entry, new String[] {
"ptime", "ptitle" }, new int[] { R.id.programtime, R.id.programtitle });
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView titleVIew = (TextView) view.findViewById(R.id.programtitle);
titleTextFromView = titleVIew.getText().toString();
createPopupWindownForDescription(titleTextFromView);
}
});
}
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("loading...");
prgDialog.setCancelable(false);
}
private void createPopupWindownForDescription(String pTitle){
String content = controller.getDescription(pTitle);
AlertDialog.Builder builder = new AlertDialog.Builder(ProgramPickerActivity.this);
builder.setTitle("Content");
builder.setMessage(content);
builder.show();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(prgDialog!=null)
prgDialog.dismiss();
}
public static String getTitleTextFromView(){
return titleTextFromView;
}
}
I think the trouble is related with the listview, because, when no data put on listview, it is fine, but with data, it threw the errors.
I believe your current problem is
<TextView
android:id="#+id/programtitle"
...
android:drawableLeft="#+id/programtime" /> // HERE
You are referencing the other TextView but you should be referencing a Drawable. My guess is that you want to put it to the left of the other TextView?
If this is the case, the LinearLayout will lay them out from left to right by default so you just need to put them in the order that you want them to appear. Also, not a problem but since they are left to right by default, there's no need for
android:orientation="horizontal"
It's not showing when you don't have any items in your ListView because the layout is never inflated so the error is never caught.
Docs
android:id="#+id/list"
use that, you're welcome
PS: Change
ListView myList = (ListView) findViewById(android.R.id.list);
for:
ListView myList = (ListView) findViewById(R.id.list);
I'm an android newbie
I was following this tutorial Android splash screen howto, it's a little outdated, but anyways.
I just add the new layout file splash.xml into res/layout, and then the image into drawable/mdpi, then I change the main image into MainActivity.java, ie:
setContentView(R.layout.activity_main);
to
setContentView(R.layout.splash);
Being activity_main the name of the default layout, activity_main.xml, and splash the name of the other layout called splash.xml.
But application stops, says the application has unfortunately stopped, and I can't even access it.
I don't know what am I missing here, here's the interesting code on MainActivity.java:
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)
findViewById(R.id.surfaceView1);
// cameraSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(640, 480));
cameraSurfaceHolder = cameraSurfaceView.getHolder();
cameraSurfaceHolder.addCallback(this);
// cameraSurfaceHolder.setType(SurfaceHolder.
// SURFACE_TYPE_PUSH_BUFFERS);
btnCapture = (Button)findViewById(R.id.button1);
btnCapture.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
camera.takePicture(cameraShutterCallback,
cameraPictureCallbackRaw,
cameraPictureCallbackJpeg);
}
});
}
My activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/containerImg"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="1276px"
android:layout_height="745px"
android:layout_centerInParent="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/surfaceView1"
android:layout_alignLeft="#+id/surfaceView1"
android:layout_marginLeft="20px"
android:src="#drawable/mark3" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:background="#drawable/camera" />
</FrameLayout>
My splash.xml:
<?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="fill_parent">
<ImageView
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
</LinearLayout>
Any ideas?
Maybe I should use the same activity_main.xml for this purposes, not really sure...
Thanks in advance!
EDIT
Logcat:
3774-3774/com.kkoci.photo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kkoci.photo/com.kkoci.photo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.kkoci.photo.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
these are the views present in your activity_main xml file.. when you change your layout file why you are accessing these?
I am new on android and trying to develop simple calculator but this Fatal exception : main occurs please help.
I did comment my onClickListner to check but it did'nt helped at all.
package com.example.calculatorsinglescreen;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText value1,value2,myoperator,result;
Button ok;
String strvalue1,strvalue2,stroperator,strresult;
int ivalue1,ivalue2,ioperator,iresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1 = (EditText) findViewById(R.id.txtvalue1);
value2 = (EditText) findViewById(R.id.txtvalue2);
myoperator = (EditText) findViewById(R.id.txtoperator);
result = (EditText) findViewById(R.id.txtresult);
ok = (Button) findViewById(R.id.btnok);
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (stroperator.equals("+")) {
Toast msg = Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG);
msg.show();
}
}
});
}
#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);
}
}
This is my XML file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.calculatorsinglescreen.MainActivity" >
<TextView
android:id="#+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:text="Value 1" />
<TextView
android:id="#+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/value1"
android:layout_margin="20dp"
android:text="Value 2" />
<TextView
android:id="#+id/operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/value2"
android:layout_margin="20dp"
android:text="Operator" />
<EditText
android:id="#+id/txtvalue1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/value1"
android:layout_alignBaseline="#+id/value1"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtoperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/operator"
android:layout_below="#+id/txtvalue2"
android:layout_toRightOf="#+id/operator"
android:layout_alignParentRight="true"
android:ems="10" />
<EditText
android:id="#+id/txtvalue2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/value2"
android:layout_toRightOf="#+id/value2"
android:layout_below="#+id/txtvalue1"
android:layout_alignParentRight="true"
android:ems="10" />
<Button
android:id="#+id/btnok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Ok" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnok"
android:layout_margin="40dp"
android:text="Result" />
<EditText
android:id="#+id/txtresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/result"
android:layout_toRightOf="#+id/result"
android:ems="10" />
Here is my LogCat to get a clearer view :
10-18 17:51:25.468: D/AndroidRuntime(742): Shutting down VM
10-18 17:51:25.468: W/dalvikvm(742): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-18 17:51:25.488: E/AndroidRuntime(742): FATAL EXCEPTION: main
10-18 17:51:25.488: E/AndroidRuntime(742): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculatorsinglescreen/com.example.calculatorsinglescreen.MainActivity}: java.lang.NumberFormatException: unable to parse '' as integer
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.os.Looper.loop(Looper.java:123)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-18 17:51:25.488: E/AndroidRuntime(742): at dalvik.system.NativeStart.main(Native Method)
10-18 17:51:25.488: E/AndroidRuntime(742): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.Integer.parseInt(Integer.java:362)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.Integer.parseInt(Integer.java:332)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.example.calculatorsinglescreen.MainActivity.onCreate(MainActivity.java:34)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-18 17:51:25.488: E/AndroidRuntime(742): ... 11 more
this is the error
java.lang.ClassCastException: android.widget.TextView
check your line 24 and see if you cast the right widget in java -(xml), this error means incompatible cast widget lol...
if everything seems correct-(mean widgets are cast in java to with their respective class) then clean rebuild and restart..
EDIT: It worked.. you've solved it but there is a new error which is
java.lang.NumberFormatException:
its because of this on these lines
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ioperator = Integer.parseInt(stroperator);
so change these to this
Integer.valueOf(strvalue1); do that as follows
and also looking at your codes.. your string values are pullled from the edittext during oncreate which means when the app starts in oncreate before the Onresume(which is called when the app shows on the), and during oncreate the user cant flirt with your app, and going further to input values, so at the end your strings are empty when you pull from the edittext, so basically what im saying is remove these lines
strvalue1 = value1.getText().toString();
and put them in the click events ... i am lucid enough to you??..
EDIT2: OVERALL CODE
for button click
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.valueOf(strvalue1);
ivalue2 = Integer.valueOf(strvalue2);
if (stroperator.equals("+")) {
Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG).show();
}
}
});
remove ioperator = Integer.parseInt(stroperator); from the code.I think Your trying to convert the operator to Integer.
ioperator = Integer.parseInt(stroperator);
if you mean operator like this +,-,/,* , you only can convert to thr char or stay in string and do something like this:
private int plus(int ivalue1 , int ivalue2 , String operator ){
if(operator.equal("+")){
return ivalue1 + ivalue2;
}
return 0;
}
so Im trying to make an app that when you click on a button, it displays a lot of images. The problem is that when I click the button, the application crashes.
Here is a part of my XML file. It has 36 different images:
<ImageView
android:id="#+id/imageView31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh1" />
<ImageView
android:id="#+id/imageView32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh2" />
<ImageView
android:id="#+id/imageView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh3" />
...
And here is the LogCat:
02-18 22:47:00.263: E/AndroidRuntime(366): FATAL EXCEPTION: main
02-18 22:47:00.263: E/AndroidRuntime(366): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.guzi.samphelptools/com.guzi.samphelptools.Vehicles}: android.view.InflateException: Binary XML file line #81: Error inflating class <unknown>
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.os.Looper.loop(Looper.java:130)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 22:47:00.263: E/AndroidRuntime(366): at dalvik.system.NativeStart.main(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: android.view.InflateException: Binary XML file line #81: Error inflating class <unknown>
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.Activity.setContentView(Activity.java:1657)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.guzi.samphelptools.Vehicles.onCreate(Vehicles.java:14)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 11 more
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: java.lang.reflect.InvocationTargetException
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Constructor.constructNative(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 24 more
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.content.res.Res`enter code here`ources.loadDrawable(Resources.java:1709)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.widget.ImageView.<init>(ImageView.java:118)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.widget.ImageView.<init>(ImageView.java:108)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 27 more
Okay, so I found this simple listview thing, but i cant get it running.
import android.os.Bundle;
import android.app.Activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
// Array of integers points to images stored in /res/drawable-hdpi/
int[] vehs = new int[]{
R.drawable.veh1,
R.drawable.veh2,
R.drawable.veh3,
R.drawable.veh4,
R.drawable.veh5,
R.drawable.veh6,
R.drawable.veh7,
R.drawable.veh8,
R.drawable.veh9,
R.drawable.veh10,
R.drawable.veh11,
R.drawable.veh12,
R.drawable.veh13,
R.drawable.veh14,
R.drawable.veh15,
R.drawable.veh16,
R.drawable.veh17,
R.drawable.veh18,
R.drawable.veh19,
R.drawable.veh20,
R.drawable.veh21,
R.drawable.veh22,
R.drawable.veh23,
R.drawable.veh24,
R.drawable.veh25,
R.drawable.veh26,
R.drawable.veh27,
R.drawable.veh28,
R.drawable.veh29,
R.drawable.veh30,
R.drawable.veh31,
R.drawable.veh32,
R.drawable.veh33,
R.drawable.veh34,
R.drawable.veh35,
R.drawable.veh36,
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("", Integer.toString(vehs[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.vehs};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
If you could help me with this, that would be great! :D
The problem you have here is you are loading everything at the same time, I guess. You should try using a ListView and inflate each row! The way this would work is that your app will load an ImageView or two at once and the rest will be left on the side, until the user scrolls close to it. The tutorial I found for this is (watch #71 to about #87 - you can get all his videos at slidenerd.com):
http://www.youtube.com/watch?v=uic3TVp_j3M#t=0
You should also resize your bitmaps so they aren't loaded in full resolution. You don't need a 1920x1080 image for a 540*420 screen, for example. The following link shows you how to take care of this.
http://developer.android.com/training/displaying-bitmaps/index.html
So, in the end, you will have only 1 ListView, 1 ImageView and way less code and way less confusion :P
EDIT: QUICK GUIDE:
I'll walk you through the ListView method only, because I think the scaling down of an image example by Google is easy to follow.
So, first of all, you will have the following:
myActivity.xml (the main Activity where you need these images)
Single row (the layout of what a single row looks like)
MyActivity.java (The java class for your activity)
myActivity.xml will contain a ListView:
<ListView
android:id="#+id/lvList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
singleRow.xml will contain just an ImageView - you will use the same ImageView's LayoutParamaters and mirror them 36 times:
<ImageView
android:id="#+id/ivImage"
android:layout_width="50dp"
android:layout_height="85dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
MyActivity.java - where all the magic happens!
public class MyActivity extends Activity implements OnItemClickListener {
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
lv = (ListView) findViewById(R.id.lvList);
MyAdapter adapter = new MyAdapter(this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MyView holder = (MyView) view.getTag(); // This will get the tag of the item click - that is set up below
}
//Create a holder for the image that will be in an ImageView
class SingleRowData {
int img;
SingleRowData(int img) {
this.img = img;
}
}
//A reference to the ImageView that needs to be copied
private class MyView {
ImageView ivImg;
MyView(View v) {
ivImg = (ImageView) v.findViewById(R.id.ivHomePageRow);
}
}
class MyAdapter extends BaseAdapter{
ArrayList<SingleHPSRow> list;
Context context;
public MyAdapter(Context context) {
this.context = context;
list = new ArrayList<SingleRowData>();
...
int[] imgId = ...; // let's say you are getting all the images from your resources .. you will have to set this up, I'm guessing you know how to.
for(int item = 0; item < imgUrl.length; i++) {
list.add(imgId[item]);
}
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view row = convertView; //This will try to see if the row has already been loaded b4
MyView holder = null; initialize our custome View for the row
if(row == null) { // new row
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Get the LayoutInflater
row = inflater.inflate(R.layout.singleRow, parent, false); //Reference to the row layout
holder = new MyView(row);
row.setTag(holder); // Give the row a tag - the holder that contains the item
} else { // old row
holder = (MyView) row.getTag(); //Get the tag that was assigned
}
SingleRowData item = list.get(position); //Get the item at the current position
int image = item.img; //Get the imgId assigned to the current item
holder.ivImg.setImageResource(image); //Load image for the current position
return row; // Return the current row
}
}
}
Hopefull this is well explained, I copied and shortened one of my codes. Let me know if there's something you don't understand
So im parsing JSON data from a API Enabled website, It requires authentiucation by a APIKEY, My issue is that im trying to parse the data then insert it into TextViews on the UI. My current code previously used to use a HashMap and a ListView However now im trying to change it to just populate the data into the TextViews. Currently, I've started changing the code over and now im getting RunTimeExceptions with my current code. Any Help would be greatly appreciated.
Issue Resolved
public class ParseMoreDetails extends Activity {
private Context context;
private static String url = "https://api.company.com/api/systems?";
private static final String TAG_SYSTEM = "systems";
private static final String TAG_SYSTEM_ID = "system_id";
private static final String TAG_CITY = "city";
private static final String TAG_STATE = "state";
private static final String TAG_SYSTEM_NAME = "system_name";
private static final String TAG_SYSTEM_PUBLIC_NAME = "system_public_name";
private static final String TAG_STATUS = "status";
TextView textView;
TextView textView2;
TextView textView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView2);
textView2 = (TextView) findViewById(R.id.TextView01);
textView3 = (TextView) findViewById(R.id.TextView03);
new ProgressTask(ParseMoreDetails.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, ArrayList<String>> {
private ProgressDialog dialog;
ArrayList<String> arrfortextviews;
private ParseMoreDetails activity;
// private List<Message> messages;
public ProgressTask(ParseMoreDetails parseMoreDetails) {
this.activity = parseMoreDetails;
context = parseMoreDetails;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(ArrayList<String> success) {
if(arrfortextviews.size() >0){
textView.setText(success.get(0));
textView2.setText(success.get(1));
textView3.setText(success.get(2));
}
if (dialog.isShowing()) {
dialog.dismiss();
}
}
protected ArrayList<String> doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
arrfortextviews=new ArrayList<String>();
// Using APIKEY from strings.xml
String apikey = getString(R.string.apikey);
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url + "&key=" + apikey);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String vcolor = c.getString(TAG_SYSTEM_ID);
String vfuel = c.getString(TAG_CITY);
String vtread = c.getString(TAG_STATE);
arrfortextviews.add(vcolor);
arrfortextviews.add(vfuel);
arrfortextviews.add(vtread);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return arrfortextviews;
}
}
Layout
<?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="fill_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="System ID: " />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..." />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City:" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..." />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="State:" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..." />
</LinearLayout>
</FrameLayout>
</LinearLayout>
Current Errors
RuntimeException - NullPointerException
Leaked Window com.android.internal.policy.impl.PhoneWindow$DecorView
LogCat - UPDATED
05-01 10:27:47.040: E/AndroidRuntime(28236): FATAL EXCEPTION: AsyncTask #1
05-01 10:27:47.040: E/AndroidRuntime(28236): java.lang.RuntimeException: An error occured while executing doInBackground()
05-01 10:27:47.040: E/AndroidRuntime(28236): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-01 10:27:47.040: E/AndroidRuntime(28236): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.lang.Thread.run(Thread.java:856)
05-01 10:27:47.040: E/AndroidRuntime(28236): Caused by: java.lang.NullPointerException
05-01 10:27:47.040: E/AndroidRuntime(28236): at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.doInBackground(ParseMoreDetails.java:116)
05-01 10:27:47.040: E/AndroidRuntime(28236): at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.doInBackground(ParseMoreDetails.java:1)
05-01 10:27:47.040: E/AndroidRuntime(28236): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-01 10:27:47.040: E/AndroidRuntime(28236): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-01 10:27:47.040: E/AndroidRuntime(28236): ... 5 more
05-01 10:27:55.035: I/Choreographer(28236): Skipped 464 frames! The application may be doing too much work on its main thread.
05-01 10:27:55.210: E/WindowManager(28236): Activity com.jitesh.androidjsonparser.ParseMoreDetails has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#4285c818 that was originally added here
05-01 10:27:55.210: E/WindowManager(28236): android.view.WindowLeaked: Activity com.jitesh.androidjsonparser.ParseMoreDetails has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#4285c818 that was originally added here
05-01 10:27:55.210: E/WindowManager(28236): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:402)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
05-01 10:27:55.210: E/WindowManager(28236): at android.view.Window$LocalWindowManager.addView(Window.java:554)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.Dialog.show(Dialog.java:277)
05-01 10:27:55.210: E/WindowManager(28236): at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.onPreExecute(ParseMoreDetails.java:86)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.AsyncTask.execute(AsyncTask.java:534)
05-01 10:27:55.210: E/WindowManager(28236): at com.jitesh.androidjsonparser.ParseMoreDetails.onCreate(ParseMoreDetails.java:63)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.Activity.performCreate(Activity.java:5206)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 10:27:55.210: E/WindowManager(28236): at android.os.Looper.loop(Looper.java:137)
05-01 10:27:55.210: E/WindowManager(28236): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-01 10:27:55.210: E/WindowManager(28236): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 10:27:55.210: E/WindowManager(28236): at java.lang.reflect.Method.invoke(Method.java:511)
05-01 10:27:55.210: E/WindowManager(28236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-01 10:27:55.210: E/WindowManager(28236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-01 10:27:55.210: E/WindowManager(28236): at dalvik.system.NativeStart.main(Native Method)
In Here :
TextView textView = (TextView) findViewById(R.id.textView2); //<<< here
TextView textView2 = (TextView) findViewById(R.id.TextView01);//<<< here
TextView textView3 = (TextView) findViewById(R.id.TextView03);//<<< here
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
you are using findViewById before setting layout for current Activity .so you will need to move all Widgets initialize inside onCreate method of Activity after setting layout for it. Change your code as:
TextView textView,textView2,textView3; //<<<declare here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize all textViews here
textView = (TextView) findViewById(R.id.textView2);
textView2 = (TextView) findViewById(R.id.TextView01);
textView3 = (TextView) findViewById(R.id.TextView03);
......
you are having an error in doInBackround of the AsyncTask
I suspect that
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url + "&key=" + apikey);
returns null
put this line instead may be it will work
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url + "key=" + apikey);
you shouldn't handle UI elements (TextViews) from doInBackground OnPostExecute is made for that