Bianca
Bianca

Reputation: 25

Adding a new project to existing project android application (How to move to next activity)

I've only watched and read tutorials from YouTube and on the internet but I need a specific instruction to this. (Still a student and just self-studying android development.)

I have a found a source code for that I want to make as reference in experimenting applications: https://github.com/vjycool97/Zomato-Search-API (credits to the owner)

I wanted to try it to appear after I tap the "Get Started" button on the welcome screen but the button intent doesn't work for me. The app just crashes.

public class WelcomeScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome_screen);

    Button gsbtn=(Button)findViewById(R.id.getstartedbtn);
    gsbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);
        }
    });

update Logcat

java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getCount()' on a null object reference
    at e.alasse.foodiary.views.activities.MainActivity.onLoadFinished(MainActivity.java:382)
    at e.alasse.foodiary.views.activities.MainActivity.onLoadFinished(MainActivity.java:66)
    at android.support.v4.app.LoaderManagerImpl$LoaderObserver.onChanged(LoaderManagerImpl.java:250)
    at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
    at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
    at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
    at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.setValue(LoaderManagerImpl.java:189)
    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManagerImpl.java:174)
    at android.support.v4.content.Loader.deliverResult(Loader.java:132)
    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:109)
    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:41)
    at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:258)
    at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:83)
    at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:490)
    at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:507)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

EDIT 2:

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="e.alasse.foodiary">
<uses-permission android:name="android.permission.INTERNET" />

<application

    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/icon2"
    android:roundIcon="@mipmap/icon2"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".WelcomeScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider
        android:name="e.alasse.foodiary.providers.RestaurantContentProvider"
        android:authorities="e.alasse.providers.RestaurantContentProvider"
        android:exported="false" />
    <activity android:name=".views.activities.MainActivity"></activity>
    <activity android:name=".ImagesActivity" />
    <activity android:name=".UploadActivity" />
</application>

UPDATE

The get started button moved to the next activity but it crashes after, again.

I have this error

 java.lang.RuntimeException: An error occurred while executing 
doInBackground()

Updated version https://drive.google.com/file/d/1udjQcT02b8sDTDA3udWDbx96X3rpCssJ/view?usp=sharing

Upvotes: 1

Views: 52

Answers (2)

navylover
navylover

Reputation: 13579

In onLoadFinished method of MainActivity class, you could see this:

RestaurantInfoModel model = MyApplication.getGsonInstance().fromJson(cursor.getString(dataIndex), RestaurantInfoModel.class);

Which calls MyApplication's method getGsonInstance(), and to ensure this method executed successfully, you should add this line in your AndroidManifest.xml :

android:name=".MyApplication"

The detailed info about custom application is here

Upvotes: 0

Shayan Tabatabaee
Shayan Tabatabaee

Reputation: 467

First in your manifest you must declare your application class with name attribute. add something like this to your manifest :

<application android:name=".MyApplication"

the getCityId() method in MainActivity class throws nullpointer exception since you did not declare your application class so MyApplication.getContext() is null.

I add the line above and everything works fine.

Upvotes: 1

Related Questions