I'm trying to implement Actionbar-PullToRefresh library into my project. Followed the guide, but couldn't get through it.
XML layout:
<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/clouds" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:dividerHeight="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:layout_centerInParent="true"
android:textAlignment="center" />
</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
Fragment:
public class LatestFragment extends ListFragment implements OnScrollListener, OnRefreshListener {
private PullToRefreshLayout mPullToRefreshLayout;
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup viewGroup = (ViewGroup) view;
mPullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext());
ActionBarPullToRefresh.from(getActivity())
.insertLayoutInto(viewGroup)
.theseChildrenArePullable(getListView(), getListView().getEmptyView())
.listener(this)
.setup(mPullToRefreshLayout);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
loadItemList(1);
}
#Override
public void onRefreshStarted(View view) {
loadItemList(1);
}
private void loadItemList(int page) {
...
}
...
}
I'm getting this error:
04-09 18:23:35.739 12681-12681/kedai.cryptomarket.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoSuchMethodError: fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable$Builder.width
at uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer.applyProgressBarSettings(DefaultHeaderTransformer.java:372)
at uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer.onViewCreated(DefaultHeaderTransformer.java:104)
at uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.<init>(PullToRefreshAttacher.java:120)
at uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout.createPullToRefreshAttacher(PullToRefreshLayout.java:223)
at uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh$SetupWizard.setup(ActionBarPullToRefresh.java:92)
at kedai.cryptomarket.app.fragment.drawer.LatestFragment.onViewCreated(LatestFragment.java:65)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:952)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
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:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
What's weird is the error indicates it came from SmoothProgressBar which I have already implemented in my project (using version 0.4.0) but didn't use it in this Fragment, however the error is pointing to ActionBarPullToRefresh line in code.
What did I missed?
If you use gradle, try to delete the dependency to smoothprogressbar in your build.gradle file.
The dependency to actionbarpulltorefresh will import smootprogressbar too.
I had the same problem and that worked for me.
ActionBar-PullToRefresh 0.9.3 does not support SmoothProgressBar 0.4.0. Use must use an older version SmoothProgressBar 0.2.2.
Related
I try to make my StartActivity class can dynamically change from one Fragment to another, such as first it will open up LoginFragment. If user does not have any account they could register new account. Login and register would be two different fragments in a single activity.
Here's the StartAcitivity.class
public class StartActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.flDetailContainer, new LoginFragment(), "LOGIN")
.commit();
}
}
}
My LoginFragment.class
public class LoginFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_login, parent, false);
}
}
And the activity_start.xml layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/flDetailContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
</FrameLayout>
My fragment_login.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
And the error I got.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asus.carclubapps, PID: 2748
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.carclubapps/com.example.asus.carclubapps.StartActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0c006f (com.example.asus.carclubapps:id/activity_start) for fragment RegistrationFragment{85ef24b #0 id=0x7f0c006f}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c006f (com.example.asus.carclubapps:id/activity_start) for fragment RegistrationFragment{85ef24b #0 id=0x7f0c006f}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1098)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I do have RegistrationFragment and fragment_registration, but the thing is on my StartActivity I only call for LoginFragment not RegistrationFragment yet. Am I missing something here?
RegistrationFragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Asus on 11/27/2016.
*/
public class RegistrationFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_registration, parent, false);
}
}
fragment_registration
<?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:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_registration"/>
</LinearLayout>
The logcat clearly indicates that the problem is in your RegistrationFragment in the activity_start.xml layout.
No view found for id 0x7f0c006f (com.example.asus.carclubapps:id/activity_start) for fragment RegistrationFragment{85ef24b #0 id=0x7f0c006f}
It also says you're referring to an invalid view which doesn't really exists in your activity_start.xml layout.
Here's your activity_start.xml layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/flDetailContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
</FrameLayout>
I see one and only FrameLayout with id flDetailContainer which you might not want to get from your RegisterFragment. I think you're getting wrong view inside your RegisterFragment where you're getting the view from layout using LayoutInflater.
I guess, there's a layout called fragment_register.xml. You need to get the view from that layout in your RegisterFragment.
In that case your onCreateView function of the RegisterFragment may look like
public class RegisterFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_register, parent, false);
}
}
the error is coming from another Activity, Mainly the Registerfragment. The view is incorrect.
Unable to start activity --> No view found for fragment RegistrationFragment
If you fix the view and the problem is persistant, kindly update us and we will work it out promptly.
From Logs its clear in your RegistrationFragment you are trying to access some view which actually not exist in your attached layout file of fragment.
So confirm in your RegistrationFragment that you attached correct view. Because generally this error(View not found) comes when you trying to use view which actually not exist with current attached view.
If still not works then try to invalidate cache and restart you Android Studio.
I am facing java.lang.IllegalStateException Required view 'splash_text' but I have included it in the xml.
I am using Butterknife to Bind the views.
compile 'com.jakewharton:butterknife:7.0.1'
Xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<com.CustomTextView
android:id="#+id/splash_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Activity :
#Bind(R.id.splash_text)
CustomTextView mSplashTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ButterKnife.bind(this);
mSplashTv.setText("Splash");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finishSplash();
}
},3000);
}
Application is crashing at the line mSplashTv.setText("Splash");
Log :
Caused by: java.lang.IllegalStateException: Required view 'splash_text' with ID 2131492944 for field 'mSplashTv' was not found. If this view is optional add '#Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at com.sonymix.activities.SplashActivity$$ViewBinder.bind(SplashActivity$$ViewBinder.java:12)
at com.sonymix.activities.SplashActivity$$ViewBinder.bind(SplashActivity$$ViewBinder.java:9)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.sonymix.activities.BaseActivity.onCreate(BaseActivity.java:16)
at com.sonymix.activities.SplashActivity.onCreate(SplashActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access$700(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
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:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Update:
BaseActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
}
Support Library used:
compile 'com.android.support:appcompat-v7:23.1.1'
Remove binding from base activity. When ButterKnife bind in base class, your layout not inflated yet.
Also you can add #Nullable annotation to field.
It's because your layout is not inflated yet. If you are using BaseActivity, do something like this :
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(this.getLayoutContentViewID());
ButterKnife.bind(this); //Configure Butterknife
}
public abstract int getLayoutContentViewID();
}
Next, in your MainActivity :
public class MainActivity extends BaseActivity {
#BindView(R.id.main_activity_coordinator_layout) CoordinatorLayout coordinatorLayout;
...
#Override
public int getLayoutContentViewID() { return R.layout.activity_main; }
...
}
Check your support Library version. I was getting the above error since i upgraded support library to 23 version. may be that is causing error.NavigationView get/find header layout
I get following error (using the newest support library v23.0 and build SDK 23):
Case 1: Android 4.2.1
java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:523)
at android.view.View.measure(View.java:15612)
at android.support.v7.internal.widget.ListViewCompat.measureHeightOfChildrenCompat(ListViewCompat.java:301)
at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1200)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:17439)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5341)
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:929)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
at dalvik.system.NativeStart.main(Native Method)
Case 2: Android 4.3
java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:578)
at android.view.View.measure(View.java:16831)
at android.support.v7.internal.widget.ListViewCompat.measureHeightOfChildrenCompat(ListViewCompat.java:301)
at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1200)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
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:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Actually I don't know where to start searching. I get this error from crashes and can't reproduce it on my phone. And I don't know where it really comes from.
I searched my code and I don't have a Spinner directly nested in any RelativeLayout...
Does anyone have a hint what can cause this problem?
Changes in my code I made
Mainly I updated from support library and build sdk v22 to v23. And I never had a problem like this before...
Reason for error, demonstrated based on the error in Android 4.2.1:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.1_r1.2/android/widget/RelativeLayout.java#RelativeLayout
Here in Line 523... That's what I get from the bug report I receive...
523 if (mLayoutParams.height >= 0) {
524 height = Math.max(height, mLayoutParams.height);
525 }
In android 4.3 it's actually the same problem, mLayoutParams is null...
I found the reason.
I'm using many adapters and in one of them I accidently did attach the view to the root view... This appearently is no problem on most devices and android versions, but on some...
convertView = mInflater.inflate(mLayoutRes, null);
Should be
convertView = mInflater.inflate(mLayoutRes, null, false);
if the adapter is used in a Spinner...
Try using the following code:
activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp" />
</RelativeLayout>
MainActivity.java:
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(),country[position] ,Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You catch the exception incase you inflate layout which has only a view (without ViewGroup).
Example
android:layout/simple_spinner_dropdown_item has only CheckedTextView:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
You can fix it by add an ViewGroup in layout of your adapter item view :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#android:layout/simple_spinner_dropdown_item"/>
</LinearLayout>
#prom85 solution did not work for me till I passed the parent ViewGroup instead of null:
convertView = mInflater.inflate(mLayoutRes, parent, false);
Hello there am crawling into this android world slowly and following the android bootcamp exercise 2012 edition. I am able to test my application on the AVm with just one layout and one class however on introducing the second class, second layout and coding the button, the application does not open and the log cat error looks like this
06-18 17:22:29.461: D/AndroidRuntime(542): Shutting down VM
06-18 17:22:29.461: W/dalvikvm(542): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
06-18 17:22:29.471: E/AndroidRuntime(542): FATAL EXCEPTION: main
06-18 17:22:29.471: E/AndroidRuntime(542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.Main}: java.lang.NullPointerException
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.os.Looper.loop(Looper.java:137)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.ActivityThread.main(ActivityThread.java:4340)
06-18 17:22:29.471: E/AndroidRuntime(542): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 17:22:29.471: E/AndroidRuntime(542): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 17:22:29.471: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-18 17:22:29.471: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-18 17:22:29.471: E/AndroidRuntime(542): at dalvik.system.NativeStart.main(Native Method)
06-18 17:22:29.471: E/AndroidRuntime(542): Caused by: java.lang.NullPointerException
06-18 17:22:29.471: E/AndroidRuntime(542): at com.example.helloworld.Main.onCreate(Main.java:24)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.Activity.performCreate(Activity.java:4465)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-18 17:22:29.471: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
06-18 17:22:29.471: E/AndroidRuntime(542): ... 11 more
this is the main.java
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.btnPlay);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Recipe.class));
// TODO Auto-generated method stub
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
this is the new class
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class Recipe extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe);
}
}
this is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.helloworld.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Recipe"></activity>
</application>
</manifest>
activity_main
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.helloworld.Main"
tools:ignore="MergeRootFrame" />
fragment_main
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.helloworld.Main$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="#string/mduduzi_games"
android:textSize="30sp" />
<Button
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="#string/play" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:src="#drawable/anim4" />
</RelativeLayout>
From your stack trace, there is a NullPointerException on line 24 of Main.java:
at com.example.helloworld.Main.onCreate(Main.java:24)
This is your problem:
Button b = (Button)findViewById(R.id.btnPlay);
//b is null, so the next line throws a NullPointerException:
b.setOnClickListener(...
b is null, meaning there is no Button with the id "btnPlay" in activity_main.xml
Check to make sure you have something like this in activity_main.xml:
<Button
android:layout_width="wrap_content"
android:text="Some text"
android:id="#+id/btnPlay"
android:layout_height="wrap_content" />
EDIT:
You are calling findViewById from your Activity, which is searching amongst the Views of your Activity defined by activity_main.xml. Since you have defined your Button in your Fragment layout, you must get a reference to the Button from within your Fragment code. Remove the Button code from your Activity class, and stick it into your Fragment:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//find your Button view in rootView
Button b = (Button)rootView.findViewById(R.id.btnPlay);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Recipe.class));
// TODO Auto-generated method stub
}
});
return rootView;
}
}
While you are on the right path, you need to change a line slightly in your manifest. Try:
<activity android:name="com.example.helloworld.Recipe"></activity>
Instead of
<activity android:name="Recipe"></activity>
EDIT: Actually, this doesn't seem to be the source of your current problem, but will be a source of a future problem once your current NullPointerException is fixed. As stated in other answers, it looks to deal with the button you set to navigate to your new Activity.
The problem is that btnPlay is defined in your Fragment's layout, but you are trying to find it in your Activity before the Fragment is added to the Activity.
When you call findViewById(R.id.btnPlay);, the only thing that has been added to your Activity's layout is activity_main, which only contains a FrameLayout. The Fragment containing btnPlay doesn't exist until you call
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
One solution would be to move the above three lines to before Button b = (Button)findViewById(R.id.btnPlay);.
However, it is generally a bad practice to reference Views defined in a Fragment from an Activity, because it is a very poor separation of concerns. Your Fragment should handle of the user interaction with Views belonging to that Fragment, and your Activity should only be concerned with Views defined in the Activity's layout.
Please help. What happens? What is the cause of error, and how could I solve it? Thanks for your help!
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.interviewhelpers/com.example.interviewhelpers.Dashboard}:
> android.view.InflateException: Binary XML file line #192: Error
> inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #192: Error inflating class fragment
This is my code: Main class...
public class Dashboard extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
activity_dashboard... Has more elements and layouts, and this:
<fragment
android:id="#+id/listFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:name="com.example.interviewhelpers.ClientDetailActivity" ></fragment>
And...
package com.example.interviewhelpers;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ClientDetailActivity extends FragmentActivity {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.cliente_detailed, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
return view;
}
}
(Always try to use the support libraries as you are doing)
First:
1. Should use a FragmentActivity and NO Activity
2. Should use a Fragment to the child components you'll be using.
3. On the Fragments on the onCreateView just do the Inflate
4. On the Fragment use the onViewCreated to find the TextView.
Sample:
//This is the Activity
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//This is the layout.activity_main
<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" >
<fragment
android:name="com.example.fragmentapp.ChildFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
//This is the Fragment to be shown
public class ChildFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.child_frag, null);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView tvText = (TextView)view.findViewById(R.id.textView1);
tvText.setText("Found!");
}
}
//This is the layout for the fragment layout.child_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>