variables in classes outside activities - java

Am I doing this right? I'm trying to implement a simple countdown timer just to get my feet of the ground with it.
my timer class is not nested in my activity, and all by itself in it's own java file, for the sake of neatness. here:
public class McatTimer extends CountDownTimer {
private TextView timer_tv;
public McatTimer(long millisInFuture, long countDownInterval, TextView textview) {
super(millisInFuture, countDownInterval);
this.timer_tv = textview;
}
#Override
public void onFinish() {
timer_tv.setText("DONE!");
}
#Override
public void onTick(long millisUntilFinished) {
timer_tv.setText("seconds remaining: " + millisUntilFinished / 1000);
}
}
and here is logcat:
10-03 08:40:36.007: E/AndroidRuntime(2582): FATAL EXCEPTION: main
10-03 08:40:36.007: E/AndroidRuntime(2582): java.lang.NullPointerException
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.mangodeveloper.mcathomie.McatTimer.onTick(McatTimer.java:22)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.Looper.loop(Looper.java:123)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 08:40:36.007: E/AndroidRuntime(2582): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 08:40:36.007: E/AndroidRuntime(2582): at java.lang.reflect.Method.invoke(Method.java:507)
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 08:40:36.007: E/AndroidRuntime(2582): at dalvik.system.NativeStart.main(Native Method)
and my activity has these in their appropriate places:
private McatTimer mcatTimer;
....
mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
mcatTimer.start();
Also is there some trick to reading these logcats? I'm sorry but i'm self-teaching.

Looks like your TextView object is null. Please ensure that you have intialized your TextView before you call the
mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
Call this,
timerTv=(TextView)findViewById(R.id.textview);

Related

Fragment cannot be cast to android.content.Context

I want to connect my login page to MySQL PHP but I got some error here.
This is my logcat:
807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807
java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context
at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284)
at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
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)
and this is the error line in my loginFragment.java:
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData);
task.execute("http://10.0.3.2/mlibrary/HTML/login.php");
}
I need your help guys!
Thank you.
Replace LoginFragment.this with getActvity()
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);
Replace LoginFragment.this with getContext() :
PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);

Android Studio: activity_main.xml and content_main.xml not showing design preview && ClassNotFoundException

