0

hi i want to scroll down automatically to a text view from a text view in the same layout but it gives me error please find out the error where i am doing wrong..here is my code. i want that when i click on the textview of id "Signandsymptoms" it scroll down directly to the id of "Signandsymptomsparagragh".

Error log:

01-28 15:22:39.929: E/AndroidRuntime(18673): FATAL EXCEPTION: main
01-28 15:22:39.929: E/AndroidRuntime(18673): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kmcpesh.shortreviewofcardiology/com.kmcpesh.shortreviewofcardiology.Atherosclerosis}: java.lang.NullPointerException
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.os.Looper.loop(Looper.java:137)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.ActivityThread.main(ActivityThread.java:5419)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at java.lang.reflect.Method.invokeNative(Native Method)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at java.lang.reflect.Method.invoke(Method.java:525)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at dalvik.system.NativeStart.main(Native Method)
01-28 15:22:39.929: E/AndroidRuntime(18673): Caused by: java.lang.NullPointerException
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.Activity.findViewById(Activity.java:1914)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at com.kmcpesh.shortreviewofcardiology.Atherosclerosis.<init>(Atherosclerosis.java:24)
E/AndroidRuntime(18673):    at java.lang.Class.newInstanceImpl(Native Method)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at java.lang.Class.newInstance(Class.java:1130)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
01-28 15:22:39.929: E/AndroidRuntime(18673):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)

.

import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class Atherosclerosis extends Activity {
ScrollView scrollView=(ScrollView)findViewById(R.id.scrollatherosclerosis);
TextView tv=(TextView)findViewById(R.id.Signandsymptompsid);
TextView tvabove=(TextView)findViewById(R.id.Signandsymptomsparagraph);
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atherosclerosis);
         ActionBar bar = getActionBar();
            bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true); 

            tv.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(1500, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (tvabove.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.atherosclerosis, menu);
        return true;
    }
      @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Take appropriate action for each action item click
            switch (item.getItemId()) {
            case R.id.id_search:
                Intent newActivity0 = new Intent(this,Search.class);     
                 startActivity(newActivity0);
                return true;
            case R.id.id_favorit:
               return true;

            default:
                return super.onOptionsItemSelected(item);
            }
        }
}

My .XML layout

<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:background="#ffffff"
    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=".Atherosclerosis"
    tools:ignore="UselessParent,ScrollViewCount" >
<ScrollView
    android:id="@+id/scrollatherosclerosis"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
     android:id="@+id/tablecontentlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#EFECCB" >
    <TextView
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:text="@string/table" />
     <TextView
         android:id="@+id/definitionid"
         android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Definition" />
     <TextView
         android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/RiskFactors" />
      <TextView
          android:id="@+id/pathophysiology_id"
          android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Pathophysiology" />
      <TextView
          android:id="@+id/Signandsymptompsid"
          android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/SignsandSymptoms" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Diagnosis" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Treatment" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Prevention" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Complications" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_below="@+id/tablecontentlayout"
    android:layout_marginTop="15dp"
    android:orientation="vertical" >
        <TextView
            android:id="@+id/atherodefinition" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Definition"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/atherosclerosisdefinition"
            android:textSize="15sp"/>
          <TextView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/RiskFactors"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Modifiable"
            android:textStyle="bold"
            android:textSize="15sp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/modifiableatherosclerosis"
            android:textSize="15sp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Nonmodifiable"
            android:textStyle="bold"
            android:textSize="15sp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/nonmodifiableatherosclerosis"
            android:textSize="15sp"/>
          <TextView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Pathophysiology"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/pathophysiologyatherosclerosis"
            android:textSize="15sp"/>
        <TextView
            android:id="@+id/Signandsymptomsparagraph"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/SignsandSymptoms"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/atherosclerosissignsandsymptoms"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Diagnosis"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Diagnosisatherosclerosis"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Treatment"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lifestylemodification"
            android:textSize="15sp"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/treatmentatherosclerosis"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Prevention"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Preventionatherosclerosis"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Complications"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/complicationatherosclerosis"
            android:textSize="15sp"/>
    </LinearLayout>
    </RelativeLayout>
    </ScrollView>

</RelativeLayout>
9
  • Please post your log cat error report Jan 28, 2014 at 10:21
  • i posted it..please look at it.. Jan 28, 2014 at 10:27
  • what is the code at line 24 in Atherosclerosis.java
    – user957654
    Jan 28, 2014 at 10:36
  • sorry didn't understand the question? Jan 28, 2014 at 10:38
  • @user2592807 on which Android API you have tested ? means on which version? Jan 28, 2014 at 10:39

1 Answer 1

0

i think your problem solved if you call findViewById() in onCreate() Method like this:

private ScrollView scrollView;
private TextView tvabove;
private TextView tv;
....
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dragpanel);
    scrollView = (ScrollView)findViewById(R.id.scrollatherosclerosis);
    tv = (TextView)findViewById(R.id.Signandsymptompsid);
    tvabove = (TextView)findViewById(R.id.Signandsymptomsparagraph);
    ...
}
4
  • now it start working but now the scroll is too slow and the required textview comes in the middle i want it to be on the top? Jan 28, 2014 at 10:52
  • sorry didn't understand! you want to scroll to textview and textview comes in top off activity? Jan 28, 2014 at 10:56
  • yes exactly i want it to be on the top. and its scrolling is a little bit slow Jan 28, 2014 at 10:57
  • if you want to be fast change CountDownTimer value or remove CountDownTimer and sroll in run Method. and for textview comes up you must have layout in buttom of scrollview Jan 28, 2014 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.