Android NumberPicker NullPointer why? - java

i am try to implement a numberPicker to select minute values.
But i am getting a NullPointer Exception at this line:
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
Following Code:
public class MainActivity extends Activity {
NumberPicker minutePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Auswahl Minuten zum starten / Stoppen aller
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
minutePicker.setMaxValue(30);
minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
abschaltzeit = minutePicker.getValue();
}
});
minutePicker.setValue(0);
minutePicker.setWrapSelectorWheel(false);
}
}
XML:
<NumberPicker
android:id="#+id/minuten_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="6"
android:layout_column="0"
android:paddingLeft="20dp" />
Log:
09-25 11:00:09.749 10687-10687/de.carsten.awesome.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.carsten.awesome.app, PID: 10687
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.carsten.awesome.app/de.carsten.awesome.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
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 de.carsten.awesome.app.MainActivity.onCreate(MainActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
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)

minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
You're creating a NumberPicker programmatically and then overwriting the reference with whatever findViewById() returns. It returns null if your activity_main layout does not contain a minuten_picker.
Choose only the other: either create it prorgrammatically or find it from a view hierarchy you inflated.
If you choose the programmatic way new NumberPicker(), remember to add it to some layout in your activity view hierarchy, e.g. with setContentView()
If you choose the inflation way, make sure you have the view in your XML layout file.
I'm guessing the NPE you're seeing is actually on the following line where you're trying to invoke a method on the minutePicker and it's null.

I apologize.
I moved the Code from the MainActivity in the creating Fragment and it works with rootView.findView.
Sorry but i am new with the Fragement Konzept.
Thanks a lot for your help !

Related

Android problems with Intent

I have two Activities MainActivity and GameOverscreen Activity.
To my MainActivity i add two Views like this:
myView1 = new MyView1(this);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(480,
900);
myView1.setLayoutParams(params1);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative1);
myView1.setBackgroundColor(Color.YELLOW);
layout1.addView(myView1);
myView2 = new MyView2(this);
RelativeLayout.LayoutParams params2 = new
RelativeLayout.LayoutParams(300,300);
myView2.setLayoutParams(params2);
RelativeLayout layout2 = (RelativeLayout) findViewById(R.id.relative2);
myView2.setBackgroundColor(Color.GREEN);
layout2.addView(myView2);
When my Game is gameover Gameoverscreen Activity is starting correct, but when i touch on it and try to start MainActivity Activity again, my app chrushed with following exception:
java.lang.RuntimeException: Unable to start activity
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
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.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The specified child already
has
a
parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.view.ViewGroup.addView(ViewGroup.java:3360)
at android.view.ViewGroup.addView(ViewGroup.java:3336)
at com.example.admin.tetris.MainActivity.onCreate(MainActivity.java:74)
at android.app.Activity.performCreate(Activity.java:5231)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
How can i solve this?
Any help would be appreciate.
you can use this for your both views in your onCreate()
View myView1 = new View(this);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(480,
900);
myView1.setLayoutParams(params1);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative1);
myView1.setBackgroundColor(Color.YELLOW);
if (layout!=null){
layout.removeAllViews();//Remove all views here
layout.addView(myView1);// add views again
} else{
layout.addView(myView1);// add views as you do it before
}
Hope it helps.

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
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: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
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)
Use
textshow.setText(String.valueOf(receiveValue));

Setting up an avatar on android app