I want to use CircularLayout in my android app and I'm following this Circular Layout .
Errors:
1)activity_main.xml and content_main.xml are not showing any design preview.
2)On running app, it shows Unfortunately, app has stopped.
3)The logcat shows ClassNotFoundException.
There is no compilation error or warning.
Following is my logcat:
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: FATAL EXCEPTION: main
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: Process: com.example.shalini.circlelayout, PID: 15699
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shalini.circlelayout/com.example.shalini.circlelayout.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.shalini.circlelayout.MainActivity.Activity
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.shalini.circlelayout.MainActivity.Activity
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at com.example.shalini.circlelayout.MainActivity.onCreate(MainActivity.java:103)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5264)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.shalini.circlelayout.MainActivity.Activity" on path: DexPathList[[zip file "/data/app/com.example.shalini.circlelayout-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.shalini.circlelayout-2, /vendor/lib, /system/lib]]
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:559)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:745) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at com.example.shalini.circlelayout.MainActivity.onCreate(MainActivity.java:103) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5264) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
10-03 00:27:29.609 15699-15699/com.example.shalini.circlelayout E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
Well I inferred from it that it's not able to locate the class com.example.shalini.circlelayout.MainActivity.Activity.
Question:
How do we deal with ClassNotFoundException in this case?
This is how my MainActivity.java looks like:
package com.example.shalini.circlelayout;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
public class Activity extends View {
private final static int TOTAL_DEGREE = 360;
private final static int START_DEGREE = -90;
private Paint mPaint;
private RectF mOvalRect = null;
private int mItemCount = 5;
private int mSweepAngle;
private int mInnerRadius;
private int mOuterRadius;
private Bitmap mCenterIcon;
private int[] mColors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.CYAN};
private String[] mTitles = {"APPT CENTER", "MEDS CABINET", "CHECK-IN", "MY TRACKERS", "MY ACCOUNTS"};
public Activity(Context context) {
this(context, null);
}
public Activity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Activity(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(2);
mSweepAngle = TOTAL_DEGREE / mItemCount;
mInnerRadius = 125;
mOuterRadius = 400;
mCenterIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
#Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
if (mOvalRect == null) {
mOvalRect = new RectF(width / 2 - mOuterRadius, height / 2 - mOuterRadius, width / 2 + mOuterRadius, height / 2 + mOuterRadius);
}
for (int i = 0; i < mItemCount; i++) {
int startAngle = START_DEGREE + i * mSweepAngle;
mPaint.setColor(mColors[i]);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);
int centerX = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.cos(Math.toRadians(startAngle + mSweepAngle / 2)));
int centerY = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.sin(Math.toRadians(startAngle + mSweepAngle / 2)));
canvas.drawBitmap(mCenterIcon, width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY - mCenterIcon.getHeight() / 2, null);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(mTitles[i], width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY + mCenterIcon.getHeight(), mPaint);
}
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(width / 2, height / 2, mInnerRadius, mPaint);
canvas.drawBitmap(mCenterIcon, width / 2 - mCenterIcon.getWidth() / 2, height / 2 - mCenterIcon.getHeight() / 2, null);
super.onDraw(canvas);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).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.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);
}
}
This is my content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/lib/com.google.custom"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<com.example.shalini.circlelayout.MainActivity.Activity
android:id="#+id/pie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
custom:dividerWidth="5dp"
custom:innerCircle="#drawable/profile_pic_icon"
custom:innerRadius="50dp"
custom:layoutMode="pie"
custom:sliceDivider="#android:color/transparent" >
<RelativeLayout
android:id="#+id/appt_center_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher1" >
<TextView
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/appcenter"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher2" >
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/medscabinet"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher3" >
<TextView
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/cjeckin"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher4" >
<TextView
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/mytrackers"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher5" >
<TextView
android:id="#+id/five"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/myaccounts"
android:textStyle="bold" />
</RelativeLayout>
</com.example.shalini.circlelayout.MainActivity.Activity>
</RelativeLayout>
And here is the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Create outer class YourNewView extends View but not as Activity extends View.
In xml put as <com.example.shalini.circlelayout.YourNewView
Wherever you use id for your new circular layout cast it with YourNewView
Try it.
public class YourNewView extends View {
private final static int TOTAL_DEGREE = 360;
private final static int START_DEGREE = -90;
private Paint mPaint;
private RectF mOvalRect = null;
private int mItemCount = 5;
private int mSweepAngle;
private int mInnerRadius;
private int mOuterRadius;
private Bitmap mCenterIcon;
private int[] mColors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.CYAN};
private String[] mTitles = {"APPT CENTER", "MEDS CABINET", "CHECK-IN", "MY TRACKERS", "MY ACCOUNTS"};
public YourNewView (Context context) {
this(context, null);
}
public YourNewView (Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public YourNewView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(2);
mSweepAngle = TOTAL_DEGREE / mItemCount;
mInnerRadius = 125;
mOuterRadius = 400;
mCenterIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
#Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
if (mOvalRect == null) {
mOvalRect = new RectF(width / 2 - mOuterRadius, height / 2 - mOuterRadius, width / 2 + mOuterRadius, height / 2 + mOuterRadius);
}
for (int i = 0; i < mItemCount; i++) {
int startAngle = START_DEGREE + i * mSweepAngle;
mPaint.setColor(mColors[i]);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);
int centerX = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.cos(Math.toRadians(startAngle + mSweepAngle / 2)));
int centerY = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.sin(Math.toRadians(startAngle + mSweepAngle / 2)));
canvas.drawBitmap(mCenterIcon, width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY - mCenterIcon.getHeight() / 2, null);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(mTitles[i], width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY + mCenterIcon.getHeight(), mPaint);
}
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(width / 2, height / 2, mInnerRadius, mPaint);
canvas.drawBitmap(mCenterIcon, width / 2 - mCenterIcon.getWidth() / 2, height / 2 - mCenterIcon.getHeight() / 2, null);
super.onDraw(canvas);
}
}
Please remove Activity extends View from MainActivity

Java app crashes (NullPointerException)

