Im working on this project. When i run the program it have some runtime errors and app does not appears in emulator. it show the message app stopped. I think there is a error in OnAttach method. I tried many ways to solve the issue but nothing works.I'm new to Android development please help me to solve the issue.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.workout, PID: 26059
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.workout/com.example.user.workout.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5643)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.user.workout.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:6100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5643)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassCastException: com.example.user.workout.MainActivity cannot be cast to com.example.user.workout.WorkoutListFragment$WorkoutListListener
at com.example.user.workout.WorkoutListFragment.onAttach(WorkoutListFragment.java:53)
at android.support.v4.app.Fragment.onAttach(Fragment.java:1340)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1372)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1659)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1905)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3715)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.user.workout.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:6100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5643)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
package com.example.user.workout;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class WorkoutListFragment extends ListFragment {
static interface WorkoutListListener{
void itemClicked(long id);
};
private WorkoutListListener listener;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String [] names = new String[Workout.workouts.length];
for(int i =0; i < names.length;i++)
{
names[i] = Workout.workouts[i].getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
inflater.getContext(), android.R.layout.simple_list_item_1,names);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
this.listener = (WorkoutListListener)activity;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
if(listener!=null)
{
listener.itemClicked(id);
}
}
}
package com.example.user.workout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void itemClicked(long id)
{
WorkoutDetialFragment detials = new WorkoutDetialFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
detials.setWorkoutId(id);
ft.replace(R.id.fragment_container,detials);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
Your activity should implement *WorkoutListListener* interface and override its method itemClicked().
public class MainActivity extends AppCompatActivity implements WorkoutListListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void itemClicked(long id){
// handle your click here
}
}
Related
I am learning how to develop for Android (and development in general). I am trying to create a Quiz App, so I can learn the basics.
While trying to use an Intent to go to another class with an extra variable on it, I found this problem (which I think is pretty usual): My app crashes when I go to the next Activity.
This is the code in the activity that gets the username ("usuario and nomeUsuario"):
package com.isa56.quiz2;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class HomeActivity extends AppCompatActivity {
public EditText usuario;
public EditText senha;
public Button botaoLogin;
public String nomeUsuario;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
botaoLogin = findViewById(R.id.botaoLogin);
senha = findViewById(R.id.senha);
usuario = findViewById(R.id.usuario);
nomeUsuario = usuario.getText().toString();
Log.d("nome de usuario", nomeUsuario);
botaoLogin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(HomeActivity.this, FirstQuestionActivity.class);
i.putExtra("nome", nomeUsuario);
startActivity(i);
}});
}
}
And this is the code on the SecondQuestionActivity:
package com.isa56.quiz2;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class FirstQuestionActivity extends AppCompatActivity {
public Button prox1;
public Button respUm1;
public Button respUm2;
public TextView texto1;
public int pontuacao = 0;
Intent i = getIntent();
String nome = i.getStringExtra("nome");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstquestion);
prox1 = findViewById(R.id.prox1);
respUm1 = findViewById(R.id.botao1a);
respUm2 = findViewById(R.id.botao1b);
texto1 = findViewById(R.id.texto1);
respUm1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
texto1.setText("Resposta errada!");
respUm1.setEnabled(false);
respUm2.setEnabled(false);
prox1.setEnabled(true);
}});
respUm2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
texto1.setText("Resposta certa!");
respUm2.setEnabled(false);
respUm1.setEnabled(false);
pontuacao += 1;
prox1.setEnabled(true);
}});
prox1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(FirstQuestionActivity.this, SecondQuestionActivity.class);
i.putExtra("pontos", pontuacao);
i.putExtra("nome", nome);
startActivity(i);
}
});
}
}
What is wrong with it? How can I prevent it?
Thank you!
P.S.: I am not used to forum or development in general, so I might have done this wrongly. I'm sorry.
Edit: Also, if anyone wanna take a look at the whole project, it's here.
Edit2: This is the Logcat output:
2020-11-13 21:05:18.531 7517-7517/com.isa56.quiz2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.isa56.quiz2, PID: 7517
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.isa56.quiz2/com.isa56.quiz2.FirstQuestionActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.isa56.quiz2.FirstQuestionActivity.<init>(FirstQuestionActivity.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Keep these lines inside onCreate() method.
Intent i = getIntent();
String nome = i.getStringExtra("nome");
I know this question has been asked multiple times, but none of those solved my problem. The stacktrace looks like this.
02-09 19:39:20.364 18846-18846/com.michael1011.tweetcomposer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.michael1011.tweetcomposer, PID: 18846
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michael1011.tweetcomposer/com.michael1011.tweetcomposer.login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.support.design.widget.Snackbar.setText(Snackbar.java:334)
at android.support.design.widget.Snackbar.make(Snackbar.java:210)
at com.michael1011.tweetcomposer.login.onCreate(login.java:43)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
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)
And the adapter class looks similar to this
package com.example.babu.moviemanager;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
Context context;
List<Movie> movieList;
public CustomAdapter(Context context, List<Movie> movieList) {
this.context = context;
this.movieList = movieList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.items,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Movie movie=movieList.get(position);
holder.title.setText(movie.getTitle());
holder.overview.setText(movie.getOverview());
Picasso.with(getcontext()).load(movie.getPosterPath()).into(holder.poster);
}
#Override
public int getItemCount() {
return movieList.size();
}
public Context getcontext() {
return context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.imageContainer)
ImageView poster;
#BindView(R.id.titleContainer)
TextView title;
#BindView(R.id.overviewContainer)
TextView overview;
#BindView(R.id.card)
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(itemView);
}
}
}
According to the butterknife documentation, the bind method that you used in the viewholder must accept either view target bind(this) or view target and view source bind(this,view). In your case change the bind method to bind(this,itemView), it should work.
Changing the ButterKnife dependencies to the latest version solved it for me.
dependencies {
...
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
}
Summary: MainActivityFragment passes passes an intent to DetailedActivity when an item is clicked to display detailed data. Since I want to display these within a Two-Pane interface, I needed to make the DetailedActivity a fragment.
Problem : Whenever clicking on the item in MainActivityFragment to launch the intent for DetailedActivityFragment, I get an error saying its unable to start or inflate the activity. The detailedActivity for sake here is named "test" and "testFragment". It appears as though it points to line 25 of the activity_test.xml which is the line of include layout="#...".
I have left the fragment_test.xml out due to its length but i will provide a github link to anyone whom may want to see it or the manifest.
Im currently a novice to the android world so any help is appriciated =)! This branch of my project is a huge mess right now experimenting with things right now so pardon the mess...
Github Link:https://github.com/Rykuno/Flix-Viewer/tree/tablet/app/src/main/java/com/rykuno/movietestapp/Activities
FATAL EXCEPTION: main
Process: com.rykuno.movietestapp, PID: 14281
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rykuno.movietestapp/com.rykuno.movietestapp.Activities.test}: android.view.InflateException: Binary XML file line #25: Binary XML file line #1: Error inflating class fragment
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: android.view.InflateException: Binary XML file line #25: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
at android.app.Activity.setContentView(Activity.java:2172)
at com.rykuno.movietestapp.Activities.test.onCreate(test.java:14)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
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: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:941)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
at android.app.Activity.setContentView(Activity.java:2172)
at com.rykuno.movietestapp.Activities.test.onCreate(test.java:14)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
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: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
at com.rykuno.movietestapp.Activities.testFragment.setViewData(testFragment.java:100)
at com.rykuno.movietestapp.Activities.testFragment.onCreateView(testFragment.java:87)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:941)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
at android.app.Activity.setContentView(Activity.java:2172)
at com.rykuno.movietestapp.Activities.test.onCreate(test.java:14)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
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)
Here is the code to the Activity Files vvv
package com.rykuno.movietestapp.Activities;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.rykuno.movietestapp.R;
public class test extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
And the fragment
package com.rykuno.movietestapp.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.rykuno.movietestapp.Adapter.TrailerAdapter;
import com.rykuno.movietestapp.BuildConfig;
import com.rykuno.movietestapp.Data.DatabaseHelper;
import com.rykuno.movietestapp.R;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* A placeholder fragment containing a simple view.
*/
public class testFragment extends Fragment {
private static final String MOVIEKEYS = "videoKeys";
private String movieVideoUrl;
private List<String> movieKeys = new ArrayList<>();
private String mPoster;
private String mOriginalTitle;
private String mPlot;
private String mReleaseDate;
private String mBackDrop;
private String mRating;
private boolean mFavorited = false;
private int mId;
private DatabaseHelper db;
private TrailerAdapter mTrailerAdapter;
#Bind(R.id.title_textView)
TextView mTitleTextView;
#Bind(R.id.rating_textView)
TextView mRatingTextView;
#Bind(R.id.plot_textView)
TextView mPlotTextView;
#Bind(R.id.backdrop_image)
ImageView mBackdropImageView;
#Bind(R.id.poster_imageView)
ImageView mPosterImageView;
#Bind(R.id.released_textView)
TextView mReleaseDateTextView;
#Bind(R.id.comments_button)
Button mCommentsButton;
#Bind(R.id.favorite_imageView)
ImageView mFavoritesImageView;
#Bind(R.id.trailer_gridview)
GridView mGridView;
public testFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);
db = new DatabaseHelper(getActivity());
ButterKnife.bind(getActivity());
extractExtras();
setViewData();
getMovieYoutubeHandle();
setClickItems();
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void setViewData() {
Picasso.with(getActivity()).load("http://image.tmdb.org/t/p/w780/" + mBackDrop).into(mBackdropImageView);
Picasso.with(getActivity()).load("http://image.tmdb.org/t/p/w342/" + mPoster).into(mPosterImageView);
mTitleTextView.setText(mOriginalTitle);
mRatingTextView.setText(mRating + "/10");
mPlotTextView.setText(mPlot);
mReleaseDateTextView.setText(mReleaseDate);
}
private void extractExtras() {
mPoster = getActivity().getIntent().getStringExtra("poster");
mOriginalTitle = getActivity().getIntent().getStringExtra("title");
mPlot = getActivity().getIntent().getStringExtra("plot");
mReleaseDate = getActivity().getIntent().getStringExtra("releaseDate");
mBackDrop = getActivity().getIntent().getStringExtra("backdrop");
mRating = getActivity().getIntent().getStringExtra("rating");
mId = getActivity().getIntent().getIntExtra("id", 0);
mFavorited = getActivity().getIntent().getBooleanExtra("favorited", false);
}
private void getMovieYoutubeHandle(){
movieVideoUrl = "http://api.themoviedb.org/3/movie/"+mId+"/videos?api_key="+ BuildConfig.MY_MOVIE_DB_API_KEY;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(movieVideoUrl).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
}
#Override
public void onResponse(Call call, Response response) throws IOException {
String jsonData = response.body().string();
try {
movieKeys = Arrays.asList(getTrailerData(jsonData));
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
mTrailerAdapter.setListData(movieKeys);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
private String[] getTrailerData(String jsonData) throws JSONException {
JSONObject trailerDetails = new JSONObject(jsonData);
JSONArray results = trailerDetails.getJSONArray("results");
String[] keyArray = new String[results.length()];
for(int i = 0; i<results.length(); i++){
JSONObject keyResults = results.getJSONObject(i);
String youtubeKey = keyResults.getString("key");
keyArray[i] = youtubeKey;
}
return keyArray;
}
private void setClickItems() {
//initializes gridview for trailers and onItemClicks
mTrailerAdapter = new TrailerAdapter(getActivity(), R.layout.trailer_item, movieKeys);
mGridView.setAdapter(mTrailerAdapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String keyPosition = movieKeys.get(position).toString();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=" + keyPosition)));
}
});
//onClick launches the CommentsActivity
mCommentsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CommentsActivity.class);
intent.putExtra("id", mId);
startActivity(intent);
}
});
//onClick for favorites
mFavoritesImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db = new DatabaseHelper(getActivity());
boolean isInserted = db.insertData(getActivity(), mId, mOriginalTitle, mPlot, mBackDrop, mPoster, mRating, mReleaseDate);
if (isInserted = true) {
mFavoritesImageView.setImageResource(android.R.drawable.star_big_on);
Log.v(DetailedMovie.class.getSimpleName(), "entry worked");
} else if (isInserted = false) {
Log.v(DetailedMovie.class.getSimpleName(), "entry failed");
}
}
});
}
#Override
public void onSaveInstanceState(Bundle outState) {
ArrayList<String> toSave = movieKeys instanceof ArrayList ?
(ArrayList<String>) movieKeys : new ArrayList<>(movieKeys);
outState.putStringArrayList(MOVIEKEYS, toSave);
super.onSaveInstanceState(outState);
}
}
As well as the layout files.
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.rykuno.movietestapp.Activities.test">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_test"/>
</android.support.design.widget.CoordinatorLayout>
content_test.xml
<fragment android:id="#+id/fragment1"
android:name="com.rykuno.movietestapp.Activities.testFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_test"/>
make sure to use:
import android.support.v4.app.Fragment;
And also make sure that the Activity that is using the fragment(s) extends FragmentActivity instead of the regular Activity,
import android.support.v4.app.FragmentActivity;
to get the FragmentActivity class.
One think I found is add app:layout_behaviour to include tag and not inside content_test.xml
<include layout="#layout/content_test"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
And your project got many NPE unable to test more.
I have 2 classes. I am calling an intent from my menu class, and when I call the intent to go to my MainActivity class, it gives me an error that says, Unfortunately, app name has stopped. I need help figuring out what is happening. I know it is because there is an error somewhere in MainActivity. I just need help finding it.
Here is MainActivity.java's code:
package com.arborhillsvet.arborhillsveterinarianclinic;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ViewAnimator;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.SaveCallback;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "parse information", "parse information");
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
});
WebView appointWeb = (WebView) findViewById(R.id.appointments);
appointWeb.loadUrl("https://www.google.com");
WebSettings webSettings = appointWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
appointWeb.setWebViewClient(new WebViewClient());
WebView promosWeb = (WebView) findViewById(R.id.promos);
promosWeb.loadUrl("https://www.yahoo.com");
WebSettings webSettings2 = promosWeb.getSettings();
webSettings2.setJavaScriptEnabled(true);
promosWeb.setWebViewClient(new WebViewClient());
WebView infoWeb = (WebView) findViewById(R.id.info);
infoWeb.loadUrl("https://www.bing.com");
WebSettings webSettings3 = infoWeb.getSettings();
webSettings3.setJavaScriptEnabled(true);
infoWeb.setWebViewClient(new WebViewClient());
WebView medsWeb = (WebView) findViewById(R.id.medicines);
medsWeb.loadUrl("https://www.aol.com");
WebSettings webSettings4 = medsWeb.getSettings();
webSettings4.setJavaScriptEnabled(true);
medsWeb.setWebViewClient(new WebViewClient());
Button next = (Button) findViewById(R.id.next);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewAnimator anim = (ViewAnimator) findViewById(R.id.views);
anim.showNext();
}
};
next.setOnClickListener(listener);
Button prev = (Button) findViewById(R.id.prev);
View.OnClickListener listener2 = new View.OnClickListener() {
public void onClick(View view) {
ViewAnimator anim = (ViewAnimator) findViewById(R.id.views);
anim.showPrevious();
}
};
prev.setOnClickListener(listener2);
ImageButton menu = (ImageButton) findViewById(R.id.menu);
View.OnClickListener listener3 = new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, menu.class);
startActivity(intent);
}
};
menu.setOnClickListener(listener3);
}
}
I also have edited this post and added the Logcat after somebody has requested it. I think I see the error and it appears as if it is something wrong with the Parse.initialize and Parse.enableLocalDatastore methods, although I do not see anything wrong with their location. I am wondering if I need to put the thread to sleep for a couple seconds so local datastore can load first. I'm not sure but here is the Logcat.
>08-17 19:39:03.018 4601-4601/com.arborhillsvet.arborhillsveterinarianclinic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.arborhillsvet.arborhillsveterinarianclinic, PID: 4601
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arborhillsvet.arborhillsveterinarianclinic/com.arborhillsvet.arborhillsveterinarianclinic.MainActivity}: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
at com.parse.Parse.enableLocalDatastore(Parse.java:58)
at com.arborhillsvet.arborhillsveterinarianclinic.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I need help! Please and thank you.
I guess you have to initialize in Application class may be problem arise because you initialize in MainActivity
do like this
public class ParseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
ParseCrashReporting.enable(this);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxx", "xxx");
}
}
I'm developing an app that uses Parse to store and retrieve some data. I followed the setup guide on the website but it´s crashing!
These are the files:
MainActivity.java:
package apps.XXXXXX.test;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new Frag())
.commit();
}
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Frag.java
package apps.XXXXXX.test;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag extends Fragment {
private String mParam1;
private String mParam2;
String[] values = new String[5];
FragmentActivity activity;
private OnFragmentInteractionListener mListener;
public static Frag newInstance(String param1, String param2) {
Frag fragment = new Frag();
return fragment;
}
public Frag() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this.getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
values[0] = "laps";
values[1] = "activeEvent";
values[2] = "eventName";
values[3] = "highlights";
values[4] = "image";
ParseHelper parseHelper = new ParseHelper("Event", "objectId", "XXXXXXX", values, activity);
parseHelper.executeString();
return inflater.inflate(R.layout.fragment_, container, false);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
The app is able to retrieve data form Parse. But when I press the Back Button and go back to the app (icon or trough the recent apps) it's giving me this:
02-25 09:22:32.146 3295-3295/apps.xxxx.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: apps.xxxx.test, PID: 3295
java.lang.RuntimeException: Unable to start activity ComponentInfo{apps.xxxx.test/apps.xxxx.test.MainActivity}: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context, String, String)`
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context, String, String)`
at com.parse.Parse.enableLocalDatastore(Parse.java:102)
at apps.xxxx.test.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I found the solution!
Just create a class named Application.java
package your.package.name;
import com.parse.Parse;
import com.parse.ParseUser;
public class Application extends android.app.Application {
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");
}
}
Edit the application section on your AndroidManifest.xml
<application
android:name="your.package.name.Application"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
That's it. That will maintain the Global Application State.