BaseAdapter app crashes [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My app build, but it crashes, and I don't know why.
Basically I'm implementing BaseAdapter inside MainActivity, later I will put it in another class
Here's MainActivity.java
public class MainActivity extends Activity {
ListView mListView;
String[] mTitle={"item1","item2"};
String[] mDetail={"info1","info2"};
int[] mImage ={R.drawable.ic_launcher,R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView=(ListView)findViewById(R.id.list);
mListView.setAdapter(new dataListAdapter(mTitle, mDetail, mImage, this));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
context = null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3, Context ctx) {
Title = text;
Detail = text1;
imge = text3;
context = ctx;
}
public final Context context;
public dataListAdapter(Context mContext){
this.context = mContext;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView titleView, detailView;
ImageView imageView;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.custom, parent,false);
titleView = (TextView) convertView.findViewById(R.id.title);
detailView = (TextView) convertView.findViewById(R.id.detail);
imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(R.id.title, titleView);
convertView.setTag(R.id.detail, detailView);
convertView.setTag(R.id.image, imageView);
}else{
titleView = (TextView) convertView.getTag(R.id.title);
detailView = (TextView) convertView.getTag(R.id.detail);
imageView = (ImageView) convertView.getTag(R.id.image);
}
titleView.setText(Title[position]);
detailView.setText(Detail[position]);
imageView.setImageDrawable(context.getResources().getDrawable(imge[position]));
return convertView;
}
}
Logcat:
01-10 21:20:43.290 26874-26874/com.robigroza.justlisttest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.robigroza.justlisttest.MainActivity$dataListAdapter.getView(MainActivity.java:91)
at android.widget.AbsListView.obtainView(AbsListView.java:2465)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:15371)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
at android.view.View.measure(View.java:15371)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2011)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1250)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1425)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Here's the custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#339966"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video1"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Here's the main.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>

Your custom.xml should contain 2 TextViews (title and detail) and an ImageView (image).
E.g. something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"/>
</LinearLayout>

Try this.
Imagview.setImageResource (img [position]);

Related

findViewById() may produce NullPointerException for getView() in HomeFragment.java

public class HomeFragment extends Fragment implements HomeView{
private HomeViewModel homeViewModel;
public static final String EXTRA_CATEGORY = "category";
public static final String EXTRA_POSITION = "position";
public static final String EXTRA_DETAIL = "detail";
#BindView(R.id.viewPagerHeader)
ViewPager viewPagerMeal;
#BindView(R.id.recyclerCategory)
RecyclerView recyclerViewCategory;
HomePresenter presenter;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
super.onCreate(savedInstanceState);
ButterKnife.bind(getActivity());
presenter = new HomePresenter(this);
presenter.getMeals();
presenter.getCategories();
return root;
}
#Override
public void showLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.VISIBLE);
}
#Override
public void hideLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.GONE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.GONE);
}
#Override
public void setMeal(List<Meals.Meal> meal) {
ViewPagerHeaderAdapter headerAdapter = new ViewPagerHeaderAdapter(meal, getActivity());
viewPagerMeal.setAdapter(headerAdapter);
viewPagerMeal.setPadding(20, 0, 150, 0);
headerAdapter.notifyDataSetChanged();
headerAdapter.setOnItemClickListener((view, position) -> {
TextView mealName = view.findViewById(R.id.mealName);
Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
intent.putExtra(EXTRA_DETAIL,mealName.getText().toString());
startActivity(intent);
});
}
#Override
public void setCategory(List<Categories.Category> category) {
RecyclerViewHomeAdapter homeAdapter = new RecyclerViewHomeAdapter(category, getActivity());
recyclerViewCategory.setAdapter(homeAdapter);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3,
GridLayoutManager.VERTICAL, false);
recyclerViewCategory.setLayoutManager(layoutManager);
recyclerViewCategory.setNestedScrollingEnabled(true);
homeAdapter.notifyDataSetChanged();
homeAdapter.setOnItemClickListener((view, position) -> {
Intent intent = new Intent(getActivity(), CategoryActivity.class);
intent.putExtra(EXTRA_CATEGORY, (Serializable) category);
intent.putExtra(EXTRA_POSITION, position);
startActivity(intent);
});
}
#Override
public void onErrorLoading(String message) {
Utils.showDialogMessage(getActivity(), "Title", message);
}
}
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}
public LiveData<String> getText() {
return mText;
}
}
fragment_home.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground">
<RelativeLayout
android:id="#+id/headerView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp">
<View
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#color/colorPrimary" />
<TextView
android:id="#+id/title"
android:textColor="#color/colorWhite"
android:text="#string/app_name"
android:textStyle="bold"
android:textSize="30sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.CardView
android:id="#+id/cardSearch"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="15dp"
app:cardBackgroundColor="#color/colorPrimaryDark"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp"
android:text="#string/search_your_recipes"
android:textColor="#color/colorPrimaryLight"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_search_primary_light"
android:drawableStart="#drawable/ic_search_primary_light"
android:drawablePadding="10dp"/>
</android.support.v7.widget.CardView>
<android.support.v4.view.ViewPager
android:id="#+id/viewPagerHeader"
android:foregroundGravity="center"
android:overScrollMode="never"
android:clipToPadding="false"
android:layout_below="#id/cardSearch"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="150dp" />
<include android:id="#+id/shimmerMeal"
layout="#layout/item_view_pager_header_shimmer" />
<TextView
android:id="#+id/titleCategory"
android:text="#string/meal_categories"
android:textSize="19sp"
android:textColor="#color/colorPrimary"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/viewPagerHeader"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerCategory"
android:scrollbars="none"
android:layout_marginTop="10dp"
android:clipToPadding="false"
android:layout_below="#id/titleCategory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="50dp" />
<include android:id="#+id/shimmerCategory"
layout="#layout/item_recycler_category_shimmer" />
<TextView
android:padding="10dp"
android:gravity="center"
android:textColor="#color/colorWhite"
android:text="#string/app_name"
android:background="#color/colorPrimaryLight"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Here is Logcat file:
2019-12-20 18:04:26.459 4910-4910/com.vikaskonaparthi.origin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vikaskonaparthi.origin, PID: 4910
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.vikaskonaparthi.origin.ui.home.HomeFragment.showLoading(HomeFragment.java:63)
at com.vikaskonaparthi.origin.ui.home.HomePresenter.getMeals(HomePresenter.java:29)
at com.vikaskonaparthi.origin.ui.home.HomeFragment.onCreateView(HomeFragment.java:56)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:874)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6729)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Getting error with the shimmer in HomeFragmentActivity how to resolve this issue?
Should showloading() be kept in FragmentViewModel?
Since I had converted Activity to the fragment here.
The issue is not with the shimmer.
The issue is your getView() in getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE);
is null.
If you are having your shimmer views in fragment then you need to call showLoading() after your fragments onViewCreated() method is called.
I think you must put everything that connects with layout, like constraints and others widget when onViewCreated() is called.
Call it below your onCreateView(). This error happens because of calling widget but the widget itself not yet exist or created by fragment. Widgets will exist in onViewCreated(). Tell me if it's work

I get a Null Reference on my Recycler View

I'm sorry if my english isn't perfect.
I am learning Java and Android Studio since 2 weeks, i'm trying to add sliding tabs, and inside a tab, a RecyclerView with CardViews as a row item having a name and a picture.
My Sliding tabs works, and I can put a cardview in tabs, with a picture and a name. But when i use the RecyclerView, it doesn't work and i have an error. I can't resolve this problem since yesterday.
Here my program.
My Class Person
public class Person {
String name;
int photoId;
public Person(String name, int photoId) {
this.name = name;
this.photoId = photoId;
}
private List<Person> persons;
private void initializeData(){
persons = new ArrayList<>();
persons.add(new Person("Benjamin", R.drawable.team));
persons.add(new Person("Thomas", R.drawable.haltere));
}
//GETTER
public String getName() {
return this.name;
}
public int getPhoto() {
return this.photoId;
}
My Class Adapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardView);
personName = (TextView)itemView.findViewById(R.id.cardTitle);
personPhoto = (ImageView)itemView.findViewById(R.id.cardPhoto);
}
}
List<Person> persons;
public RVAdapter(List<Person> persons){
this.persons = persons;
}
#Override
public int getItemCount() {
return persons.size();
}
#Override
public PersonViewHolder onCreateViewHolder (ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
return new PersonViewHolder(v);
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
My Activity
public class ActivityHome extends AppCompatActivity {
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"1","2","3","4"};
int Numboftabs =4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView rv = (RecyclerView)findViewById(R.id.my_recycler_view);
ArrayList<Person> persons = new ArrayList<>();
RVAdapter rAdapter = new RVAdapter(persons);
rv.setAdapter(rAdapter);
rv.setLayoutManager(layoutManager);
}
(There is a few code after, but it only uses for the Sliding Tabs, and it works.)
My recycler_view.xml
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
My activity_home.xml
<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
And my cardview.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#ffffff"
app:cardCornerRadius="5dp"
app:cardElevation="7dp">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityHome"
android:background="#ffffff">
<TextView
android:id="#+id/cardTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Card Title"
android:textColor="#000000"
android:textSize="17sp" />
<ImageView
android:id="#+id/cardPhoto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="50dp"
android:src="#drawable/haltere"
android:scaleType="center"
/>
</RelativeLayout>
My error :
Process: com.android.frutii.fitness5, PID:640 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.frutii.fitness5/com.android.frutii.fitness5.ActivityHome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at com.android.frutii.fitness5.ActivityHome.onCreate(ActivityHome.java:45)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526) 
at android.app.ActivityThread.access$800(ActivityThread.java:169) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5549) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
I have read a lot of answers on internet but cannot get rid of this error.
Activity.findViewById() method is searching for my_recycler_view inside the layout which was set with setContentView(). So you should move RecyclerView to R.layout.activity_home layout.
Your RecyclerView with Id R.id.my_recycler_view does not exist in the activity_home layout. Use the R.layout.recycler as layout the contentView in setContentView();
I move my RecyclerView.xml, i put the code in activity_home.xml like this :
<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
</android.support.v4.view.ViewPager>
And now i'ts work partially, I have my 4 tabs, and I have a cardview too, but only one :( Why my second is not here ? Maybe this is not the good thing and I don't have to go in this direction.. ?