I'm wanting to let the user create an avatar (profile picture if you like) when they set up their user info. I've created a method for a single click/touch which would ask the user to take a picture and one for a long click which would ask the user to choose a picture from their gallery.
Below are my methods from the class file:
public void onLaunchCamera(View v) {
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
}
And below is the XML associated with the ImageButton:
<ImageButton
android:id="#+id/ImageButton_Avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="#dimen/avatar_size"
android:minHeight="#dimen/avatar_size"
android:onClick="onLaunchCamera"
android:scaleType="fitXY"
android:src="#drawable/avatar"></ImageButton>
All it does is crash when I click on the ImageButton and I have no idea why. Any ideas?
Thanks
EDIT: Adding the logcat below (Sorry about the formatting. Couldn't work out how to get it all sorted properly:
[ 04-12 18:32:50.989 5901: 5901 D/ ]
HostConnection::get() New Host Connection established 0xb8a44530, tid 5901
04-12 18:32:51.039 5901-5901/cct.mad.lab D/OpenGLRenderer: Enabling debug mode 0
04-12 18:32:55.739 5901-5901/cct.mad.lab V/RenderScript: 0xb8c53300 Launching thread(s), CPUs 2
04-12 18:32:57.389 5901-5901/cct.mad.lab D/AndroidRuntime: Shutting down VM
04-12 18:32:57.389 5901-5901/cct.mad.lab W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb0d2db20)
04-12 18:32:57.399 5901-5901/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
Process: cct.mad.lab, PID: 5901 java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at a 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:5017)
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: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
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:5017) 
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: java.lang.NullPointerException
at cct.mad.lab.SettingsActivity.onLaunchCamera(SettingsActivity.java:201)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at android.view.View$1.onClick(View.java:3818) 
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:5017) 
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) 
It looks like you might not have defined avatarButton, if you follow the Caused By path on the LogCat you see the bottom one is a NullPointerException.
Since I can't see the line numbers, the issue is happening on line 201--the only obvious null pointer I see in your code is avatarButton.
Based on what you want to do, you'll want to go about this a bit differently.
Remove the android:onClick="onLaunchCamera" from the XML.
in your onCreate() after you set the content view add the following:
View avatarButton = findViewById(R.id.ImageButton_Avatar);
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
This allows you to set both a click and a longClick listener with more control. The way you had it, you were never really defining the onClick or onLongClick until you clicked on them the first time.

ANDROID JAVA: Activity doesn't start when the used SharedPreferences

I have a problem. I want to read value Integer of activity "Settings" in the service GPSTracker and use it there. I use this SharedPrefrences and confirming key input. If the data is validated that the application returns to class FullscreenActivity.
This is the code responsible for this in activity Settings:
SharedPreferences.Editor editor;
public static final String NAME = "DISTANCE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
SharedPreferences pref = getApplicationContext().getSharedPreferences(NAME, MODE_PRIVATE);
editor=pref.edit();
Edit1= (EditText) findViewById(R.id.editSkan);
accept= (Button) findViewById(R.id.buttonSkan);
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (!Edit1.getText().toString().equals(""))
value = Integer.parseInt(Edit1.getText().toString());
editor.putInt("settings", value);
editor.commit();
Toast.makeText(getApplicationContext(), "Changed, value + " m", Toast.LENGTH_SHORT).show();
finish();
}
});
A code in GPSTracker like this:
SharedPreferences pref;
(...)
private int downloadSettings()
{
pref=context.getSharedPreferences("DISTANCE", Activity.MODE_PRIVATE);
value = pref.getInt("settings",15);
return value;
}
and a method call:
int dist = downloadSettings();
When I run the apps I get two errors in the log:
*java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adam.mobileproject/com.example.adam.mobileproject.FullscreenActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
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 com.example.adam.mobileproject.GPSTracker.downloadSettings(GPSTracker.java:284)
at com.example.adam.mobileproject.GPSTracker.<init>(GPSTracker.java:200)
at com.example.adam.mobileproject.FullscreenActivity.onCreate(FullscreenActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            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)*
PLEASE HELP!!!
The problem is definitely that context is null.
However, just like the Activity class, the Service class extends Context, so you should be able to replace context with this or getApplicationContext().
Try this:
private int downloadSettings()
{
pref = getApplicationContext().getSharedPreferences("DISTANCE", Activity.MODE_PRIVATE);
value = pref.getInt("settings",15);
return value;
}