i'm just programming a password manager but every time I run it, it crashed in cause of NullPointerException. I checked the code but i'm a new developer. Someone here who knows why? :(
Code of MainActivity.java:
package de.lennartschoch.passwordmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button login;
EditText masterpassfield;
static String masterpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
masterpass = "";
masterpassfield = (EditText) findViewById(R.id.masterpass);
if(masterpass.equals("")) {
Intent intent = new Intent(this,NewMasterpass.class);
startActivity(intent);
return;
}
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(masterpassfield.getText().toString().equals(masterpass)) {
Intent success = new Intent(view.getContext(), Passwords.class);
startActivityForResult(success, 0);
}
}});
}
#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;
}
}
Code of NewMasterpass.java:
package de.lennartschoch.passwordmanager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NewMasterpass extends Activity {
EditText masterpasscreate;
EditText repeat;
Button finish;
TextView mistake;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_masterpass);
masterpasscreate = (EditText) findViewById(R.id.masterpasscreate);
repeat = (EditText) findViewById(R.id.repeatcreate);
finish = (Button) findViewById(R.id.finish);
mistake = (TextView) findViewById(R.id.mistake);
finish.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(!masterpasscreate.getText().toString().equals(repeat.getText().toString())) {
mistake.setText("The passwords are different.");
mistake.setTextColor(Color.RED);
}
else if(masterpasscreate.getText().toString().equals("") || repeat.getText().toString().equals("")) {
mistake.setText("Both textfields must be filled in");
mistake.setTextColor(Color.RED);
}
else {
MainActivity.masterpass = masterpasscreate.getText().toString();
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
}});
}
}
LogCat error:
01-20 17:17:52.629: D/AndroidRuntime(561): Shutting down VM
01-20 17:17:52.629: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:17:52.639: E/AndroidRuntime(561): FATAL EXCEPTION: main
01-20 17:17:52.639: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivit y}: java.lang.NullPointerException
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.os.Looper.loop(Looper.java:137)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:17:52.639: E/AndroidRuntime(561): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:17:52.639: E/AndroidRuntime(561): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:17:52.639: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:17:52.639: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:17:52.639: E/AndroidRuntime(561): at dalvik.system.NativeStart.main(Native Method)
01-20 17:17:52.639: E/AndroidRuntime(561): Caused by: java.lang.NullPointerException
01-20 17:17:52.639: E/AndroidRuntime(561): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:26)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:17:52.639: E/AndroidRuntime(561): ... 11 more
01-20 17:17:57.129: I/Process(561): Sending signal. PID: 561 SIG: 9
01-20 17:21:37.499: D/AndroidRuntime(608): Shutting down VM
01-20 17:21:37.499: W/dalvikvm(608): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:21:37.511: E/AndroidRuntime(608): FATAL EXCEPTION: main
01-20 17:21:37.511: E/AndroidRuntime(608): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.os.Looper.loop(Looper.java:137)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:21:37.511: E/AndroidRuntime(608): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:21:37.511: E/AndroidRuntime(608): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:21:37.511: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:21:37.511: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:21:37.511: E/AndroidRuntime(608): at dalvik.system.NativeStart.main(Native Method)
01-20 17:21:37.511: E/AndroidRuntime(608): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.startActivity(Activity.java:3297)
01-20 17:21:37.511: E/AndroidRuntime(608): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:30)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:21:37.511: E/AndroidRuntime(608): ... 11 more
01-20 17:22:30.769: D/AndroidRuntime(655): Shutting down VM
01-20 17:22:30.769: W/dalvikvm(655): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:22:30.779: E/AndroidRuntime(655): FATAL EXCEPTION: main
01-20 17:22:30.779: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.os.Looper.loop(Looper.java:137)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:22:30.779: E/AndroidRuntime(655): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:22:30.779: E/AndroidRuntime(655): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:22:30.779: E/AndroidRuntime(655): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:22:30.779: E/AndroidRuntime(655): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:22:30.779: E/AndroidRuntime(655): at dalvik.system.NativeStart.main(Native Method)
01-20 17:22:30.779: E/AndroidRuntime(655): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.startActivity(Activity.java:3297)
01-20 17:22:30.779: E/AndroidRuntime(655): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:30)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:22:30.779: E/AndroidRuntime(655): ... 11 more
01-20 17:22:33.180: I/Process(655): Sending signal. PID: 655 SIG: 9
It asks:
01-20 17:22:30.779: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass};
have you declared this activity in your AndroidManifest.xml?
Have you?
Declare the NewMasterPass activity in your manifest xml as clearly mentioned in the logcat!
Try to add this to your AndroidManifest.xml
<activity android:name="de.lennartschoch.passwordmanager.NewMasterpass" />

unable to instantiate activity component info