Call another activity from button on camera Android

I'm implementing a Camera app on Android, I'd like to have a button on it which will lead the user to the developer website, or my website.
This button is placed right of the shutter button.
I was trying to call the webview activity from this button, but it's giving me errors and I'm confused on this case, because there are plenty of examples of calling an activity inside another one, but no from a camera app.
I don't know what am I doing wrong.
Here's the piece of code in MainActivity:
public class MainActivity extends Activity /**implements OnClickListener**/ {
ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
Button webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
webButton = (Button) findViewById(R.id.imageView_world);
fotoButton = (ImageView) findViewById(R.id.imageView_foto);
exitButton = (Button) findViewById(R.id.button_exit);
image = (ImageView) findViewById(R.id.imageView_photo);
progressLayout = (LinearLayout) findViewById(R.id.progress_layout);
preview = new Preview(this,
(SurfaceView) findViewById(R.id.KutCameraFragment));
FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
frame.addView(preview);
preview.setKeepScreenOn(true);
fotoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
takeFocusedPicture();
} catch (Exception e) {
}
exitButton.setClickable(false);
fotoButton.setClickable(false);
webButton.setClickable(true);
progressLayout.setVisibility(View.VISIBLE);
}
});
webButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
Everytime I run it it throws me this error:
4-04 00:33:43.929 4237-4237/com.kut.kutcamera E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kut.kutcamera/com.kut.kutcamera.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
at com.kut.kutcamera.MainActivity.onCreate(MainActivity.java:56)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
The layout declaration of the world button is just Okay, I don't really think it's because of that, I guess there is something inside that camera method that doesn't allows me to properly make the call.
Can anybody shed some light on this?
Thanks in advance!
EDIT
activiy_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<SurfaceView
android:id="#+id/KutCameraFragment"
android:name="com.kut.camera.KutCameraFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="#+id/rel_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="1"
android:background="#android:color/black"
android:orientation="vertical"
android:padding="10dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textViewReferan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Photo"
android:textColor="#android:color/white"
android:textSize="16sp" />
<Button
android:id="#+id/button_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:text="Ok"
android:textColor="#2799CF" />
</RelativeLayout>
<LinearLayout
android:id="#+id/progress_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/islem_value_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..." />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:alpha="0.9"
android:background="#android:color/black"
android:padding="10dp" >
<ImageView
android:id="#+id/imageView_foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/camera"
android:contentDescription="1" />
<ImageView
android:id="#+id/imageView_photo"
android:layout_width="80dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/fotocekicon"
android:contentDescription="2" />
<ImageView
android:id="#+id/imageView_world"
android:layout_width="80dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/world"
android:contentDescription="2" />
</RelativeLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mark3"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</FrameLayout>
</FrameLayout>
Hi I have update your activity class, just replace with my code let me know you still face the problem.
public class MainActivity extends Activity /**implements OnClickListener**/ {
ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
ImageView webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
webButton = (ImageView) findViewById(R.id.imageView_world);
fotoButton = (ImageView) findViewById(R.id.imageView_foto);
exitButton = (Button) findViewById(R.id.button_exit);
image = (ImageView) findViewById(R.id.imageView_photo);
progressLayout = (LinearLayout) findViewById(R.id.progress_layout);
preview = new Preview(this,
(SurfaceView) findViewById(R.id.KutCameraFragment));
FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
frame.addView(preview);
preview.setKeepScreenOn(true);
fotoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
takeFocusedPicture();
} catch (Exception e) {
}
exitButton.setClickable(false);
fotoButton.setClickable(false);
webButton.setClickable(true);
progressLayout.setVisibility(View.VISIBLE);
}
});
webButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
Thank you

