1

Hello Guys this is the second time am facing this problem in last two days. So I guess there is some place in the code where in am going horribly wrong. Anyways what am trying to do here is simply invoke a method in another class which will display a particular text file in my activity class.

MainActivity is:

package com.example.testflashfile;

public class MainActivity extends Activity{
    Button nextButton;
    Button playButton;
    Button backButton;
    ReadText readText=new ReadText(this);
    Context contextObject;
    GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        setContentView(R.layout.activity_main);

        TextView helloTxt = (TextView)findViewById(R.id.displaytext);

        helloTxt.setText(readText.readTxt());

        gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
        View mainview = (View) findViewById(R.id.mainView);

        // Set the touch listener for the main view to be our custom gesture listener
        mainview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

        playButton=(Button)findViewById(R.id.play);
        playButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class);
                startAnimation.putExtra("SWF_NAME","a");
                startActivity(startAnimation);
            }
        });

        nextButton=(Button)findViewById(R.id.next);
        nextButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);

            }
        });

        backButton=(Button)findViewById(R.id.back);
        backButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,FifthActivity.class);
                startActivity(intent);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public class MyGestureDetector extends SimpleOnGestureListener   {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (Math.abs(e1.getY() - e2.getY()) > GlobalVariables.SWIPE_MAX_OFF_PATH) {
                return false;
            }

            // right to left swipe
            if(e1.getX() - e2.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {

                Intent i= new Intent(MainActivity.this,SecondActivity.class);
                startActivity(i);

                //  left to right  swipe
            }  else if (e2.getX() - e1.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {


                Intent i= new Intent(MainActivity.this,FifthActivity.class);
                startActivity(i);

            }

            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }
}

The method readTxt() is defined in the class below:

package com.example.testflashfile;

public class ReadText {
    Context context;
    public ReadText(Context c) {
        context = c;
        readTxt();
    }

    public String readTxt() {
        InputStream inputStream = context.getResources().openRawResource(R.raw.textone);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int i;
        try {
            i = inputStream.read();
            while (i != -1)
            {
                byteArrayOutputStream.write(i);
                i = inputStream.read();
            }

            inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return byteArrayOutputStream.toString();
    } 
}

The manifest file is :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testflashfile"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testflashfile.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.testflashfile.SecondActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.ThirdActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.FourthActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.FifthActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.ReadText"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.PlayAnimationActivity"
            android:screenOrientation="portrait" >
        </activity>
    </application>

</manifest>

The log cat :

03-13 11:01:47.792: D/AndroidRuntime(630): Shutting down VM
03-13 11:01:47.843: W/dalvikvm(630): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-13 11:01:47.972: E/AndroidRuntime(630): FATAL EXCEPTION: main
03-13 11:01:47.972: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.MainActivity}: java.lang.NullPointerException
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.os.Looper.loop(Looper.java:123)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.reflect.Method.invoke(Method.java:507)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-13 11:01:47.972: E/AndroidRuntime(630):  at dalvik.system.NativeStart.main(Native Method)
03-13 11:01:47.972: E/AndroidRuntime(630): Caused by: java.lang.NullPointerException
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.example.testflashfile.ReadText.readTxt(ReadText.java:24)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.example.testflashfile.ReadText.<init>(ReadText.java:14)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.example.testflashfile.MainActivity.<init>(MainActivity.java:26)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.Class.newInstanceImpl(Native Method)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.Class.newInstance(Class.java:1409)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-13 11:01:47.972: E/AndroidRuntime(630):  ... 11 more
03-13 11:01:52.054: I/Process(630): Sending signal. PID: 630 SIG: 9

Things I have already tried: 1. class ReadText is mentioned in the manifest. 2. checked similar threads on stack overflow in related questions.

Please Note: The problem is in line number 41:

helloTxt.setText(readText.readTxt());

Any help/Suggestions/pointers write to this question are appreciated.

Renjith
  • 5,783
  • 9
  • 31
  • 42
D'yer Mak'er
  • 1,632
  • 5
  • 24
  • 47

3 Answers3

1

Answer: (thanks to @ρяσѕρєя K )

The readText=new ReadText(this); should be inside the onCreate method of the activity.

That solved my problem. I would still like to know why defining this inside onCreate is essential.

Thanks.

D'yer Mak'er
  • 1,632
  • 5
  • 24
  • 47
0

I think context.getResources() is returning null. Is this in the context of an Activity? If not it getResources will return null.

In other words make sure that the this keyword is what you think it is.

DjHacktorReborn
  • 2,908
  • 2
  • 20
  • 29
  • Thanks for the reply @DjHacktorReborn. Well i dont think thats the issue though mentioning context in the constructor is essential for the getReasources method. and its not returning null. I got the solution from ρяσѕρєя K. Read above. – D'yer Mak'er Mar 13 '13 at 05:53
0

I think you forget to pass parameter in public ReadText(Context c) means you should pass something here as your method public String readTxt() ,it should not be blank

public class ReadText {
Context context;

public ReadText(Context c)

{
    context = c;
    readTxt();
}

public String readTxt()

{
    InputStream inputStream = context.getResources().openRawResource(R.raw.textone);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1)
        {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }

        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
} 
Kirtikumar A.
  • 4,140
  • 43
  • 43