when I run the programme I get a runtime error. Unable to instantiate activity component info.
Here is the code I use, I tried to write a listner for spinner.
public void addListenerlist() {
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String s = arg0.getItemAtPosition(arg2).toString();
if (s.equals("Malaysia")) {
message = getResources().getString(R.string.listdesc1);
} else if (s.equals("United States")) {
message = getResources().getString(R.string.listdesc1);
} else if (s.equals("Sri Lanka")) {
message = getResources().getString(R.string.listdesc2);
} else if (s.equals("Indonesia")) {
message = getResources().getString(R.string.listdesc3);
} else if (s.equals("France")) {
message = getResources().getString(R.string.listdesc4);
} else if (s.equals("Italy")) {
message = getResources().getString(R.string.listdesc5);
} else if (s.equals("Singapore")) {
message = getResources().getString(R.string.listdesc6);
} else if (s.equals("New Zealand")) {
message = getResources().getString(R.string.listdesc7);
} else if (s.equals("India")) {
message = getResources().getString(R.string.listdesc8);
} else {
message = getResources().getString(R.string.listdesc9);
}
String listtitle1 = getResources().getString(
R.string.listtitle1);
// String message =
// getResources().getString(R.string.listdesc1);
// txtview.setText(message);
// txtview2.setText(listtitle1);
Toast.makeText(Page5SubActivity.this,
"OnClickListener : " + message , Toast.LENGTH_SHORT)
.show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
I am new to java, another problem is that , can't I use message as local variable instead of a class variable. I have use message varable as a Class variable since using it as a local variable shows errors.
10-31 05:18:04.605: E/AndroidRuntime(854): FATAL EXCEPTION: main
10-31 05:18:04.605: E/AndroidRuntime(854): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xxx.xx/com.xxx.xx.Page5SubActivity}: java.lang.NullPointerException
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.os.Looper.loop(Looper.java:123)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-31 05:18:04.605: E/AndroidRuntime(854): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 05:18:04.605: E/AndroidRuntime(854): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 05:18:04.605: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-31 05:18:04.605: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-31 05:18:04.605: E/AndroidRuntime(854): at dalvik.system.NativeStart.main(Native Method)
10-31 05:18:04.605: E/AndroidRuntime(854): Caused by: java.lang.NullPointerException
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.Activity.findViewById(Activity.java:1647)
10-31 05:18:04.605: E/AndroidRuntime(854): at com.meegua.zakat.Page5SubActivity.<init>(Page5SubActivity.java:17)
10-31 05:18:04.605: E/AndroidRuntime(854): at java.lang.Class.newInstanceImpl(Native Method)
10-31 05:18:04.605: E/AndroidRuntime(854): at java.lang.Class.newInstance(Class.java:1409)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-31 05:18:04.605: E/AndroidRuntime(854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
10-31 05:18:04.605: E/AndroidRuntime(854): ... 11 more
I could find the proble after spending several hours on this.
I hadnt declared class variables properly. it was the reason to error "unable to instantiate activity component info"
private Spinner spinner1;
private TextView txtv ;
I had n't declared private keyword.
think this will be helpful to someone.

Android service always null

I'm trying to start a service, but always getting NPE. What could I be doing wrong?
Even the onServiceConnected() method does not get called, why ever?
#Override
protected void onCreate(Bundle savedInstanceState) {
mServiceIntent = new Intent(this, MyService.class);
bindService(mServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
//other stuff
}
usage:
private okButton(View v) {
startService(mServiceIntent);
}
result: NullPointerException!
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.v("service", "gets never executed!");
mDownloadService = ((LocalBinder<MyService>) service).getService();
}
#Override
public void onServiceDisconnected(ComponentName className) {
}
};
}
public class LocalBinder<S> extends Binder {
private String TAG = "LocalBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
Of course I also have the service in manifest:
<service android:enabled="true" android:name=".service.MyService" />
Logcat:
06-06 23:48:20.411: W/dalvikvm(592): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-06 23:48:20.461: E/AndroidRuntime(592): FATAL EXCEPTION: main
06-06 23:48:20.461: E/AndroidRuntime(592): java.lang.RuntimeException: Unable to start activity ComponentInfo{MyActivity}: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Looper.loop(Looper.java:137)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 23:48:20.461: E/AndroidRuntime(592): at dalvik.system.NativeStart.main(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): Caused by: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at MyActivity.okButton(MyActivity.java:217)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1939)
06-06 23:48:20.461: E/AndroidRuntime(592): ... 11 more
The intent seems to be ok:
Log.v("service", String.valueOf(mServiceIntent == null)); //false
Your stack trace clearly shows the NullPointerException happening in okButton(), and according to the code you posted, the only line in that function is
startService(mServiceIntent);
So clearly mServiceIntent is null at this point, despite being set in onCreate().
Do you have any other lines where you set mServiceIntent?
Or do you have a constructor in which you call okButton()? A constructor is the only thing I can think of that would run before onCreate(). That would also explain why onServiceConnected() is never run despite the call to bindService() in onCreate().
This code is not proper Java syntax, as the method definition has no return type :
private okButton(View v) {
startService(mServiceIntent);
}
You need something like:
private void okButton(View v) {
startService(mServiceIntent);
}
It looks to me from the stack trace that the okButton() method is being called during the creation of the MyActivity instance before the onCreate() method is called and of course at that time mService will be null.
Post more code or correct the code you've posted so that it actually matches what you are using.

Categories