Eclipse finds no errors in my code, but when I try to run the app on an emulator, it opens and then immediately crashes. Logcat gives me the vague nullPointerException error. I can comment out the onKeyDown method, and then it runs just fine. But of course, I can't use the back key to go back, it will just close out the app.
My code is as follows:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
static String[] items = {"Campaign", "Multiplayer", "Zombies"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);
listView.setAdapter(new BaseAdapter(){
public int getCount() {
return items.length;
}
public Object getItem(int position) {
return items[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_row, null);
TextView textView = (TextView) view.findViewById(R.id.TextView01);
textView.setText(items[position]);
textView.setTextColor(Color.rgb(255,106,0));
textView.setTextSize(24);
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setTextColor(Color.rgb(255,106,0));
return view;
}});
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (position == 0) {
viewflipper.showNext();
}if (position == 1) {
viewflipper.showNext();
viewflipper.showNext();
}if (position == 2) {
viewflipper.showNext();
viewflipper.showNext();
viewflipper.showNext();
}}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if(viewflipper.getDisplayedChild() == 0){
viewflipper.showPrevious();
}if(viewflipper.getDisplayedChild() == 1){
viewflipper.showPrevious();
viewflipper.showPrevious();
}if(viewflipper.getDisplayedChild() == 2){
viewflipper.showPrevious();
viewflipper.showPrevious();
viewflipper.showPrevious();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
};
My Logcat is as follows:
01-21 11:38:46.151: E/AndroidRuntime(760): FATAL EXCEPTION: main
01-21 11:38:46.151: E/AndroidRuntime(760): java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.blackopsiiexperience/com.example.blackopsiiexperience.MainActivity}: java.lang.NullPointerException
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.os.Looper.loop(Looper.java:137)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 11:38:46.151: E/AndroidRuntime(760): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 11:38:46.151: E/AndroidRuntime(760): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 11:38:46.151: E/AndroidRuntime(760): at dalvik.system.NativeStart.main(Native Method)
01-21 11:38:46.151: E/AndroidRuntime(760): Caused by: java.lang.NullPointerException
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.Activity.findViewById(Activity.java:1839)
01-21 11:38:46.151: E/AndroidRuntime(760): at com.example.blackopsiiexperience.MainActivity.<init>(MainActivity.java:73)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.Class.newInstanceImpl(Native Method)
01-21 11:38:46.151: E/AndroidRuntime(760): at java.lang.Class.newInstance(Class.java:1319)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-21 11:38:46.151: E/AndroidRuntime(760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-21 11:38:46.151: E/AndroidRuntime(760): ... 11 more
Any suggestions?
move
ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);
inside onCreate of Activity after setting layout for Activity
EDIT :
declare Viewflipper as class level field to access it through out class instead of method level as :
public class MainActivity extends Activity {
static String[] items = {"Campaign", "Multiplayer", "Zombies"};
ListView listView;
ViewFlipper viewflipper; //<<<< declare here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);
//your code here.....
Related
I am trying to create an Image Slider containing 4 images for my application and i've been running into the following errors. I've tried searching and have found similiar titles but the problems are different.
This is the logcat:
example.prgguru.com.wallit E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.prgguru.com.wallit, PID: 32048
java.lang.RuntimeException: Unable to start activity ComponentInfo{example.prgguru.com.wallit/example.prgguru.com.wallit.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class example.prgguru.com.wallit.MainActivityFragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:585)
at android.app.Fragment.instantiate(Fragment.java:561)
at android.app.Activity.onCreateView(Activity.java:5066)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException
at android.app.Fragment.instantiate(Fragment.java:585)
at android.app.Fragment.instantiate(Fragment.java:561)
at android.app.Activity.onCreateView(Activity.java:5066)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
This is my Main Activity.java:
package example.prgguru.com.wallit;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:name="example.prgguru.com.wallit.MainActivityFragment"
android:id="#+id/fragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32383d">
<!-- ViewPager -->
<!-- Footer -->
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageviewPager" />
<include layout="#layout/footer"/>
</FrameLayout>
MainActivityFragment.java
package example.prgguru.com.wallit;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivityFragment extends Fragment {
private ViewPager _mViewPager;
private ImageViewPagerAdapter _adapter;
private ImageView _btn1, _btn2, _btn3,_btn4;
public MainActivityFragment() {
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUpView();
setTab();
onCircleButtonClick();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
private void onCircleButtonClick() {
_btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn1.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(0);
}
});
_btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn2.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(1);
}
});
_btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn3.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(2);
}
});
_btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn4.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(3);
}
});
}
private void setUpView() {
_mViewPager = (ViewPager) getView().findViewById(R.id.imageviewPager);
_adapter = new ImageViewPagerAdapter(getActivity(), getFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
initButton();
}
private void setTab() {
_mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int position) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
_btn1.setImageResource(R.drawable.holo_circle);
_btn2.setImageResource(R.drawable.holo_circle);
_btn3.setImageResource(R.drawable.holo_circle);
_btn4.setImageResource(R.drawable.holo_circle);
btnAction(position);
}
});
}
private void btnAction(int action) {
switch (action) {
case 0:
_btn1.setImageResource(R.drawable.fill_circle);
break;
case 1:
_btn2.setImageResource(R.drawable.fill_circle);
break;
case 2:
_btn3.setImageResource(R.drawable.fill_circle);
break;
case 3:
_btn4.setImageResource(R.drawable.fill_circle);
break;
}
}
private void initButton() {
_btn1 = (ImageView) getView().findViewById(R.id.btn1);
_btn1.setImageResource(R.drawable.fill_circle);
_btn2 = (ImageView) getView().findViewById(R.id.btn2);
_btn3 = (ImageView) getView().findViewById(R.id.btn3);
_btn4 = (ImageView) getView().findViewById(R.id.btn4);
}
private void setButton(Button btn, String text, int h, int w) {
btn.setWidth(w);
btn.setHeight(h);
btn.setText(text);
}
}
Your Activity extends from Activity and not AppCompatActivity. But your fragment extends from android.support.v4.app.Fragment. You must use one type of fragment: from support library or normal one.
Try to change extends Fragment from android.app.Fragment in your fragment class.
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
SharedPreferences mySharedPreferences;
TextView t1,t2;
Button generate1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
t1 = (TextView) findViewById(R.id.textView3);
t2 = (TextView) findViewById(R.id.textView8);
generate1 = (Button) findViewById(R.id.button);
generate1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random rand = new Random();
int num1 = rand.nextInt(100);
int num2 = mySharedPreferences.getInt("INT_KEY", -1);
if(num2 == -1) {
num2 = 0; //initialize the total count value to 0
}
if (num1 % 2 == 0) {
num2 += 1;
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putInt("INT_KEY", num2);
editor.commit();
t1.setText("" + num2);
}
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
#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);
}
}
What I want to do is :
1.Check Whether Num2 value is exist or not, If not set to zero.
2.Randomly Generate new numbers 0~100 in num1
3.If num1 is Even, Increase num2 by 1.
4.Save Value and Repeat.
When I clicked the button, It crashes. What could be the reason?
Logcat:
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 D/AndroidRuntime: Shutting down VM
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x423dcc08)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: FATAL EXCEPTION: main
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: Process: com.example.tans.goldminer1, PID: 30860
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: java.lang.NullPointerException
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at com.example.tans.goldminer1.MainActivity$1.onClick(MainActivity.java:35)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at android.view.View.performClick(View.java:4753)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19567)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:146)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5635)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
Add mySharedPreferences = PreferenceManeger.getDefaultSharedPreferences(this) to your code before you use it. (As assumed that you want to use default preferences.)
I am following slidenerd Material design tutorial(Android RecyclerView Example part1,2,3,4) & I am using real device to test my android application.I have successfully build navigation drawer & when I try to add RecylerView into it by adding adapter (ThalakazAdapter.java) , app unfortunately get stopped.
This is my NavigationDrawerFragment.java
package com.thaalakaz.nadeesha.materialdesignapp1;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
//import android.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
*/
public class NavigationDrawerFragment extends Fragment {
private RecyclerView recyclerView;
public static final String PREF_FILE_NAME = "testpref";
public static final String KAY_USER_LEARNED_DRAWER ="user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ThalakazAdapter adapter;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
private View containerView;
public NavigationDrawerFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KAY_USER_LEARNED_DRAWER,"false"));
if (savedInstanceState != null){
mFromSavedInstanceState = true;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawer_layout);
adapter = new ThalakazAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<Information> getData(){
List<Information> data = new ArrayList<>();
int[] icons ={R.drawable.ic_m,R.drawable.ic_m1,R.drawable.ic_m2,R.drawable.ic_m3};
String[] titles = {"Nadeesha","Thilakarathne","Semini","Roshani"};
for (int i=0;i<titles.length && i<icons.length;i++){
Information current = new Information();
current.iconId = icons[i];
current.titlel = titles[i];
data.add(current);
}
return data;
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(!mUserLearnedDrawer){
mUserLearnedDrawer = true;
saveToPreferences(getActivity(),KAY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
}
getActivity().invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (slideOffset<0.6){
toolbar.setAlpha(1-slideOffset);
}
}
};
if (!mUserLearnedDrawer && mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static void saveToPreferences(Context context,String prferenceName,String preferenceValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(prferenceName,preferenceValue);
editor.apply();
}
public static String readFromPreferences(Context context,String prferenceName,String defaultValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
return sharedPreferences.getString(prferenceName,defaultValue);
}
}
This is my Adapter (ThalakazAdapter.java)
package com.thaalakaz.nadeesha.materialdesignapp1;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
/**
* Created by hp on 7/22/2015.
*/
public class ThalakazAdapter extends RecyclerView.Adapter <ThalakazAdapter.myViewHolder>{
private LayoutInflater inflater;
List<Information> data = Collections.emptyList();
public ThalakazAdapter(Context context, List<Information> data){
inflater=LayoutInflater.from(context);
this.data = data;
}
#Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_row, parent, false);
myViewHolder holder = new myViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(myViewHolder holder, int position) {
Information current = data.get(position);
holder.title.setText(current.titlel);
holder.icon.setImageResource(current.iconId);
}
#Override
public int getItemCount() {
return 0;
}
class myViewHolder extends RecyclerView.ViewHolder{
ImageView icon;
TextView title;
public myViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.listText);
icon = (ImageView) itemView.findViewById(R.id.listIcon);
}
}
}
My information.java class
package com.thaalakaz.nadeesha.materialdesignapp1;
/**
* Created by hp on 7/22/2015.
*/
public class Information {
int iconId;
String titlel;
}
You can view my entire project from here
Logcat
07-21 22:32:07.101 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ Late-enabling CheckJNI
07-21 22:32:07.111 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/SystemProperties﹕ get key=ro.kernel.android.tracing
07-21 22:32:07.281 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
07-21 22:32:07.281 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ VFY: unable to resolve virtual method 14685: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
07-21 22:32:07.281 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
07-21 22:32:07.301 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-21 22:32:07.301 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-21 22:32:07.301 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-21 22:32:07.311 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-21 22:32:07.311 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ VFY: unable to resolve virtual method 436: Landroid/content/res/TypedArray;.getType (I)I
07-21 22:32:07.311 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-21 22:32:07.381 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/AndroidRuntime﹕ Shutting down VM
07-21 22:32:07.381 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41027208)
07-21 22:32:07.391 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thaalakaz.nadeesha.materialdesignapp1/com.thaalakaz.nadeesha.materialdesignapp1.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.thaalakaz.nadeesha.materialdesignapp1.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.thaalakaz.nadeesha.materialdesignapp1.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:63)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:838)
at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:668)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.thaalakaz.nadeesha.materialdesignapp1.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
fragment_navigation_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DADADA"
tools:context="com.thaalakaz.nadeesha.materialdesignapp1.NavigationDrawerFragment">
<LinearLayout
android:id="#+id/containerDrawerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF6F00">
<ImageView
android:layout_width="280dp"
android:layout_height="140dp"
android:src = "#drawable/dog"
android:layout_margin="16dp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
in your onCreateView your are looking for the wrong id. Change
recyclerView = (RecyclerView) layout.findViewById(R.id.drawer_layout);
with
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
I also noticed that in your Adapter's constructor, you are assingn this.data to itself. Change
this.data = this.data;
to
this.data = data
and getItemCount shouldn't return 0, but the size of data (if it is not null)
I am trying to pass a String form one activity to another in my app, however I keep getting a null pointer error. This is what shows up in my LogCat:
12-29 02:49:03.256: D/(663): HostConnection::get() New Host Connection established 0x96cb850, tid 663
12-29 02:49:06.288: W/KeyCharacterMap(663): No keyboard for id 0
12-29 02:49:06.288: W/KeyCharacterMap(663): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-29 02:49:09.600: D/AndroidRuntime(663): Shutting down VM
12-29 02:49:09.600: W/dalvikvm(663): threadid=1: thread exiting with uncaught exception (group=0xb5f444f0)
12-29 02:49:09.604: E/AndroidRuntime(663): FATAL EXCEPTION: main
12-29 02:49:09.604: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld3/com.example.helloworld3.FloorPlan}: java.lang.NullPointerException
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.os.Looper.loop(Looper.java:130)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-29 02:49:09.604: E/AndroidRuntime(663): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 02:49:09.604: E/AndroidRuntime(663): at java.lang.reflect.Method.invoke(Method.java:507)
12-29 02:49:09.604: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-29 02:49:09.604: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-29 02:49:09.604: E/AndroidRuntime(663): at dalvik.system.NativeStart.main(Native Method)
12-29 02:49:09.604: E/AndroidRuntime(663): Caused by: java.lang.NullPointerException
12-29 02:49:09.604: E/AndroidRuntime(663): at com.example.helloworld3.FloorPlan.onCreate(FloorPlan.java:24)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-29 02:49:09.604: E/AndroidRuntime(663): ... 11 more
12-29 02:49:12.316: D/(677): HostConnection::get() New Host Connection established 0x96c5790, tid 677
This is my ManActivity.java:
package com.example.helloworld3;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract.RawContacts.Data;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText destination = (EditText)findViewById(R.id.roomdinput);
final Button floorPlan = (Button)findViewById(R.id.floorPlanButton);
floorPlan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
String roomName = destination.getText().toString();
Bundle myb = new Bundle();
myb.putString("key1", roomName);
Intent a = new Intent(MainActivity.this, FloorPlan.class);
a.putExtras(myb);
startActivity(a);
startActivity(new Intent("com.example.helloworld3.FLOORPLAN"));
}
});
}
#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);
}
}
And this is my FloorPan.java:
package com.example.helloworld3;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FloorPlan extends Activity{
DrawView2 drawView;
String roomName2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.floorplan);
Bundle myb2= getIntent().getExtras();
roomName2 = myb2.getString("key1");
drawView = new DrawView2(this);
drawView.setBackgroundResource(R.drawable.tait1st);
setContentView(drawView);
}
public class DrawView2 extends View {
Paint paint = new Paint();
float ux, dx, rx,lx;
public String getRoomName(){
return roomName2;
}
public void setCoordinates(){
if(roomName2 == "C154"){
ux =90;
dx = 250;
rx = 90;
lx = 400;
}else {
ux =76;
dx = 98;
rx = 140;
lx = 300;
}
};
public DrawView2(Context context) {
super(context);
paint.setColor(Color.RED);
//roomName2 = drawView.getTag();
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawLine(ux, dx , rx, lx, paint);
canvas.drawLine(20, 0, 0, 20, paint);
canvas.drawCircle(150, 400, 30, paint);
}
}
}
Thank you for all your help!
I think , This line in OnClickListener of floorPlan in your MainActivity causing problem :
startActivity(new Intent("com.example.helloworld3.FLOORPLAN"));
putString method is correct. It's inherited from android.os.BaseBundle.
The logcat shows your bundle get from intent.getExtras() is null:
Bundle myb2= getIntent().getExtras();
roomName2 = myb2.getString("key1");//here myb2 is null
Maybe you should override onNewIntent to setIntent:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
Why you have started activity 2 times in below code ?
Intent a = new Intent(MainActivity.this, FloorPlan.class);
a.putExtras(myb); startActivity(a);
startActivity(new Intent("com.example.helloworld3.FLOORPLAN")); // this intent doesnt`t have bundle
This might be the reason which lead where Bundle object is null in intent and causing nullpointerexception.
I am getting a NullPointerException. Below you can find my Logcat and the relevant code.
Logcat:
12-23 00:17:35.330: E/AndroidRuntime(2019): FATAL EXCEPTION: main
12-23 00:17:35.330: E/AndroidRuntime(2019): Process: com.android.timesheet, PID: 2019
12-23 00:17:35.330: E/AndroidRuntime(2019): java.lang.NullPointerException
12-23 00:17:35.330: E/AndroidRuntime(2019): at com.android.timesheet.adapter.CustomCursorAdapter$1.onClick(CustomCursorAdapter.java:54)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.view.View.performClick(View.java:4438)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.view.View$PerformClick.run(View.java:18422)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.os.Handler.handleCallback(Handler.java:733)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.os.Handler.dispatchMessage(Handler.java:95)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.os.Looper.loop(Looper.java:136)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-23 00:17:35.330: E/AndroidRuntime(2019): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 00:17:35.330: E/AndroidRuntime(2019): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 00:17:35.330: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-23 00:17:35.330: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-23 00:17:35.330: E/AndroidRuntime(2019): at dalvik.system.NativeStart.main(Native Method)
CustomCursorAdapter.java:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;
import com.android.timesheet.ModifyMember;
import com.android.timesheet.R;
public class CustomCursorAdapter extends CursorAdapter {
Button delete_btn;
TextView memID_tv, memName_tv;
#SuppressWarnings("deprecation")
public CustomCursorAdapter(Activity context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.single_row_item, parent, false);
return retView;
}
#Override
public void bindView(View view, final Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
delete_btn=(Button)view.findViewById(R.id.delete_btn);
delete_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
memID_tv = (TextView)v.findViewById(R.id.member_id);
memName_tv = (TextView)v.findViewById(R.id.member_name);
String memberID_val = memID_tv.getText().toString();; ---->54th Line
String memberName_val = memName_tv.getText().toString();
Intent i = new Intent(context,
ModifyMember.class);
i.putExtra("memberName", memberName_val);
i.putExtra("memberID", memberID_val);
((Activity)context).startActivity(i);
}
});
}
}
I am using a delete button to delete all the listview row items. At that point I am getting the NullPointerException.
Use view(view of row) instead of v parameter of onClick method which is view of Button :
delete_btn=(Button)view.findViewById(R.id.delete_btn);
delete_btn.setTag(view);
delete_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rowview=(View)v.getTag();
memID_tv = (TextView)rowview.findViewById(R.id.member_id);
memName_tv = (TextView)rowview.findViewById(R.id.member_name);
}
});
Use view instead of v
memID_tv = (TextView)view.findViewById(R.id.member_id);
In bindview try this-
if (view == null) {
view = inflater.inflate(R.layout.single_row_item, parent, null);
}
TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
delete_btn=(Button)view.findViewById(R.id.delete_btn);
.
.
.
you are getting the view from clicked view, but you have to get the views from the root view.
memID_tv = (TextView)v. findViewById(R.id.member_id);
memName_tv = (TextView)v.findViewById(R.id.member_name);
change these lines into this:
memID_tv = (TextView)view. findViewById(R.id.member_id);
memName_tv = (TextView)view.findViewById(R.id.member_name);