Android Development Tutorial-22 Problem

0 Paramjeet SIngh Nigam · June 20, 2015
I completed the task given by Bucky in tutorial 22 but the problem starts when I give a background color to the screen.
It gives me the error "Unfortunately the app has stoppped".
Pls note that this issue comes only when I write the code for background color for my app.Otherwise the app works fine.
I am attaching the java code along with the logcat.
Java code:

package com.example.paramjeet.gesturech;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import android.view.View;
import android.widget.Button;
import android.graphics.Color;
import android.widget.TextView;
import android.widget.RelativeLayout;

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{

private TextView message;
private GestureDetectorCompat gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RelativeLayout applayout = new RelativeLayout(this);
applayout.setBackgroundColor(Color.CYAN);
setContentView(applayout);




Button button1=(Button)findViewById(R.id.button1); //reference created
button1.setOnClickListener( //event and callback
new Button.OnClickListener(){
public void onClick(View v){
TextView text1=(TextView)findViewById(R.id.message); //text reference created
text1.setText("This is it!");
}
}
);
// A reference to the message
message = (TextView)findViewById(R.id.message);
// A object from this class that allows us to detect gestures
this.gestureDetector=new GestureDetectorCompat(this,this);
// Allows to detect double tabs
gestureDetector.setOnDoubleTapListener(this);


}
////gesture starts
@Override
public boolean onDoubleTap(MotionEvent e) {
message.setText("onDoubleTap");
return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
message.setText("onSingleTapConfirmed");
return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
message.setText("onDoubleTapEvent");
return true;
}

@Override
public boolean onDown(MotionEvent e) {
message.setText("onDown");
return true;
}

@Override
public void onShowPress(MotionEvent e) {
message.setText("onShowPress");

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
message.setText("onSingleTapUp");
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
message.setText("onScroll");
return true;
}

@Override
public void onLongPress(MotionEvent e) {
message.setText("onLongPress");

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
message.setText("onFling");
return true;
}
//////Gesture ends


@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

Hers's the logcat :-

06-20 14:35:34.552  17493-17493/? W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0x40c331f8)
06-20 14:35:34.552  17493-17493/? E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paramjeet.gesturech/com.example.paramjeet.gesturech.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1976)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2001)
            at android.app.ActivityThread.access$600(ActivityThread.java:129)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1153)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4536)
            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:791)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.paramjeet.gesturech.MainActivity.onCreate(MainActivity.java:35)
            at android.app.Activity.performCreate(Activity.java:4648)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2001)
            at android.app.ActivityThread.access$600(ActivityThread.java:129)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1153)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4536)
            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:791)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
            at dalvik.system.NativeStart.main(Native Method)
06-20 14:35:34.560      533-533/? I/[LGHome]EVENT? [LauncherModel.java:1115:onReceive()]onReceive intent=Intent { act=android.search.action.SEARCHABLES_CHANGED flg=0x20000010 }
06-20 14:35:34.568      288-995/? W/ActivityManager? Force finishing activity com.example.paramjeet.gesturech/.MainActivity
06-20 14:35:34.638      533-533/? D/OpenGLRenderer? Flushing caches (mode 0)
06-20 14:35:34.661  15801-16211/? D/PicasaSyncManager? reject MetadataSyncTask (*****************) because master auto sync is off
06-20 14:35:34.661  15801-16211/? D/PicasaSyncManager? reject PrefetchFullImage (*****************) because downsync is disabled
06-20 14:35:34.685  15801-16211/? D/PicasaSyncManager? reject PrefetchAlbumCover (*****************) because master auto sync is off
06-20 14:35:34.700  15801-16211/? D/PicasaSyncManager? reject PrefetchScreenNail (*****************) because master auto sync is off
06-20 14:35:34.732      143-231/? D/SurfaceFlinger? createSurface for (1 x 1) Name=Note
06-20 14:35:34.755  16187-16187/? W/AppBoxStateReceiver? [[SelfUpdateHelper]] onReceive.. [android.intent.action.PACKAGE_REPLACED]
06-20 14:35:34.763  16199-16199/? D/appbox2? [PreloadListManagerHelper] onReceive com.example.paramjeet.gesturech
06-20 14:35:34.763  16199-16199/? D/appbox2? [PreloadListManagerHelper] action android.intent.action.PACKAGE_REPLACED
06-20 14:35:34.763  16199-16199/? D/appbox2? [PreloadListManagerHelper] REPLACING true DATA_REMOVEDfalse
06-20 14:35:34.786      288-302/? I/dalvikvm-heap? Grow heap (frag case) to 27.705MB for 326608-byte allocation
06-20 14:35:34.833  14589-17516/? V/ConfigFetchTask? ConfigFetchTask getDeviceDataVersionInfo(): ABFEt1VOs27tZ2m9_sGeEHsmVyZPH08EvUMeuoLRetS387YuLnn0bwBLnnkbDRwWtwBGPG_ePG9lbqQzf32GY2fVTN-82sO_SUgHS88qvggwU3naqMrB71RtxvLA_MR831zu2PfOINpBvpE995eYfzBMrbmtD8cKz2n7tXjvA7FyslPjnuB5zdP-F9zOf1-CbK1cz7iHNn0rxUeBzDX-U-QTJqUwn_eGpaZwkXdrZLH5uLdLlBgNR1Mp_arwILhVw3xW-0YnS_WZM_J1NJQuQP8ld0cI_dRmQ_klpKsmXk6kMQxIZK0QSBfTiRsANkCzby5GeKNAAz58_wPvPgK2TZVcq3PWCD5mkex4ZBQLalcnydb2J1OtZz4
06-20 14:35:34.865  14496-14496/? E/NetworkScheduler.SchedulerReceiver? Invalid parameter app
06-20 14:35:34.865  14496-14496/? E/NetworkScheduler.SchedulerReceiver? Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-20 14:35:34.872  10851-10851/? D/wpa_supplicant? nl80211: survey data missing!
06-20 14:35:34.880  14589-17516/? I/GoogleURLConnFactory? Using platform SSLCertificateSocketFactory
06-20 14:35:34.896  14589-17528/? D/PackageBroadcastService? Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.example.paramjeet.gesturech
06-20 14:35:34.943      143-798/? D/SurfaceFlinger? removeLayer_l  = com.lge.launcher2/com.lge.launcher2.Launcher
06-20 14:35:34.943    143-10744/? D/SurfaceFlinger? removeLayer_l  = com.lge.launcher2/com.lge.launcher2.Launcher
06-20 14:35:34.943  14589-17529/? I/PeopleContactsSync? CP2 sync disabled
06-20 14:35:34.974      533-533/? E/[LGHome]LGAllWidgetPagedView? [LGAllWidgetsPagedView.java:407:checkIfUpdate()]Nothing Changed size = 53
06-20 14:35:35.169      288-301/? W/ActivityManager? Activity pause timeout for ActivityRecord{422d8c60 com.example.paramjeet.gesturech/.MainActivity}
06-20 14:35:35.177      143-231/? D/SurfaceFlinger? createSurface for (540 x 960) Name=com.lge.launcher2/com.lge.launcher2.Launcher
06-20 14:35:35.208      533-533/? I/[LGHome]EVENT? [Launcher.java:1810:onResume()]onResume
06-20 14:35:35.404  16457-16457/? I/eFrame? pkgname_before:com.example.paramjeet.gesturech  class:com.lge.launcher2.Launcher


Post a Reply

Replies

Oldest  Newest  Rating
+1 Jagdeep Matharu · June 25, 2015
Can you share the XML file as well??
  • 1

Java / Android Development

107,053 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator