NullPointer exception when using ListView - android

I've got following code, which gets launched as a second activity:
public class SensorActivity extends Activity implements SensorEventListener{
List<Sensor> sensors;
Sensor selectedSens;
SensorManager SensMng;
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
final ListView listSensors = (ListView) findViewById(R.id.listvalues);
SensMng = (SensorManager) getSystemService(SENSOR_SERVICE);
sensors = SensMng.getSensorList(Sensor.TYPE_ALL);
ArrayAdapter<Sensor> adapter = new ArrayAdapter<Sensor>(this, android.R.layout.simple_list_item_1, sensors);
listSensors.setAdapter(adapter);
}
Here's my XML file:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listvalues"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I keep on getting a nullpointer exception:
10-03 21:46:57.475: E/AndroidRuntime(9893): FATAL EXCEPTION: main
10-03 21:46:57.475: E/AndroidRuntime(9893): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ch.ethz.inf.vs.android.spurra.sensors/ch.ethz.inf.vs.android.spurra.sensors.SensorActivity}: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.os.Looper.loop(Looper.java:137)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.main(ActivityThread.java:4898)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.reflect.Method.invoke(Method.java:511)
10-03 21:46:57.475: E/AndroidRuntime(9893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-03 21:46:57.475: E/AndroidRuntime(9893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-03 21:46:57.475: E/AndroidRuntime(9893): at dalvik.system.NativeStart.main(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): **Caused by**: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.Activity.findViewById(Activity.java:1882)
10-03 21:46:57.475: E/AndroidRuntime(9893): at ch.ethz.inf.vs.android.spurra.sensors.SensorActivity.<init>(SensorActivity.java:24)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.Class.newInstanceImpl(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.Class.newInstance(Class.java:1319)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
10-03 21:46:57.475: E/AndroidRuntime(9893): ... 11 more
All I did was really just copy past from my main activity, plus adjusting the values. The main activity works without any problems, this one however doesn't. I've tried cleaning, rebuilding the project, to no avail.
Can anyone please tell me whats wrong?
Thanks,
Regards

I've declared " final ListView listVals = (ListView) findViewById(R.id.listvalues);" as a field, so before running setContentView(). Once I've moved it into the actual function, it worked. So I assume that was causing it to return null?
Yes. If you call findViewById(R.id.someId); before calling setContentView(R.layout.someLayout); it will return null because you haven't yet inflated your layout which contains the View id you are trying to find.
Basically, your Views exist inside of your layout so if you try to use findViwById() before you inflate your layout (usually with setContentView()) then there is nothing to find. So you will get NPE when you try to use like when you call a function on it.

If your project has external jar files, go to Project->properties->order and export and select all external jar files. That should do the work.

Related

Android custom dialog null exception

line 102 and 104 null exception
i think imageView and textView id have problems.
Layout:
activity_main.xml
and custom_view.xml -- there are imageView and textView.
code:
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case 0:
dialog = new Dialog(this);
dialog.setContentView(R.layout.activity_main);
dialog.setTitle("Custom Dialog");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
//102: image.setImageResource(R.drawable.android);
TextView text = (TextView)dialog.findViewById(R.id.text);
//104: text.setText(");
break;
}
return dialog;
}
Log:
10-03 02:41:00.929: D/gralloc_goldfish(4372): Emulator without GPU emulation detected.
10-03 02:47:07.159: D/gralloc_goldfish(4415): Emulator without GPU emulation detected.
10-03 02:47:16.529: E/InputEventReceiver(4415): Exception dispatching input event.
10-03 02:47:16.529: E/MessageQueue-JNI(4415): Exception in MessageQueue callback: handleReceiveCallback
10-03 02:47:17.109: E/MessageQueue-JNI(4415): java.lang.NullPointerException
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at com.example.hw3_new.MainActivity.onCreateDialog(MainActivity.java:102)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.app.Activity.onCreateDialog(Activity.java:2989)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.app.Activity.createDialog(Activity.java:976)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.app.Activity.showDialog(Activity.java:3082)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.app.Activity.showDialog(Activity.java:3041)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at com.example.hw3_new.MainActivity$CustomView.onTouchEvent(MainActivity.java:64)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.View.dispatchTouchEvent(View.java:7384)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.View.dispatchPointerEvent(View.java:7564)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.os.MessageQueue.nativePollOnce(Native Method)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.os.MessageQueue.next(MessageQueue.java:132)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.os.Looper.loop(Looper.java:124)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at java.lang.reflect.Method.invoke(Method.java:525)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-03 02:47:17.109: E/MessageQueue-JNI(4415): at dalvik.system.NativeStart.main(Native Method)
10-03 02:47:17.140: D/AndroidRuntime(4415): Shutting down VM
10-03 02:47:17.140: W/dalvikvm(4415): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-03 02:47:17.550: E/AndroidRuntime(4415): FATAL EXCEPTION: main
10-03 02:47:17.550: E/AndroidRuntime(4415): java.lang.NullPointerException
10-03 02:47:17.550: E/AndroidRuntime(4415): at com.example.hw3_new.MainActivity.onCreateDialog(MainActivity.java:102)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.app.Activity.onCreateDialog(Activity.java:2989)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.app.Activity.createDialog(Activity.java:976)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.app.Activity.showDialog(Activity.java:3082)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.app.Activity.showDialog(Activity.java:3041)
10-03 02:47:17.550: E/AndroidRuntime(4415): at com.example.hw3_new.MainActivity$CustomView.onTouchEvent(MainActivity.java:64)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.View.dispatchTouchEvent(View.java:7384)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
10-03 02:47:17.550: E/AndroidRuntime(4415): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
10-03 02:47:17.550: E/AndroidRuntime(4415): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
10-03 02:47:17.550: E/AndroidRuntime(4415): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.View.dispatchPointerEvent(View.java:7564)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.os.MessageQueue.nativePollOnce(Native Method)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.os.MessageQueue.next(MessageQueue.java:132)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.os.Looper.loop(Looper.java:124)
10-03 02:47:17.550: E/AndroidRuntime(4415): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-03 02:47:17.550: E/AndroidRuntime(4415): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 02:47:17.550: E/AndroidRuntime(4415): at java.lang.reflect.Method.invoke(Method.java:525)
10-03 02:47:17.550: E/AndroidRuntime(4415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-03 02:47:17.550: E/AndroidRuntime(4415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-03 02:47:17.550: E/AndroidRuntime(4415): at dalvik.system.NativeStart.main(Native Method)
10-03 02:47:17.769: D/dalvikvm(4415): GC_FOR_ALLOC freed 348K, 16% free 2671K/3176K, paused 110ms, total 120ms
10-03 02:47:21.018: I/Process(4415): Sending signal. PID: 4415 SIG: 9
Add the following code in your activity_main.xml -
<include layout="#layout/custom_view"/>
Hope its work.

Android fragments backward compatibility

I am trying to do a android application where we need to use the fragments. I am following the rule while i am doing with backward compatibility. i.e. Extends FragmentActivity instead of Activity and i use the use the getSupportFragmentManager() also. The same code working with 4.1 but not working with 2.2 .
i using the
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
String newTime = String.valueOf(System.currentTimeMillis());
DetailFragment fragment = (DetailFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(newTime);
} else {
Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
intent.putExtra("value", newTime);
startActivity(intent);
Can any one help ? i am getting the force close. that unable start the activity component info.
it's showing error at setContentView() main activity ,which contains the framents .
<?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="horizontal" >
<fragment
android:id="#+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.fragmentsample.MyListFragment" ></fragment>
<fragment
android:id="#+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.fragmentsample.DetailFragment" >
<!-- Preview: layout=#layout/details -->
</fragment>
</LinearLayout>
logcat:
10-03 16:03:54.586: E/AndroidRuntime(649): FATAL EXCEPTION: main
10-03 16:03:54.586: E/AndroidRuntime(649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentsample/com.example.fragmentsample.RssfeedActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.os.Looper.loop(Looper.java:123)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 16:03:54.586: E/AndroidRuntime(649): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 16:03:54.586: E/AndroidRuntime(649): at java.lang.reflect.Method.invoke(Method.java:507)
10-03 16:03:54.586: E/AndroidRuntime(649): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 16:03:54.586: E/AndroidRuntime(649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 16:03:54.586: E/AndroidRuntime(649): at dalvik.system.NativeStart.main(Native Method)
10-03 16:03:54.586: E/AndroidRuntime(649): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
10-03 16:03:54.586: E/AndroidRuntime(649): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3692)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1400)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1326)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:47)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.view.LayoutInflater.rInflate(LayoutInflater.java:625)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-03 16:03:54.586: E/AndroidRuntime(649): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.Activity.setContentView(Activity.java:1657)
10-03 16:03:54.586: E/AndroidRuntime(649): at com.example.fragmentsample.RssfeedActivity.onCreate(RssfeedActivity.java:10)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-03 16:03:54.586: E/AndroidRuntime(649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-03 16:03:54.586: E/AndroidRuntime(649): ... 11 more
In your XML file you need to supply the full path to Fragment:
<android.support.v4.app.Fragment
android:id="#+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.fragmentsample.DetailFragment" >
<!-- Preview: layout=#layout/details -->
</android.support.v4.app.Fragment>

Custom Pinch To Zoom View

I am trying to implement Pinch To Zoom on an Android app I am working on. For this, I have made a Java Class called PinchToZoom using the Android developer's website. However, if I try to launch my app I get an error in my XML file. I am confused as to why though, because I thought I had implemented it correctly. This is the XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#222222"
android:orientation="vertical" >
<com.andriesse.athena.PinchToZoom
android:id="#+id/selectedImage"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/content" />
</LinearLayout>
I hope you guys can help me out!
EDIT:
I forgot to add the error log before, so here it is:
10-03 13:35:40.207: E/AndroidRuntime(2421): FATAL EXCEPTION: main
10-03 13:35:40.207: E/AndroidRuntime(2421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andriesse.athena/com.andriesse.athena.ImageEditing}: android.view.InflateException: Binary XML file line #8: Error inflating class com.andriesse.athena.PinchToZoom
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.os.Looper.loop(Looper.java:137)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.reflect.Method.invoke(Method.java:511)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-03 13:35:40.207: E/AndroidRuntime(2421): at dalvik.system.NativeStart.main(Native Method)
10-03 13:35:40.207: E/AndroidRuntime(2421): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.andriesse.athena.PinchToZoom
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.createView(LayoutInflater.java:589)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.Activity.setContentView(Activity.java:1835)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.andriesse.athena.ImageEditing.onCreate(ImageEditing.java:26)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.Activity.performCreate(Activity.java:4465)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-03 13:35:40.207: E/AndroidRuntime(2421): ... 11 more
10-03 13:35:40.207: E/AndroidRuntime(2421): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.Class.getConstructor(Class.java:431)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
10-03 13:35:40.207: E/AndroidRuntime(2421): ... 22 more
This is the key line:
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
Make sure that your custom listview has these constructors:
public PinchToZoom(Context context) {
this(context, null);
}
public PinchToZoom(Context context, AttributeSet attrs) { // <--- missing?
this(context, attrs, 0);
}
public PinchToZoom(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// initialisation code here
}
I've chained them this way so you never have to do anything except in the 3rd constructor. If you have these constructors, and they don't have external dependencies that can only be resolved at runtime, your custom listview will also preview correctly.
Instead of creating an xml node of type com.andriesse.athena.PinchToZoom, try creating one of type view and giving it a class attribute of class= "com.andriesse.athena.PinchToZoom", like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#222222"
android:orientation="vertical" >
<view
class="com.andriesse.athena.PinchToZoom"
android:id="#+id/selectedImage"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/content" />
</LinearLayout>
EDIT: also, instead of writing your own pinch-zoom code, you may want to try using this: https://github.com/jasonpolites/gesture-imageview
I used it on a previous project and it worked great for me.

Android httpclient calling restfull web service

according to following url I have created a requesthelper for calling a restfull webservice(.Net).
http://as400samplecode.blogspot.com/2011/09/android-asynctask-httpclient-with.html
every time I call it from Android device it give error. in one of the topic at stackoverflow I suggest add '/' character at the end of the URL. You can find my url formats and given errors as listed below.
http://domain.com/default.svc/start/4ef8e420-e6a2-49f5-b612-c0b2dced0920/
at web browser it returns "End point not found".
from the Android Log :
10-03 18:02:07.373: WARN/HTTP1:(3983): Not Found
10-03 18:02:07.383: WARN/HTTP3:(3983): java.io.IOException: Not Found
10-03 18:02:07.383: WARN/HTTP3:(3983): at requestHelper.doInBackground(requestHelper.java:84)
10-03 18:02:07.383: WARN/HTTP3:(3983): at requestHelper.doInBackground(requestHelper.java:1)
10-03 18:02:07.383: WARN/HTTP3:(3983): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-03 18:02:07.383: WARN/HTTP3:(3983): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-03 18:02:07.383: WARN/HTTP3:(3983): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-03 18:02:07.383: WARN/HTTP3:(3983): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-03 18:02:07.383: WARN/HTTP3:(3983): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-03 18:02:07.383: WARN/HTTP3:(3983): at java.lang.Thread.run(Thread.java:1096)
http://domain.com/default.svc/start/4ef8e420-e6a2-49f5-b612-c0b2dced0920
at web browser it returns a JSONObject as I wanted.
from the Android Log :
10-03 18:03:41.683: WARN/HTTP1:(4023): Method Not Allowed
10-03 18:03:41.683: WARN/HTTP3:(4023): java.io.IOException: Method Not Allowed
10-03 18:03:41.683: WARN/HTTP3:(4023): at requestHelper.doInBackground(requestHelper.java:84)
10-03 18:03:41.683: WARN/HTTP3:(4023): at requestHelper.doInBackground(requestHelper.java:1)
10-03 18:03:41.683: WARN/HTTP3:(4023): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-03 18:03:41.683: WARN/HTTP3:(4023): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-03 18:03:41.683: WARN/HTTP3:(4023): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-03 18:03:41.683: WARN/HTTP3:(4023): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-03 18:03:41.683: WARN/HTTP3:(4023): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-03 18:03:41.683: WARN/HTTP3:(4023): at java.lang.Thread.run(Thread.java:1096)

Starting the InCallScreen activity

I am doing an application which utilize the incoming call screen of Android with the follow code:
Intent inCallIntent = new Intent();
inCallIntent.setClassName("com.android.phone", "com.android.phone.InCallScreen");
startActivity(inCallIntent);
But I received the follow error:
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): java.lang.RuntimeException: Error receiving broadcast Intent { act=...alarmservice flg=0x4 (has extras) } in ...CallMeSoon$2#44635330
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:771)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.os.Handler.handleCallback(Handler.java:609)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.os.Looper.loop(Looper.java:123)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.app.ActivityThread.main(ActivityThread.java:4595)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at java.lang.reflect.Method.invoke(Method.java:521)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at dalvik.system.NativeStart.main(Native Method)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): Caused by: java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.android.phone/.InCallScreen } from ProcessRecord{449ae8e0 1495:....callmesoon/10099} (pid=1495, uid=10099) requires null
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.os.Parcel.readException(Parcel.java:1218)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.os.Parcel.readException(Parcel.java:1206)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1226)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.app.Activity.startActivityForResult(Activity.java:2789)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.app.Activity.startActivity(Activity.java:2895)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at ....CallMeSoon$2.onReceive(CallMeSoon.java:304)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:760)
10-03 17:23:30.802: ERROR/AndroidRuntime(1495): ... 9 more
I know that the permission from somewhere is missed. But what could be?
Updated: The permission is neither
android.permission.CALL_PHONE
nor
android.permission.CALL_PRIVILEGED
Caused by: java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.android.phone/.InCallScreen } from ProcessRecord{449ae8e0 1495:....callmesoon/10099} (pid=1495, uid=10099) requires null
this is the root cause, and it arises due to double entry of application component in the application Manifestfile. you might have declared InCallScreen activity more than one time in your manifest file.
Just double check it.!!
Add this to AndroidManifest.xml
android.permission.CALL_PHONE
or
android.permission.CALL_PRIVILEGED
Also aren't you missing the phone number to dial usually you set this in the intents data. You need to setup your Intent like this if you want to make a call or use these other Intent actions
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:+15554441212"));
startActivity(intent);

Categories

Resources