Android application keep crashing

in the last few days I tried to develop android applications with no success, for some reason my application keep crashing.
the application keeps throwing few exceptions that I didn't succeeded to handle with.
MainActivity.java :
package com.mobIce.digicoin.digitalcoin;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private List<Coin> coins;
public MainActivity(){
coins = new ArrayList<Coin>();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
populateCoins();
populateListView();
}
}
private void populateCoins() {
coins.add(new Coin("Bitcoin", "650", "4", "http://coinmarketcap.com/img/Bitcoin.png"));
coins.add(new Coin("Dogecoin", "100", "400", "http://coinmarketcap.com/img/Bitcoin.png"));
coins.add(new Coin("fafaf", "8065", "400", "http://coinmarketcap.com/img/Bitcoin.png"));
}
private void populateListView() {
ArrayAdapter<Coin> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.coinsListView);
list.setAdapter(adapter);
}
#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;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
private class MyListAdapter extends ArrayAdapter<Coin> {
public MyListAdapter()
{
super(MainActivity.this, R.layout.coin_layout, coins);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
if(itemView == null)
itemView = getLayoutInflater().inflate(R.layout.coin_layout, parent, false);
//find the coin to work with.
Coin currentCoin = coins.get(position);
//fill the view
try{
TextView coinName = (TextView) itemView.findViewById(R.id.coinName);
coinName.setText(currentCoin.getName());
TextView coinPrice = (TextView) itemView.findViewById(R.id.coinPrice);
coinPrice.setText(currentCoin.getPrice());
TextView coinPercentage = (TextView) itemView.findViewById(R.id.coinPercentage);
coinPercentage.setText(currentCoin.getPercentage());
}
catch (NullPointerException e){ }
return itemView;
}
}
}
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mobIce.digicoin.digitalcoin.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/coinsListView"
android:paddingLeft="30dp" />
</LinearLayout>
coin_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="coinName"
android:id="#+id/coinName"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/coinIcon"
android:layout_marginTop="35dp"
android:layout_marginLeft="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/coinIcon"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="#drawable/dogecoin"
android:longClickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:baselineAlignBottom="false"
android:clickable="false"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/coinPrice"
android:layout_marginLeft="35dp"
android:layout_alignTop="#+id/coinIcon"
android:layout_toRightOf="#+id/coinName"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/coinPercentage"
android:layout_below="#+id/coinPrice"
android:layout_alignLeft="#+id/coinPrice"
android:layout_alignStart="#+id/coinPrice" />
</RelativeLayout>
logcat trace:
03-17 08:59:33.977 1107-1107/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 08:59:33.977 1107-1107/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 08:59:33.987 1107-1107/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1107
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:47)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            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:5017)
            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)
03-17 09:04:09.857 1149-1149/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:04:09.857 1149-1149/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:04:09.867 1149-1149/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1149
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            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:5017)
            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)
03-17 09:06:36.697 1192-1192/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:06:36.697 1192-1192/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:06:36.717 1192-1192/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1192
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            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:5017)
            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)
03-17 09:13:38.647 1239-1239/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:13:38.647 1239-1239/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:13:38.747 1239-1239/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1239
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            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:5017)
            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)
03-17 09:13:45.127 1239-1239/com.mobIce.digicoin.digitalcoin I/Process﹕ Sending signal. PID: 1239 SIG: 9
03-17 09:19:34.847 1285-1285/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:19:34.847 1285-1285/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:19:34.857 1285-1285/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1285
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            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:5017)
            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)
03-17 09:20:11.987 1285-1285/com.mobIce.digicoin.digitalcoin I/Process﹕ Sending signal. PID: 1285 SIG: 9
Your coinsListView is in the fragment layout and not in activity layout. You cannot access it with findViewById() in populateListView() called from activity onCreate(). Instead, move the list view population to the fragment's onCreateView() after the inflation, calling findViewById() on the rootView layout you just inflated.

Categories