How to start Activity in my adapter? - java

How can I start activity in adapter? I use this method in my adapter:
post.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
context.startActivity(new Intent(context,asabani_cat.class));
}
});
But this worked just for fragment of my app, when I called this code in Activity App suddenly crashed!
Another question that I have, is it the good way to start activity in adapter?
my logcat:
11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: FATAL EXCEPTION: main 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: java.lang.NullPointerException 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.katibehpayam.mahdi.katibehpayam.adapter_common$7.onClick(adapter_common.java:266) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.view.View.performClick(View.java:4377) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18044) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

You could try:
post.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(v.getContext(),asabani_cat.class));
}
});

You should post the error from the Logcat so we can help you further. However from what you've shown I can find nothing inherently wrong with your code. Make sure your context is set correctly. Furthermore if you are already in an Activity you do not need to say
context.startActivity(new Intent(context,asabani_cat.class));
you can simply say
startActivity(new Intent(this, asabani_cat.class));
If you are not in an Activity then you need to pass the context like so
MyAdapter myAdapter = new MyAdapter(this);
Then inside your adapter set your context like this:
MyAdapter(Context context) {
this.context = context;
}
EDIT
Your logcat shows a null pointer exception is the cause of your crash. Can you please post the code from adapter_common.java at line 266? Thanks

In kotlin, on your view:
class YourClass: ArrayAdapter<Feature>(), View.OnClickListener{
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var yourView = inflater.inflate(mResource, parent, false)
yourView .setOnClickListener(){
onClick(convertView)
}
}
override fun onClick(v: View?) {
val intent = Intent(mContext, SeckondActivity::class.java)
mContext.startActivity(intent)
}
}

Related

How to access a button from in a another layout in a Fragment

