Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Android App codelab: #77

Open
jawadzd opened this issue Nov 15, 2020 · 20 comments
Open

First Android App codelab: #77

jawadzd opened this issue Nov 15, 2020 · 20 comments

Comments

@jawadzd
Copy link

jawadzd commented Nov 15, 2020

I am starting to learn java and use the android studio app to create my own application but after I did all the steps the final step is not working and the app is given me and error "cannot find symbol class navArgs' in the first and the second fragment.java
can you please check the code since I was just copying it and pasting without any changes
Screenshot (176)

@StanleyShen
Copy link

no need to import navArgs, it's not used.

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

Did that and I am still left with 2 errors the one underlined with a zigzag red line in fragment 1 and another in the second fragment
and thank you for your time really appreciate it
Screenshot (177)

@StanleyShen
Copy link

have you added

        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

to build.gradle ( Project) and

apply plugin: "androidx.navigation.safeargs"

to build.gradle ( Module)

image

And compile code, these classes should be generated for you under build/generated/source/

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

Yes i did that in a previous step

@StanleyShen
Copy link

you can try to Clean Project and Rebuild Project, it should be there.

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

Just did that and stuck with the same error i can send you all my code line if it can help you figure out what's wrong

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

//first fragment

package jawadscode.counter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

public class FirstFragment extends Fragment {
TextView showCountTextView;

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState

) {

    // Inflate the layout for this fragment
    View fragmentFirstLayout = inflater.inflate(R.layout.fragment_first, container, false);
    // Get the count text view
    showCountTextView = fragmentFirstLayout.findViewById(R.id.textview_first);
    return fragmentFirstLayout;

}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.random_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int currentCount = Integer.parseInt(showCountTextView.getText().toString());
            FirstFragmentDirections.ActionFirstFragmentToSecondFragment action = FirstFragmentDirections.actionFirstFragmentToSecondFragment();

            action.setMyArg(currentCount);
            NavHostFragment.findNavController(FirstFragment.this).navigate(action);
        }
    });
    view.findViewById(R.id.toast_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast myToast = Toast.makeText(getActivity(), "Hello toast!", Toast.LENGTH_SHORT);
            myToast.show();
        }
    });
    view.findViewById(R.id.count_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countMe(view);
        }
    });
}
private void countMe(View view) {// Get the value of the text view
    String countString = showCountTextView.getText().toString();
    // Convert value to a number and increment it
    Integer count = Integer.parseInt(countString);
    count++;
    // Display the new value in the text view.
    showCountTextView.setText(count.toString());

}

}

@StanleyShen
Copy link

you can puts all your code to one repo, so that I can take a look there.

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

i am new to github so having a bit of a hard time here

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

//second fragment
`package jawadscode.counter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import java.util.Random;

public class SecondFragment extends Fragment {

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState
) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_second, container, false);
}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Integer count = SecondFragmentArgs.fromBundle(getArguments()).getMyArg();
    String countText = getString(R.string.random_heading, count);
    TextView headerView = view.getRootView().findViewById(R.id.textview_header);
    headerView.setText(countText);
    Random random = new java.util.Random();
    Integer randomNumber = 0;
    if (count > 0) {
        randomNumber = random.nextInt(count + 1);
    }
    TextView randomView = view.getRootView().findViewById(R.id.textview_random);
    randomView.setText(randomNumber.toString());
    view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NavHostFragment.findNavController(SecondFragment.this)
                    .navigate(R.id.action_SecondFragment_to_FirstFragment);
        }
    });
}

}`

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

//build .gradle project
`// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
def nav_version = "2.3.0-alpha04"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}`

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

//build.gradle module
`plugins{
id 'com.android.application'
}
apply plugin: 'com.android.application'
apply plugin: 'androidx.navigation.safeargs'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
    applicationId "jawadscode.counter"
    minSdkVersion 14
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}
`

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

sorry for the inconvenient way and thanks for the help

@StanleyShen
Copy link

I tried yours, but I cannot see any issue here, please try to see how to create a repo and put your code there so that I can take a look.
Take some time to learn how to use git, it's not hard. :-)

@jawadzd
Copy link
Author

jawadzd commented Nov 16, 2020

I just created a rep and added the files needed you can check them now

@Lifshatz
Copy link

Lifshatz commented Dec 1, 2020

I am having the same issue. Did you figure out a solution for this? Can you paste the link to your repo? I'm new to github too. Thanks!

@jawadzd
Copy link
Author

jawadzd commented Dec 2, 2020

hey, no still no luck with the problem and I didn't have time to look through it again
https://github.com/jawadzd/firstapp_androidstudio
that's my repo link

@Lifshatz
Copy link

Lifshatz commented Dec 2, 2020

This appears to be the same issue I was having check out my steps for resolution: #80 (comment)

Let me know if that works.

@MohsinIsGood
Copy link

This appears to be the same issue I was having check out my steps for resolution: #80 (comment)

Let me know if that works.
Thanks dude

@akashdapra
Copy link

Override a destination argument in an action

<action android:id="@+id/action_FirstFragment_to_SecondFragment" app:destination="@id/SecondFragment"> <argument android:name="mrAyg" android:defaultValue="-1" app:argType="integer" /> </action>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants