我在我的应用程序中遇到了一个问题,我发现自己以一种笨拙的方式在 Activity 之间共享数据 - 因此我决定尝试将应用程序的大部分内容切换到单个 Activity(以便跨屏幕使用单个 ViewModel),使用导航组件交换片段而不是更改活动。
似乎对于每个目的地,我都需要一个 xml 布局文件和一个片段类,如下所示:
class DestinationFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_destination_layout, container, false)
return view
}
}
Run Code Online (Sandbox Code Playgroud)
(我实际上使用的是 Java,但只能找到 Kotlin 示例。)
但是我不确定如何处理地图屏幕。我目前已将其作为自己的活动:
活动地图.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MapsActivity">
<!-- Toolbar stuff -->
<FrameLayout android:id="@+id/container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/my_toolbar"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
</FrameLayout>
</RelativeLayout> …Run Code Online (Sandbox Code Playgroud) 我是 Android 和 Java 新手,正在尝试制作基于位置的应用程序。
编辑
我制作了一个简单得多的测试代码并得到相同的错误。这是java:
package com.example.viewmodeltest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
public class MyViewModel extends ViewModel {
public int scoreTeamA = 0;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyViewModel locationViewModel = new ViewModelProvider(this).get(MyViewModel.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我犯了同样的错误。这是我的应用程序级 build.gradle 中的依赖项:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//dependencies for ViewModel, LiveData, etc.
def lifecycle_version = "2.2.0"
def …Run Code Online (Sandbox Code Playgroud) java android android-fusedlocation android-livedata android-viewmodel
我正在尝试通过API访问LinkedIn数据(我没有应用程序,我只想访问公司数据-或查看可以访问的内容)。关于此主题,这里还有其他问题,但是大多数问题已过时(使用LinkedIn当前授权流程之前的packagaes)。
我遵循了有关授权的LinkedIn文档:https : //developer.linkedin.com/docs/oauth2
我创建了一个应用程序(因为我没有网站,所以使用了无用的网站网址)。这给了我一个客户ID和客户机密。
我使用了来自LinkedIn(https://github.com/linkedin/api-get-started/blob/master/python/tutorial.py)的(过时的)东西:
import oauth2 as oauth
import urllib.parse as urlparse
consumer_key = 'my client id e.g. sjd6ffdf6262d'
consumer_secret = 'my customer secret e.g. d77373hhfh'
request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken'
access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
resp,content = client.request(request_token_url, "POST")
request_token = dict(urlparse.parse_qsl(content))
clean_request_token = {}
for key in request_token.keys():
clean_request_token[key.decode('ascii')] = request_token[key].decode('ascii')
request_token = clean_request_token
print ("Go to the following link in your browser:")
print ("%s?oauth_token=%s" % (authorize_url, …Run Code Online (Sandbox Code Playgroud) android ×2
java ×2
androidx ×1
linkedin ×1
linkedin-api ×1
oauth-2.0 ×1
python ×1
python-3.x ×1