i'm stuck on a problem trying to call a method from another class (when ever I call a method from this class I get a crash with NullPointerException except from when it's a static function. I currently call render.Update() and get a logcat output of -> http://pastebin.com/njjxDiQZ
11-26 17:14:40.532: E/MediaPlayer(29755): Should have subtitle controller already set
11-26 17:14:40.752: E/AndroidRuntime(29755): FATAL EXCEPTION: main
11-26 17:14:40.752: E/AndroidRuntime(29755): Process: com.coursework.courseworkapp, PID: 29755
11-26 17:14:40.752: E/AndroidRuntime(29755): java.lang.NullPointerException
11-26 17:14:40.752: E/AndroidRuntime(29755): at com.coursework.courseworkapp.Visual.updateVisualizer(Visual.java:73)
11-26 17:14:40.752: E/AndroidRuntime(29755): at com.coursework.courseworkapp.Visual$1.onWaveFormDataCapture(Visual.java:48)
11-26 17:14:40.752: E/AndroidRuntime(29755): at android.media.audiofx.Visualizer$NativeEventHandler.handleCaptureMessage(Visualizer.java:669)
11-26 17:14:40.752: E/AndroidRuntime(29755): at android.media.audiofx.Visualizer$NativeEventHandler.handleMessage(Visualizer.java:700)
11-26 17:14:40.752: E/AndroidRuntime(29755): at android.os.Handler.dispatchMessage(Handler.java:102)
11-26 17:14:40.752: E/AndroidRuntime(29755): at android.os.Looper.loop(Looper.java:137)
11-26 17:14:40.752: E/AndroidRuntime(29755): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-26 17:14:40.752: E/AndroidRuntime(29755): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 17:14:40.752: E/AndroidRuntime(29755): at java.lang.reflect.Method.invoke(Method.java:515)
11-26 17:14:40.752: E/AndroidRuntime(29755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-26 17:14:40.752: E/AndroidRuntime(29755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-26 17:14:40.752: E/AndroidRuntime(29755): at dalvik.system.NativeStart.main(Native Method)
Any help would be appreciated i'm really stumped here.
First Class
package com.coursework.courseworkapp;
import java.util.Set;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class Visual{
byte[] fft;
byte[] mFFTBytes;
byte[] mBytes;
private Visualizer mVisualizer;
Render render;
private Rect mRect = new Rect();
private Paint mFlashPaint = new Paint();
private Paint mFadePaint = new Paint();
Bitmap mCanvasBitmap;
Canvas mCanvas;
public int Test(MediaPlayer player){
mVisualizer = new Visualizer(player.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener()
{
#Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate)
{
updateVisualizer(bytes);
}
#Override
public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate)
{
updateVisualizerFFT(bytes);
}
};
mVisualizer.setDataCaptureListener(captureListener, Visualizer.getMaxCaptureRate() / 2, true, true);
mVisualizer.setEnabled(true);
return player.getAudioSessionId();
}
public void updateVisualizer(byte[] bytes) {
mBytes = bytes;
}
public void updateVisualizerFFT(byte[] bytes) {
mFFTBytes = bytes;
render.Update();
//render.invalidate()
}
public byte[] getFFT(){
return mFFTBytes;
}
}
The Render Class
package com.coursework.courseworkapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.media.audiofx.Visualizer;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class Render extends View {
public Render(Context context) {
super(context);
setMinimumWidth(800);
setMinimumHeight(1000);
setFocusable(true);
}
public void Update(){
//Will have code here after fix crash.
}
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.RED);
//Will add more code here.
}
}
MainActivity.Java - How I start Visualise.java
public class MainActivity extends Activity {
private MediaPlayer mPlayer;
Visual visualize = new Visual();
Render render;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
render = new Render(this);
((LinearLayout) findViewById(R.id.root)).addView(render, 0);
//Log.d("Freq", "test");
mPlayer = MediaPlayer.create(this, R.raw.test2);
mPlayer.setLooping(true);
mPlayer.start();
//Log.d("Freq", "test");
Handler handler = new Handler();
Runnable runnable = new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
int i = visualize.Test(mPlayer);
//render.invalidate();
}
};
handler.postDelayed(runnable, 100);
/*byte[] fft = visualize.getFFT();
if(fft != null){
Toast.makeText(this,"yey",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"nope",Toast.LENGTH_LONG).show();
}*/
/* THis part didn't work, Why? Check the other example */
//Log.w("Freq", String.valueOf(fft.length));
/*for(int i = 0; i < fft.length; i++){
String s = new String();
Log.w("Freq", String.valueOf(fft[i]));
}*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void tryDraw(){
//render.Update();
}
}
Your visual class does not extend Activtiy. But you have
render = new Render(this);
this does not refer to Activity Context.
Also you are not inflating any layoutin visual class ((LinearLayout) findViewById(R.id.root)).addView(render, 0); will give you NullPointerException.
findViewById looks for a view with the id mentioned in the current inflated layout.
You declare Render render; but don't initialize it
You can do this only if render is static so fix that
VISUAL:
public class Visual {
Context mContext;
// constructor
public Visual(Context context){
this.mContext = context;
}
....
public void updateVisualizerFFT(byte[] bytes) {
mFFTBytes = bytes;
//Don't forget to initialize redender
Render render = new Render(this.mContext);
render.Update();
//render.invalidate()
}
}
RENDER:
public class Render extends View {
public Render(Context context) {
super(context);
setMinimumWidth(800);
setMinimumHeight(1000);
setFocusable(true);
}
}
MainActivity :
You should be able to run
int i = visualize.Test(mPlayer);
Read up on context
Related
I was making a code to create a show list by ListView{in content_main.xml} like contact view.
I build a database in the (SQLite Expert Professional) with a table(name: "contact") that was contained a few name and family and numbers, with ID,NAME,fAMILY and PHONE NUMBER columns.
ID was unique, auto increment and primary key...other were Text except number that was CHAR.
then I calling them to my ListView as you see in below.(before it I made a row_list activity to made my listview , custom) but there was an Error and I can't find it.
{I could find my database.db file with that's table in the source data files that means my mistake wasn't from creating database.}
plz help me.I wanna learn android (as fast as i can) to get a JOB and I need it..
Errors.Android Monitor.Logcat
09-08 05:27:32.005 5692-5692/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x94c4fb20)
09-08 05:27:32.005 5692-5692/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: mizco.phonebook, PID: 5692
java.lang.RuntimeException: Unable to start activity ComponentInfo{mizco.phonebook/mizco.phonebook.Main}: android.database
.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at mizco.phonebook.database.getfulllist(database.java:97)
at mizco.phonebook.Main.onCreate(Main.java:36)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Main.java
package mizco.phonebook;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends AppCompatActivity {
private database db;
private String [][] res ;
private ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
list = (ListView) findViewById(R.id.main_list);
db = new database(this);
db.startusing();
db.open();
res = db.getfulllist();
db.close();
list.setAdapter(new AA());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
class AA extends ArrayAdapter<String>{
public AA() {
super(Main.this, R.layout.row_list, res[0]);
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater in = getLayoutInflater();
View row = in.inflate(R.layout.row_list,parent,false);
TextView name = (TextView) row.findViewById(R.id.row_name);
TextView number = (TextView) row.findViewById(R.id.row_number);
name.setText(res[0][position]+" "+res[1][position]);
number.setText(res[2][position]);
return row;
}
}
}
Class : database.java
package mizco.phonebook;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by Mahdi on 9/2/2017.
*/
public class database extends SQLiteOpenHelper {
public static String dbname= "database";
public static String dbpath="";
private Context mctxt;
private SQLiteDatabase mydb;
public database(Context context) {
super(context, dbname, null, 1);
mctxt = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
private boolean existdatabase(){
File m312 = new File(dbpath+dbname);
return m312.exists();
}
private void copydatabase(){
try {
InputStream IS = mctxt.getAssets().open(dbname);
OutputStream OS = new FileOutputStream(dbpath+dbname);
byte[] buffer = new byte[1024];
int length;
while ((length = IS.read(buffer))>0){
OS.write(buffer,0,length);
}
OS.flush();
OS.close();
IS.close();
} catch (Exception e) {
}
}
public void open(){
mydb = SQLiteDatabase.openDatabase(dbpath+dbname, null, SQLiteDatabase.OPEN_READWRITE);
}
public void close(){
mydb.close();
}
public void startusing () {
dbpath = mctxt.getFilesDir().getParent() + "/databases/";
if (!existdatabase()) {
this.getWritableDatabase();
copydatabase();
}
}
public String[][] getfulllist(){
Cursor cu = mydb.rawQuery("select * from Contact",null);
String [][] r = new String[3][cu.getCount()];
for (int i= 0;i<=cu.getCount();i++){
cu.moveToPosition(i);
r[0][i]=cu.getString(1);
r[1][i]=cu.getString(2);
r[2][i]=cu.getString(3);
}
return r;
}
}
You're stepping off the end of the cursor. Your code should read
for (int i= 0;i < cu.getCount();i++){
cu.moveToPosition(i);
r[0][i]=cu.getString(1);
r[1][i]=cu.getString(2);
r[2][i]=cu.getString(3);
}
return r;
I'm making an app that by now should save and then get the value of a numeric Edit Text. When i put to load data from string.xml it went fine, but when i switched to SharedPreferences it didn't even start. Any help finding the problem would be appreciated. BTW .java is below and i'm sure layout .xml is fine.
package programmingandroidapps.brightnesscustomizationapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.util.Log;
import android.content.SharedPreferences;
import android.content.Context;
public class MainActivity extends AppCompatActivity {
//Declarations
String brightnessValue;
int choice, brightnessValueInt;
final EditText mEditText1=(EditText)findViewById(R.id.editText1), mEditText2=(EditText)findViewById(R.id.editText2), mEditText3=(EditText)findViewById(R.id.editText3), mEditText4=(EditText)findViewById(R.id.editText4), mEditText5=(EditText)findViewById(R.id.editText5);
Button mButton1=(Button)findViewById(R.id.button1), mButton2=(Button)findViewById(R.id.button2), mButton3=(Button)findViewById(R.id.button3), mButton4=(Button)findViewById(R.id.button4), mButton5=(Button)findViewById(R.id.button5);
SharedPreferences storedBrightness1 = this.getSharedPreferences("brightv1", Context.MODE_PRIVATE), storedBrightness2 = this.getSharedPreferences("brightv2", Context.MODE_PRIVATE), storedBrightness3 = this.getSharedPreferences("brightv3", Context.MODE_PRIVATE), storedBrightness4 = this.getSharedPreferences("brightv4", Context.MODE_PRIVATE), storedBrightness5 = this.getSharedPreferences("brightv5", Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get saved brightness values from string.xml and set them to ediText1-5
brightnessValueInt = storedBrightness1.getInt("brightv1", 0);
brightnessIntToString();
printToEditText(1);
brightnessValueInt = storedBrightness2.getInt("brightv2", 0);
brightnessIntToString();
printToEditText(2);
brightnessValueInt = storedBrightness3.getInt("brightv3", 0);
brightnessIntToString();
printToEditText(3);
brightnessValueInt = storedBrightness4.getInt("brightv4", 0);
brightnessIntToString();
printToEditText(4);
brightnessValueInt = storedBrightness5.getInt("brightv5", 0);
brightnessIntToString();
printToEditText(5);
//
// On Click Button Listeners
mButton1.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Validate >=0 and <=100, paint #222222 if is valid, paint red if not
Log.v(brightnessValue, mEditText1.getText().toString());
mEditor = storedBrightness1.edit();
mEditor.putInt("brightv1", brightnessValueInt);
}
});
//
}
#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);
}
public void printToEditText(int choice)
{
if(choice==1)
{
mEditText1.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else if(choice==2)
{
mEditText2.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else if(choice==3)
{
mEditText3.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else if(choice==4)
{
mEditText4.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else
{
mEditText5.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
}
public void brightnessIntToString()
{
brightnessValue=brightnessValueInt+"";
}
}
I believe this is the applications crash log. I'm new to Android so if it is not just say it, no need to get angry :P
05-01 13:24:53.061 1624-1624/programmingandroidapps.brightnesscustomizationapp D/dalvikvm: Not late-enabling CheckJNI (already on)
05-01 13:24:56.451 1624-1624/programmingandroidapps.brightnesscustomizationapp D/AndroidRuntime: Shutting down VM
05-01 13:24:56.451 1624-1624/programmingandroidapps.brightnesscustomizationapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb3d11b20)
05-01 13:24:56.471 1624-1624/programmingandroidapps.brightnesscustomizationapp E/AndroidRuntime:
FATAL EXCEPTION: main
Process: programmingandroidapps.brightnesscustomizationapp, PID: 1624
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{programmingandroidapps.brightnesscustomizationapp/programmingandroidapps.brightnesscustomizationapp.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at programmingandroidapps.brightnesscustomizationapp.MainActivity.<init>(MainActivity.java:21)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
The problem is that you are calling findViewById and getSharedPreferences before layout has been applied and the activity (this) was initialized. You should put those method calls in onCreate after setContentView(R.layout.activity_main);.
Dejan is right about moving part of the code into the onCreate lifecycle method, specifically, your code should change to something like this:
package programmingandroidapps.brightnesscustomizationapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.util.Log;
import android.content.SharedPreferences;
import android.content.Context;
public class MainActivity extends AppCompatActivity {
//Declarations
String brightnessValue;
int choice, brightnessValueInt;
private EditText mEditText1, mEditText2, mEditText3, mEditText4, mEditText5;
private Button mButton1,mButton2, mButton3, mButton4, mButton5;
private SharedPreferences storedBrightness1, storedBrightness2, storedBrightness3,storedBrightness4,storedBrightness5;
private SharedPreferences.Editor mEditor;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1 =(Button)findViewById(R.id.button1);
mButton2=(Button)findViewById(R.id.button2);
mButton3=(Button)findViewById(R.id.button3);
mButton4=(Button)findViewById(R.id.button4);
mButton5=(Button)findViewById(R.id.button5);
mEditText1 =(EditText)findViewById(R.id.editText1);
mEditText2=(EditText)findViewById(R.id.editText2);
mEditText3=(EditText)findViewById(R.id.editText3);
mEditText4=(EditText)findViewById(R.id.editText4);
mEditText5=(EditText)findViewById(R.id.editText5);
//instead of doing this, I'd rather use "named" SharedPreferences - call it "brightness" and will carry the five values
//something like SharedPreferences brightnessPreferences = this.getSharedPreferences("brightness", Context.MODE_PRIVATE);
//I ma just adding this here because that's the way you have it
storedBrightness1 = this.getSharedPreferences("brightv1", Context.MODE_PRIVATE),
storedBrightness2 = this.getSharedPreferences("brightv2", Context.MODE_PRIVATE),
storedBrightness3 = this.getSharedPreferences("brightv3", Context.MODE_PRIVATE),
storedBrightness4 = this.getSharedPreferences("brightv4", Context.MODE_PRIVATE),
storedBrightness5 = this.getSharedPreferences("brightv5", Context.MODE_PRIVATE);
//Get saved brightness values from string.xml and set them to ediText1-5
brightnessValueInt = storedBrightness1.getInt("brightv1", 0);
brightnessIntToString();
printToEditText(1);
brightnessValueInt = storedBrightness2.getInt("brightv2", 0);
brightnessIntToString();
printToEditText(2);
brightnessValueInt = storedBrightness3.getInt("brightv3", 0);
brightnessIntToString();
printToEditText(3);
brightnessValueInt = storedBrightness4.getInt("brightv4", 0);
brightnessIntToString();
printToEditText(4);
brightnessValueInt = storedBrightness5.getInt("brightv5", 0);
brightnessIntToString();
printToEditText(5);
//
// On Click Button Listeners
mButton1.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Validate >=0 and <=100, paint #222222 if is valid, paint red if not
Log.v(brightnessValue, mEditText1.getText().toString());
mEditor = storedBrightness1.edit();
mEditor.putInt("brightv1", brightnessValueInt);
}
});
//
}
#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);
}
public void printToEditText(int choice)
{
if(choice==1)
{
mEditText1.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else if(choice==2)
{
mEditText2.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else if(choice==3)
{
mEditText3.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else if(choice==4)
{
mEditText4.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
else
{
mEditText5.setText(brightnessValue, TextView.BufferType.EDITABLE);
}
}
public void brightnessIntToString()
{
brightnessValue=brightnessValueInt+"";
}
}
I really can't figure out what's the problem here, I did almost the same example i got from somewhere on net and it's working, but this one reports a runtime error when I click on the button to switch to secondActivity. But before I set up onActivityResult in first and sending result in second activity, it switched fine.
It's a little bit longer code, but it's nothing complicated. In first activity you click button to go to second activity, and there you pick two numbers, which are stored in object and sent by intent back to the first activity, and in first activity in textview you get the total of those numbers.
MainActivity
package com.example.parcelablevezba4;
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.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView tv1;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.tv1);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, 42);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 42){
if(resultCode == RESULT_OK){
Object obj2 = data.getParcelableExtra("obje");
int total = obj2.getFirstSummand() + obj2.getSecondSummand();
tv1.setText(obj2.getFirstSummand()+"+"+obj2.getSecondSummand()+"is "+total);
}else if(resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "CANCELED", Toast.LENGTH_SHORT).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.main, menu);
return true;
}
}
SecondActivity
package com.example.parcelablevezba4;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class SecondActivity extends Activity {
EditText et1;
EditText et2;
Button btnOk;
int firstSummand;
int secondSummand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
et1 = (EditText)findViewById(R.id.etFirst);
et2 = (EditText)findViewById(R.id.etSecond);
btnOk = (Button)findViewById(R.id.btnOk);
firstSummand = Integer.parseInt(et1.getText().toString());
secondSummand = Integer.parseInt(et2.getText().toString());
btnOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent resultIntent = new Intent();
Object obj = new Object();
obj.setFirstSummand(firstSummand);
obj.setSecondSummand(secondSummand);
resultIntent.putExtra("obje", obj);
setResult(RESULT_OK,resultIntent);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
Object class
package com.example.parcelablevezba4;
import android.os.Parcel;
import android.os.Parcelable;
public class Object implements Parcelable {
private int firstSummand;
private int secondSummand;
public Object(){}
public Object(Parcel p){
this.firstSummand; = p.readInt();
this.secondSummand; = p.readInt();
}
public int getFirstSummand(){
return this.firstSummand;
}
public int getSecondSummand(){
return this.secondSummand;;
}
public void setFirstSummand(int f){
this.firstSummand = f;
}
public void setSecondSummand(int s){
this.secondSummand; = s;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel p, int flag) {
p.writeInt(firstSummand);
p.writeInt(secondSummand);
}
public static Parcelable.Creator<Object> CREATOR = new Parcelable.Creator<Object>() {
#Override
public Object createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Object(source);
}
#Override
public Object[] newArray(int size) {
// TODO Auto-generated method stub
return new Object[size];
}
};
}
I translated this from my language, so if I mistyped somewhere sorry about that.
Error
08-17 13:20:22.933: E/AndroidRuntime(743): FATAL EXCEPTION: main
08-17 13:20:22.933: E/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.parcelablevezba4/com.example.parcelablevezba4.SecondActivity}: java.lang.NumberFormatException: unable to parse '' as integer
08-17 13:20:22.933: E/AndroidRuntime(743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.os.Looper.loop(Looper.java:123)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 13:20:22.933: E/AndroidRuntime(743): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 13:20:22.933: E/AndroidRuntime(743): at java.lang.reflect.Method.invoke(Method.java:507)
08-17 13:20:22.933: E/AndroidRuntime(743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 13:20:22.933: E/AndroidRuntime(743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 13:20:22.933: E/AndroidRuntime(743): at dalvik.system.NativeStart.main(Native Method)
08-17 13:20:22.933: E/AndroidRuntime(743): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
08-17 13:20:22.933: E/AndroidRuntime(743): at java.lang.Integer.parseInt(Integer.java:362)
08-17 13:20:22.933: E/AndroidRuntime(743): at java.lang.Integer.parseInt(Integer.java:332)
08-17 13:20:22.933: E/AndroidRuntime(743): at com.example.parcelablevezba4.SecondActivity.onCreate(SecondActivity.java:30)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 13:20:22.933: E/AndroidRuntime(743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-17 13:20:22.933: E/AndroidRuntime(743): ... 11 more
It looks like one of your EditTexts are returning an empty string:
firstSummand = Integer.parseInt(et1.getText().toString());
secondSummand = Integer.parseInt(et2.getText().toString());
And then you try to parse that empty string.
Add a log before or even better a check:
String edit1 = et1.getText().toString();
String edit2 = et2.getText().toString();
Log.e("TAG", "First: "+edit1+" Second: "+edit2);
firstSummand = (edit1.isEmpty()) ? 0 : Integer.parseInt(edit1);
secondSummand = (edit2.isEmpty()) ? 0 : Integer.parseInt(edit2);
This is doing my head in, and it's probably something simple that's causing it - so I think I need a new set of eyes looking at it.
All activities are defined in the manifest. I've made sure all attributes are initialised in onCreate/constructor.
The 'Circle' object is just a generic object with gets and sets for x, y, radius and colour.
Any help greatly appreciated.
10-01 16:06:28.338: E/AndroidRuntime(18306): FATAL EXCEPTION: main
10-01 16:06:28.338: E/AndroidRuntime(18306): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dbk.parallelreaction/com.dbk.parallelreaction.GameActivity}: java.lang.NullPointerException
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.ActivityThread.access$600(ActivityThread.java:153)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.os.Looper.loop(Looper.java:137)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.ActivityThread.main(ActivityThread.java:5227)
10-01 16:06:28.338: E/AndroidRuntime(18306): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 16:06:28.338: E/AndroidRuntime(18306): at java.lang.reflect.Method.invoke(Method.java:511)
10-01 16:06:28.338: E/AndroidRuntime(18306): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-01 16:06:28.338: E/AndroidRuntime(18306): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-01 16:06:28.338: E/AndroidRuntime(18306): at dalvik.system.NativeStart.main(Native Method)
10-01 16:06:28.338: E/AndroidRuntime(18306): Caused by: java.lang.NullPointerException
10-01 16:06:28.338: E/AndroidRuntime(18306): at com.dbk.parallelreaction.util.GameView.<init>(GameView.java:51)
10-01 16:06:28.338: E/AndroidRuntime(18306): at com.dbk.parallelreaction.GameActivity.onCreate(GameActivity.java:64)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.Activity.performCreate(Activity.java:5104)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-01 16:06:28.338: E/AndroidRuntime(18306): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
The two files in question:
GameActivity.java
package com.dbk.parallelreaction;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.dbk.parallelreaction.util.GameView;
public class GameActivity extends Activity {
// GameView object to hold current level
private GameView game;
// level/score persistance
private int currentLevel;
private int currentScore;
// game countdown timer
private CountDownTimer timer;
private int timerTime; // in milliseconds
private int timerPeriod; // in ms also
private int timerIndex;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
// initialise
timerTime = 10000;
timerPeriod = 50;
timerIndex = (int) timerTime / timerPeriod;
// aesthetic stuff
TextView ins = (TextView) findViewById(R.id.GameInstructions);
Typeface tf = Typeface.createFromAsset(getAssets(), "Economica-Regular.ttf");
ins.setTypeface(tf);
ins.setTextColor(Color.WHITE);
ins.setTextSize(18);
// get extras
Bundle extras = getIntent().getExtras();
currentLevel = extras.getInt("LEVEL");
currentScore = extras.getInt("SCORE");
// check if starting new game, or going to next level
switch(currentLevel) {
case 1:
// new level
break;
case 2:
// etc
break;
}
// add the game view to the layout
FrameLayout frame = (FrameLayout) findViewById(R.id.GameContainer);
game = new GameView(this, 2);
frame.addView(game);
// timer bar
final ProgressBar pb = (ProgressBar) findViewById(R.id.GameTimerBar);
pb.setMax(timerIndex);
pb.setProgress(timerIndex);
timer = new CountDownTimer(timerTime, timerPeriod) {
#Override
public void onFinish() {
pb.setProgress(timerIndex);
Bundle extras = new Bundle();
extras.putInt("SCORE", 16); // placeholder score
Intent i = new Intent(getApplicationContext(), GameOverActivity.class);
i.putExtras(extras);
// GAME OVER if timer bar empties
finish();
startActivity(i);
}
#Override
public void onTick(long msLeft) {
timerIndex--;
pb.setProgress(timerIndex);
}
}.start();
}
// cancel timer if user exits game
public void onBackPressed() {
super.onBackPressed();
timer.cancel();
}
}
GameView.java updated
package com.dbk.parallelreaction.util;
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.dbk.parallelreaction.R;
public class GameView extends SurfaceView {
// Temp ArrayList containing Circle objects
private ArrayList<Circle> circles;
// Paint object for colour info
private Paint paint;
// number of circles to draw depending on level
private int numCircles;
// Current score in the current level
private int levelScore;
public GameView(Context context, int numberCircles) {
super(context);
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// replace custom_layout with the name of the .xml file that contains the `FrameLayout` with id R.id.GameContainer
View v = inflater.inflate(R.layout.activity_game, (ViewGroup) GameView.this.getParent(), true);
FrameLayout frame = (FrameLayout) v.findViewById(R.id.GameContainer);
// initialise
circles = new ArrayList<Circle>();
paint = new Paint();
levelScore = 0;
numCircles = numberCircles;
// Get the game container FrameView
int frameW = frame.getWidth();
int frameH = frame.getHeight();
int frameL = frame.getLeft();
int frameT = frame.getTop();
// fill arraylist with random circles
Random random = new Random();
for(int i=0; i<numCircles; i++) {
// generate coords within the frame
int x = random.nextInt((frameL+frameW)-frameL) + frameL;
int y = random.nextInt((frameT+frameH)-frameT) + frameT;
// random radius between 50 and 10
int r = random.nextInt(50-10) + 10;
// add them to the ArrayList
circles.add(new Circle(x, y, r, Color.WHITE));
}
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int i=0; i<numCircles; i++) {
Circle c = circles.get(i);
// set colour
paint.setColor(c.getColor());
// draw circle to canvas
canvas.drawCircle(c.getX(), c.getY(), c.getRadius(), paint);
}
}
}
If your custom SurfaceView will have a layout you need to inflate it. On your constructor you have
FrameLayout frame = (FrameLayout) findViewById(R.id.GameContainer);
You are trying to reference a FrameLayout that has not been inflated yet. To inflate the corresponding .xml you need to use a LayoutInflator. You can get a reference to the LayoutInflated via the Context passed in to your SurfaceView. For example like this:
public GameView(Context context, AttributeSet attrs, int numberCircles){
super(context,attrs);
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//replace custom_layout with the name of the .xml file that contains the `FrameLayout` with id R.id.GameContainer
View v = inflater.inflate(R.layout.custom_layout, (ViewGroup) GameView.this.getParent(), true);
FrameLayout frame = (FrameLayout)v.findViewById(R.id.GameContainer);
//The rest of your code...
}
Also, If you are going to add your GameView to an .xml file you need to use the SurfaceView(Context context, AttributeSet attrs) constructor.
Please check your XML that whether you have supplied correct ID for Frame Layout in GameView.java
I have been tasked to resolve an issue for a group project at my university however I cannot seem to resolve an issue with a NullPointerException in my service class. Our goal is to create a script which continually monitors the android history until it finds a match - then executes a warning class if the service class finds a match in the browser history. The issue occurs on line 71 (of service class) however I do not know how to resolve the issue.
Service Class:
public class Service_class extends Service {
String Dirty1 = "www.pornhub.com";
String Dirty2 = "www.playboy.com";
String Dirty3 = "www.playboy.com";
String Dirty4 = "www.playboy.com";
String Dirty5 = "www.playboy.com";
String Dirty6 = "www.playboy.com";
String Dirty7 = "www.playboy.com";
String Dirty8 = "www.playboy.com";
String Dirty9 = "www.playboy.com";
String Dirty10 = "www.playboy.com";
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
#Override
public void onCreate() {
super.onCreate();
TextView tv = (TextView) findViewById(R.id.hello);
String[] projection = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
Cursor cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null);
String urls = "";
if (cursor.moveToFirst()) {
String url1 = null;
String url2 = null;
do {
String url = cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.URL));
if (url.toLowerCase().contains(Dirty1)) {
} else if (url.toLowerCase().contains(Dirty2)) {
} else if (url.toLowerCase().contains(Dirty3)) {
} else if (url.toLowerCase().contains(Dirty4)) {
} else if (url.toLowerCase().contains(Dirty5)) {
} else if (url.toLowerCase().contains(Dirty6)) {
} else if (url.toLowerCase().contains(Dirty7)) {
} else if (url.toLowerCase().contains(Dirty8)) {
} else if (url.toLowerCase().contains(Dirty9)) {
} else if (url.toLowerCase().contains(Dirty10)) {
//if (url.toLowerCase().contains(Filthy)) {
urls = urls
+ cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.TITLE)) + " : "
+ url + "\n";
Intent intent = new Intent(Service_class.this, Warning.class);
Service_class.this.startActivity(intent);
}
} while (cursor.moveToNext());
// tv.setText(urls);
}
}
private void setContentView(int main3) {
// TODO Auto-generated method stub
}
private TextView findViewById(int hello) {
// TODO Auto-generated method stub
return null;
}
private Cursor managedQuery(Uri bookmarksUri, String[] projection,
Object object, Object object2, Object object3) {
// TODO Auto-generated method stub
return null;
}}
Main.java
import java.util.Calendar;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
// Start service using AlarmManager
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(Main.this, Service_class.class);
PendingIntent pintent = PendingIntent.getService(Main.this, 0, intent,
0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
36000 * 1000, pintent);
// click listener for the button to start service
Button btnStart = (Button) findViewById(R.id.button1);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startService(new Intent(getBaseContext(), Service_class.class));
}
});
// click listener for the button to stop service
Button btnStop = (Button) findViewById(R.id.button2);
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopService(new Intent(getBaseContext(), Service_class.class));
}
});
}}
LOGCAT:
04-15 13:58:49.980: D/AndroidRuntime(1994): Shutting down VM
04-15 13:58:50.000: W/dalvikvm(1994): threadid=1: thread exiting with uncaught exception (group=0x40cc7930)
04-15 13:58:50.000: E/AndroidRuntime(1994): FATAL EXCEPTION: main
04-15 13:58:50.000: E/AndroidRuntime(1994): java.lang.RuntimeException: Unable to create service com.nfc.linked.Service_class: java.lang.NullPointerException
04-15 13:58:50.000: E/AndroidRuntime(1994): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2539)
04-15 13:58:50.000: E/AndroidRuntime(1994): at android.app.ActivityThread.access$1600(ActivityThread.java:141)
04-15 13:58:50.000: E/AndroidRuntime(1994): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-15 13:58:50.000: E/AndroidRuntime(1994): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 13:58:50.000: E/AndroidRuntime(1994): at android.os.Looper.loop(Looper.java:137)
04-15 13:58:50.000: E/AndroidRuntime(1994): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-15 13:58:50.000: E/AndroidRuntime(1994): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 13:58:50.000: E/AndroidRuntime(1994): at java.lang.reflect.Method.invoke(Method.java:511)
04-15 13:58:50.000: E/AndroidRuntime(1994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-15 13:58:50.000: E/AndroidRuntime(1994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-15 13:58:50.000: E/AndroidRuntime(1994): at dalvik.system.NativeStart.main(Native Method)
04-15 13:58:50.000: E/AndroidRuntime(1994): Caused by: java.lang.NullPointerException
04-15 13:58:50.000: E/AndroidRuntime(1994): at com.nfc.linked.Service_class.onCreate(Service_class.java:71)
04-15 13:58:50.000: E/AndroidRuntime(1994): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2529)
04-15 13:58:50.000: E/AndroidRuntime(1994): ... 10 more
04-15 14:00:23.770: D/AndroidRuntime(2047): Shutting down VM
04-15 14:00:23.770: W/dalvikvm(2047): threadid=1: thread exiting with uncaught exception (group=0x40cc7930)
Your method managedQuery() returns null
private Cursor managedQuery(Uri bookmarksUri, String[] projection,
Object object, Object object2, Object object3) {
// TODO Auto-generated method stub
return null;
}}
So when you try to execute a method on null you will get a NullPointerException.
Cursor cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null); // cursor will be null
String urls = "";
if (cursor.moveToFirst()) { // this will be NPE
Make your managedQuery() method return an instantiated Cursor object.