i am trying to get a button from another layout in fragment.this fragment have another inflate another layout.i get a java.lang.NullPointerException. how to solve this problem?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.add_to_cart_display, container, false);
myAdapter=new MyAdapter(getActivity());
Button remove=(Button) view.findViewById(R.id.remove_cart_items);
RecyclerView recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
ch=new CartHelper(getActivity());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
rc=new RCadapter(getDataset());
recyclerView.setAdapter(rc);
return view;
}
from above pgm the remove button not in the add_cart_display xml file. it locate in another layout file. here i am accessing this button.it give the following error.
10-31 12:38:20.484 13886-13886/com.infizoom.infishopping
E/AndroidRuntime: FATAL EXCEPTION: main 10-31 12:38:20.484
13886-13886/com.infizoom.infishopping E/AndroidRuntime: Process:
com.infizoom.infishopping, PID: 13886 10-31 12:38:20.484
13886-13886/com.infizoom.infishopping E/AndroidRuntime:
java.lang.NullPointerException 10-31 12:38:20.484
13886-13886/com.infizoom.infishopping E/AndroidRuntime: at
com.infizoom.infishopping.add_cart_class.onStart(add_cart_class.java:75)
10-31 12:38:20.484 13886-13886/com.infizoom.infishopping
E/AndroidRuntime: at
android.app.Fragment.performStart(Fragment.java:1724) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
10-31 12:38:20.484 13886-13886/com.infizoom.infishopping
E/AndroidRuntime: at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
10-31 12:38:20.484 13886-13886/com.infizoom.infishopping
E/AndroidRuntime: at
android.app.BackStackRecord.run(BackStackRecord.java:698) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
10-31 12:38:20.484 13886-13886/com.infizoom.infishopping
E/AndroidRuntime: at
android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at android.os.Handler.handleCallback(Handler.java:808) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at android.os.Handler.dispatchMessage(Handler.java:103) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at android.os.Looper.loop(Looper.java:193) 10-31 12:38:20.484
13886-13886/com.infizoom.infishopping E/AndroidRuntime: at
android.app.ActivityThread.main(ActivityThread.java:5341) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at java.lang.reflect.Method.invokeNative(Native Method) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at java.lang.reflect.Method.invoke(Method.java:515) 10-31 12:38:20.484
13886-13886/com.infizoom.infishopping E/AndroidRuntime: at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
10-31 12:38:20.484 13886-13886/com.infizoom.infishopping
E/AndroidRuntime: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) 10-31
12:38:20.484 13886-13886/com.infizoom.infishopping E/AndroidRuntime:
at dalvik.system.NativeStart.main(Native Method)
add_to_cart_display.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_marginTop="#dimen/abc_action_bar_default_height_material"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recyclerView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>
Remove Button inside the CardView
final View view = inflater.inflate(R.layout.add_to_cart_display, container, false);
Button remove=(Button) view.findViewById(R.id.remove_cart_items);
you are looking for a Button with id remove_cart_items inside add_to_cart_display. But in layout may be there is no Button with id remove_cart_items. That`s way you get null
I got an answer. i can access the button inside the cardview by recyclerAdapter onBindViewHolder(). but i dnot know how to call toast in side this function.
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView product_name_at_cart;
public Button remove;
public ViewHolder(View layoutview) {
super(layoutview);
remove =(Button) layoutview.findViewById(R.id.remove_cart_items);
product_name_at_cart=(TextView)layoutview.findViewById(R.id.add_cart_item_name);
}
}
OnBindViewHolder
public void onBindViewHolder(ViewHolder viewholder, int position) {
viewholder.product_name_at_cart.setText(mDataset.get(position).getProduct_name());
viewholder.remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.w("gopal", "Gopal btn click");
Cursor m=ch.getcurrent_id();
if(m.moveToFirst()){
do{
int id=m.getInt(5);
Toast.makeText(???????, "Gopal id " + id, Toast.LENGTH_SHORT).show();
}while (m.moveToNext());
}
}
});
}
What function will came in the Toast Msg.????
In RecyclerAdapter, I have defined a private Activity context.
Then, you can call the Toast.makeText(context, "your message", ...),

Null Pointer Exception while setting value of TextView in an Array Adapter class - Android

I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
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:5103)
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:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file

Android - Switching activity with different implementation

I have 2 classes in Java and they are 2 different activities that I'd like to switch with a button click.
The problem is that when I do click the button the app crashes.
my MainActivity extends Activity and my SecondActivity extends Activity AND implements SensorEventListener. So I think this is the problem. My MainActivity CANNOT implement SensorEventListener because I am using an external sensor.
This is my MainActivity
public class MainActivity extends Activity {
//My Code goes here.
//This it the function that gets called from XML's onClick feature when I click my Button
public void gotoPathFinder(View v) {
Intent intent = new Intent(this, pathfinderActivity.class);
startActivity(intent);
}
}
This is my SecondActivity
package ca.concordia.sensortag.minimal;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class pathfinderActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
mSensorManager = (SensorManager) getSystemService(this.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
#Override
public void onSensorChanged(SensorEvent event) {
// Sensor sensor = event.sensor;
// float[] values = event.values;
// int value = -1;
//
// if (values.length > 0) {
// value = (int) values[0];
// }
//
// if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
// textView.setText("Step Counter Detected : " + value);
// } else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
// textView.setText("Step Detector Detected : " + value);
// }
}
#Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mStepCounterSensor,SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepDetectorSensor,SensorManager.SENSOR_DELAY_FASTEST);
}
#Override
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
So I think the app crashes because I'm trying to switch to an activity that implements sensoreventlistener from an activity that doesn't? How can I fix this issue?
LogCat
11-27 20:00:07.312: W/dalvikvm(31178): threadid=1: thread exiting with uncaught exception (group=0x41604ba8)
11-27 20:00:07.312: E/AndroidRuntime(31178): FATAL EXCEPTION: main
11-27 20:00:07.312: E/AndroidRuntime(31178): Process: ca.concordia.sensortag.minimal, PID: 31178
11-27 20:00:07.312: E/AndroidRuntime(31178): java.lang.IllegalStateException: Could not find a method gotoPathFinder(View) in the activity class ca.concordia.sensortag.minimal.pathfinderActivity for onClick handler on view class android.widget.ImageView with id 'pf_button'
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$1.onClick(View.java:3810)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View.performClick(View.java:4438)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$PerformClick.run(View.java:18422)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Handler.handleCallback(Handler.java:733)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Handler.dispatchMessage(Handler.java:95)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Looper.loop(Looper.java:136)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 20:00:07.312: E/AndroidRuntime(31178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-27 20:00:07.312: E/AndroidRuntime(31178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-27 20:00:07.312: E/AndroidRuntime(31178): at dalvik.system.NativeStart.main(Native Method)
11-27 20:00:07.312: E/AndroidRuntime(31178): Caused by: java.lang.NoSuchMethodException: gotoPathFinder [class android.view.View]
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.Class.getConstructorOrMethod(Class.java:472)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.Class.getMethod(Class.java:857)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$1.onClick(View.java:3803)
11-27 20:00:07.312: E/AndroidRuntime(31178): ... 11 more
NOTE: When I click the button it brings me to the SAME activity... and then i click the button again THEN i get an error
MainActivity's 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="#drawable/bg"
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="ca.concordia.sensortag.minimal.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/compass" />
<ImageView
android:id="#+id/pointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/pointer" />
<TextView
android:id="#+id/degree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ImageView
android:id="#+id/pf_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/compass"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:src="#drawable/pf_button"
android:onClick="gotoPathFinder" />
</RelativeLayout>
you dont have the method gotoPathFinder in your pathfinderActivity class, am I right that you have also a view with an onclick event in this class too that calls this method?
you do this in your second class:
setContentView(R.layout.activity_main);
so your second class has same layout like your first class. first time you click the button the acticity changes but layout stays the same then when you click it again there is no method in the current class that the botton onclick can run and switch to the other class
so if you have 2 separate layouts you want to use then switch the lone above with
setContentView(R.layout.other_acticity);
and if its intended then add method with same name to second class
This is your error:
Could not find a method gotoPathFinder(View)
android.widget.ImageView with id 'pf_button'
In your XML, find the ImageView with the id "pf_button".
For that ImageView, get rid of whatever android:onClick is already there, and change it to this:
android:onClick="gotoSecond"
You just had the wrong method name in the XML file...your app was looking for that name, but couldn't find it!
Let me know if that works. It should.

GoogleMap V2 error android.view.InflateException: Binary XML file line #7: Error inflating class fragment

Can anybody explain why this error occurs? And what I can do to fix this problem?
this is the first time i get problem like this. thank you for the help.
Stacktrace:
11-27 16:35:07.250: D/AndroidRuntime(18552): Shutting down VM
11-27 16:35:07.250: W/dalvikvm(18552): threadid=1: thread exiting with uncaught exception (group=0x4144b930)
11-27 16:35:07.255: E/AndroidRuntime(18552): FATAL EXCEPTION: main
11-27 16:35:07.255: E/AndroidRuntime(18552): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.agusharyanto.petalokasi/net.agusharyanto.petalokasi.DisplayCurrentLocActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.ActivityThread.access$700(ActivityThread.java:154)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.os.Looper.loop(Looper.java:137)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.ActivityThread.main(ActivityThread.java:5306)
11-27 16:35:07.255: E/AndroidRuntime(18552): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 16:35:07.255: E/AndroidRuntime(18552): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-27 16:35:07.255: E/AndroidRuntime(18552): at dalvik.system.NativeStart.main(Native Method)
11-27 16:35:07.255: E/AndroidRuntime(18552): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.Activity.setContentView(Activity.java:1928)
11-27 16:35:07.255: E/AndroidRuntime(18552): at net.agusharyanto.petalokasi.DisplayCurrentLocActivity.onCreate(DisplayCurrentLocActivity.java:41)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.Activity.performCreate(Activity.java:5255)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
11-27 16:35:07.255: E/AndroidRuntime(18552): ... 11 more
11-27 16:35:07.255: E/AndroidRuntime(18552): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
11-27 16:35:07.255: E/AndroidRuntime(18552): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
11-27 16:35:07.255: E/AndroidRuntime(18552): at maps.af.cf.a(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at maps.af.ay.a(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at maps.af.al.a(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at maps.af.be.a(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at maps.af.bd.a(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at cmj.onTransact(SourceFile:107)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.os.Binder.transact(Binder.java:310)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.google.android.gms.dynamic.a$4.b(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.google.android.gms.dynamic.a.a(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
11-27 16:35:07.255: E/AndroidRuntime(18552): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
11-27 16:35:07.255: E/AndroidRuntime(18552): ... 21 more
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.agusharyanto.petalokasi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="net.agusharyanto.petalokasi.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="net.agusharyanto.petalokasi.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="net.agusharyanto.petalokasi.DisplayCurrentLocActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAkJYisD9-6HB8D1ggkkWKZVtgNCLg8Fyk" />
</application>
</manifest>
DisplayCurrentLocActivity.java
public class DisplayCurrentLocActivity extends FragmentActivity {
GoogleMap googlemap;
MarkerOptions markerOptions;
LatLng latLng;
LatLng prevLatLng = null;
Marker marker = null;
ArrayList<LatLng> arrline = new ArrayList<LatLng>();
private LocationManager locManager;
private LocationListener locListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_loc);
FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
.findFragmentById(R.id.mapcurrloc);
// Getting a reference to the map
googlemap = supportMapFragment.getMap();
initLocationManager();
}
/**
* Initialize the location manager.
*/
private void initLocationManager() {
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locListener = new LocationListener() {
// method ini akan dijalankan apabila koordinat GPS berubah
public void onLocationChanged(Location newLocation) {
displayCurrentLoctoMap(newLocation);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
Location location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
displayCurrentLoctoMap(location);
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locListener);
Location location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
displayCurrentLoctoMap(location);
}
/**
* This method will be called when current position changed is submitted via
* the GPS.
*
* #param newLocation
*/
protected void displayCurrentLoctoMap(Location newLocation) {
try {
LatLng currlok = new LatLng(newLocation.getLatitude(),
newLocation.getLongitude());
if (marker != null)
marker.remove();
markerOptions = new MarkerOptions().position(currlok)
.title("Current Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(currlok, 15));
// call and execute ReversGeocoding Task
new ReverseGeocodingTask(getBaseContext()).execute(currlok);
} catch (NullPointerException e) {
Toast.makeText(DisplayCurrentLocActivity.this,"Can't get gps location, make sure your gps is enable",Toast.LENGTH_LONG).show();
}
}
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
Context mContext;
public ReverseGeocodingTask(Context context){
super();
mContext = context;
}
// Finding address using reverse geocoding
#Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
String addressText="";
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}
return addressText;
}
#Override
protected void onPostExecute(String addressText) {
// Setting the snippet for the marker.
// This will be displayed on taping the marker
Log.d("TAG","Alamat:"+addressText);
markerOptions.snippet(addressText);
marker = googlemap.addMarker(markerOptions);
}
}
}
Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
Add this is manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Why do I get these errors on my logcat whenever I run my Android app in eclipse?

I'm trying to test if my android app works, it consists of 2 activity screens. There are no errors in my codes but my app won't run. It always gives me this error on the emulator "Unfortunately 'application name' has stopped."
Here is my Activity code
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.RED);
Button next=(Button)findViewById(R.id.DGButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent=new Intent(view.getContext(),
Activity2.class);
startActivityForResult(myIntent, 0);
}});}
public void calculateClickHandler(View view)
{
if (view.getId() == R.id.CalculateButton)
{
EditText ageText = (EditText)findViewById
(R.id.AgeField);
EditText weightText = (EditText)findViewById
(R.id.WeightField);
EditText ftText = (EditText)findViewById
(R.id.HeightField);
EditText inText = (EditText)findViewById
(R.id.HeightField2);
RadioGroup weightRG = (RadioGroup) findViewById
(R.id.WeightRG);
RadioGroup sexRG = (RadioGroup) findViewById
(R.id.SexRG);
TextView resultText = (TextView)findViewById
(R.id.ResultLabel);
TextView normalBMIText = (TextView)findViewById
(R.id.NormalBMI);
TextView idealKgText = (TextView)findViewById
(R.id.IdealKgLabel);
TextView idealLbText = (TextView)findViewById
(R.id.IdealLbLabel);
int age = Integer.parseInt(ageText.getText
().toString());
double weight = Double.parseDouble
(weightText.getText().toString());
double ftheight = Double.parseDouble(ftText.getText
().toString());
double inheight = Double.parseDouble(inText.getText
().toString());
int checkedRadioButton1 =
weightRG.getCheckedRadioButtonId();
int checkedRadioButton2 =
sexRG.getCheckedRadioButtonId();
double bmiValue = calculateBMI(weight, ftheight,
inheight, checkedRadioButton1);
String bmiInterpretation1 = interpretBMI1(bmiValue);
String bmiInterpretation2 = interpretBMI2(age);
String bmiInterpretation3 = interpretBMI3(ftheight,
inheight, checkedRadioButton2);
String bmiInterpretation4 = interpretBMI4(ftheight,
inheight, checkedRadioButton2);
resultText.setText(bmiValue + " - " +
bmiInterpretation1);
normalBMIText.setText(""+bmiInterpretation2);
idealKgText.setText(""+bmiInterpretation3);
idealLbText.setText(""+bmiInterpretation4);
Intent intent1 = new Intent(MainActivity.this,
Activity2.class);
Bundle b = new Bundle();
b.putDouble("key", bmiValue);
intent1.putExtras(b);
startActivity(intent1);
}}
This is the error logcat:
12-26 02:50:45.606: E/AndroidRuntime(1776): FATAL EXCEPTION: main
12-26 02:50:45.606: E/AndroidRuntime(1776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.os.Looper.loop(Looper.java:137)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 02:50:45.606: E/AndroidRuntime(1776): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:50:45.606: E/AndroidRuntime(1776): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 02:50:45.606: E/AndroidRuntime(1776): at dalvik.system.NativeStart.main(Native Method)
12-26 02:50:45.606: E/AndroidRuntime(1776): Caused by: java.lang.NullPointerException
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.Activity.performCreate(Activity.java:5104)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 02:50:45.606: E/AndroidRuntime(1776): ... 11 more
This is the debug logcat:
12-26 03:04:06.238: I/Process(1854): Sending signal. PID: 1854 SIG: 9
12-26 03:04:12.779: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
12-26 03:04:12.837: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
12-26 03:04:14.866: D/dalvikvm(1876): GC_CONCURRENT freed 76K, 7% free 2723K/2916K, paused 32ms+32ms, total 270ms
12-26 03:04:15.427: D/AndroidRuntime(1876): Shutting down VM
12-26 03:04:15.456: W/dalvikvm(1876): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-26 03:04:15.546: E/AndroidRuntime(1876): FATAL EXCEPTION: main
12-26 03:04:15.546: E/AndroidRuntime(1876): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.os.Looper.loop(Looper.java:137)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 03:04:15.546: E/AndroidRuntime(1876): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 03:04:15.546: E/AndroidRuntime(1876): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 03:04:15.546: E/AndroidRuntime(1876): at dalvik.system.NativeStart.main(Native Method)
12-26 03:04:15.546: E/AndroidRuntime(1876): Caused by: java.lang.NullPointerException
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.Activity.performCreate(Activity.java:5104)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 03:04:15.546: E/AndroidRuntime(1876): ... 11 more
12-26 03:04:22.547: I/Process(1876): Sending signal. PID: 1876 SIG: 9
Any help is appreciated
02:50:45.606: E/AndroidRuntime(1776):
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at
com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25) 12-26
There is no code in your question so hard to tell how to fix, but based on stack trace, line 25 in MainActivity.java throwing NullPointerException. Code at line25 is some how resulting as null and you are trying to call some action on null reference which results in NullPointerException.
This is caused by NullPointerException. You might be accesing the null object which hasn't been initialized yet. You edit your question with your code, so that there would be chances of having my answer edited.
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
This is where your error lies, line 25 of your MainActivity class. Whatever you are referencing is null.
Change your MainActivity onCreate code as:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View title = getWindow().findViewById(android.R.id.title);
if (title != null) {
ViewParent titleBar = title.getParent();
if (titleBar != null && (titleBar instanceof View)) {
View parentView = (View)titleBar;
parentView.setBackgroundColor(Color.RED);
}
}
// Your Code here...

Categories