GridView.FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: String resource ID #0x0

I want that when I click on an element of GridView displayed letter, which I clicked. But initially I got here error when I wanted to take the position of the element. If I understood correctly, gridview does not see the textview, but i don't know why.
Error:
5312-5312/standandroid.ru.words E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:240)
at android.content.res.MiuiResources.getText(MiuiResources.java:131)
at android.widget.Toast.makeText(Toast.java:273)
at standandroid.ru.words.MainActivity$1$1.onItemClick(MainActivity.java:70)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1130)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2818)
at android.widget.AbsListView$1.run(AbsListView.java:3498)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)
MainActivity.java :
public class MainActivity extends Activity {
TextView txt, textTV;
EditText etxt;
Button btn;
ArrayAdapter<String> adapter;
GridView gridView;
String name;
char c;
ArrayList arrayList=new ArrayList();
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.textView);
etxt = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
textTV = (TextView) findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = etxt.getText().toString();
TreeSet list = new TreeSet();
for (i = 0; i < name.length(); i++) {
c = name.charAt(i);
list.add(c);
}
arrayList = new ArrayList(list);
adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.item, R.id.tv, arrayList);
gridView = (GridView) findViewById(R.id.gridmain);
gridView.setAdapter(adapter);
ViewGrid();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, position,Toast.LENGTH_LONG).show();
}
});
}
});}
private void ViewGrid(){
gridView.setNumColumns(4);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); }}
activity_main.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="175dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="48dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<GridView
android:id="#+id/gridmain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:layout_alignTop="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></GridView>
</RelativeLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv"
android:textSize="40sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
http://i.stack.imgur.com/j5S93.png
Ohh! I see, the issue is that Toast is thinking of position as a resourceID. If you see Toast specs here, you would see that you can define resourceId or a CharSequence. So make sure you are passing the position as a String. Something like:
Toast.makeText(MainActivity.this, String.valueOf(position),Toast.LENGTH_LONG).show();

Populate spinner with key value pair in android fragment

I am trying to create a spinner with key value pair in fragment. I am getting this logcat output:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ica.icadashboard/com.ica.icadashboard.HomeActivity}: java.lang.NullPointerException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
android.app.ActivityThread.access$600(ActivityThread.java:141)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5041)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
dalvik.system.NativeStart.main(Native Method)
05-20 08:06:36.247: E/AndroidRuntime(2746): Caused by: java.lang.NullPointerException
android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
com.ica.placement.PlacementFragment.setSpinnerContent(PlacementFragment.java:44)
com.ica.placement.PlacementFragment.onCreateView(PlacementFragment.java:29)
android.app.Fragment.performCreateView(Fragment.java:1695)
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
android.app.BackStackRecord.run(BackStackRecord.java:682)
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
android.app.Activity.performStart(Activity.java:5113)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
05-20 08:06:36.247: E/AndroidRuntime(2746): ... 11 more
The fragment class:
public class PlacementFragment extends Fragment {
Spinner adm_spinner;
Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_placement, container, false);
// Now use the above view to populate the spinner.
setSpinnerContent( view );
return container;
}
private void setSpinnerContent( View view )
{
adm_spinner = (Spinner) view.findViewById( R.id.adm_spinner );
final MyData items[] = new MyData[4];
for (int i = 0; i <= 3; i++) {
items[i] = new MyData("value " + i, "" + i);
}
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adm_spinner.setAdapter(adapter);
// spinner.setAdapter( adapter );
}
class MyData {
public MyData(String spinnerText, String value) {
this.spinnerText = spinnerText;
this.value = value;
}
public String getSpinnerText() {
return spinnerText;
}
public String getValue() {
return value;
}
public String toString() {
return spinnerText;
}
String spinnerText;
String value;
}
}
The layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Placement Analysis"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:paddingTop="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="#+id/tp_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
<Spinner
android:id="#+id/adm_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="#+id/centre_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
<Spinner
android:id="#+id/vertical_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/calendar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calendar" />
<Button
android:id="#+id/process_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Process" />
</LinearLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
What is the problem? Where am I going wrong? How should I populate the spinner?
You should
return view;
instead of
return container;
in your onCreateView(.....) in PlacementFragment
also change this
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
android.R.layout.simple_spinner_item, items);
to
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(getActivity(),
android.R.layout.simple_spinner_item, items);
Caused by: java.lang.NullPointerException
android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
Apart form the other answer you need to change to
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(getActivity(),
android.R.layout.simple_spinner_item, items);
Activity activity; activity is not initialized.
Try this..
You have not initialize Activity activity initialize it as
activity = getActivity();

Categories