这是这篇文章的后续内容:
findViewById在子类SurfaceView中抛出RuntimeException
基于Romain Guy的反馈(我很快会接受它作为解决方案),我想从View中获取调用Activity,以便我可以使用它来获取所需的TextView资源.
我没有在View中看到任何返回Activity的方法.这样做的正确方法是什么?或者在另一个View上下文中使用TextViews是否有更好的选择.
基本上,我在onCreate()中调用setContentView(R.layout.xxx)(像往常一样),所以我真的没有办法传入对其他TextView的引用,除非我在setContentView之后不方便地检索View然后进行调用在上面.
我有一个数据库,我创建了一个Database类private static class DbHelper extends SQLiteOpenHelper来帮助我管理我的数据库.
该数据库可以从四种不同的活动中访问.
我有这个public void onCreate(SQLiteDatabase db),它是创建我的数据库表并为这些表添加值的原因.由于程序仅在用户第一次运行应用程序时进入此处,因此我想创建一个ProgressDialog.
这就是我所拥有的:
Override
public void onCreate(SQLiteDatabase db) {
ProgressDialog progresso = ProgressDialog.show(context, "Info", "Loading DB" );
// Code to create the tables and add the values
progress.dismiss();
}
Run Code Online (Sandbox Code Playgroud)
有两个问题.
第一:
由于这可以从4个不同的活动中访问,我如何获得上下文?制造:
Context context = getAplicationContext();
Run Code Online (Sandbox Code Playgroud)
第二:
为什么我不能使用strings.xml中的字符串?我做不到这个:
ProgressDialog progresso = ProgressDialog.show(context, getString(R.string.driProgressTitle), getString(R.string.driProgressMessage) );
Run Code Online (Sandbox Code Playgroud)
UPDATE
public class Database {
ProgressDialog progresso;
private DbHelper DBHelper;
private final Context Context;
private SQLiteDatabase BaseDados;
private …Run Code Online (Sandbox Code Playgroud) 我在静态方法中调用非静态方法时遇到了一个大问题.
这是我的代码
Class SMS
{
public static void First_function()
{
SMS sms = new SMS();
sms.Second_function();
}
public void Second_function()
{
Toast.makeText(getApplicationContext(),"Hello",1).show(); // This i anable to display and cause crash
CallingCustomBaseAdapters(); //this was the adapter class and i anable to call this also
}
Run Code Online (Sandbox Code Playgroud)
我能够调用Second_function但无法获取Toast和CallCustomBaseAdapter()方法,发生崩溃.
我该怎么做才能解决这个问题?
如何将应用程序上下文从Singleton类传递给SharedPreferences?我在其onActivityCreated(Bundle savedInstanceState)方法中有一个片段和一个GridView ,在项目单击时,我在logcat中得到NullPointerException:
03-30 05:12:54.784: E/AndroidRuntime(1950): FATAL EXCEPTION: main
03-30 05:12:54.784: E/AndroidRuntime(1950): java.lang.NullPointerException
03-30 05:12:54.784: E/AndroidRuntime(1950): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
03-30 05:12:54.784: E/AndroidRuntime(1950): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
03-30 05:12:54.784: E/AndroidRuntime(1950): at com.example.duaapp.utility.SharedPreferencesSupplication.save(SharedPreferencesSupplication.java:35)
Run Code Online (Sandbox Code Playgroud)
单身人士班
public class SingletonClass {
public static Context applicationContext;
public static int fontSizeMin = 17;
public static int fontSizeMax = 35;
public static final String keySelVerseFromList = "keySelVerseFromList";
public static final String keyFavVerses = "keyFavVerses";
public static final String keyListOfVerses = "keyListOfVerses";
public static final String keyIsFavSelected = "keyIsFavSelected";
} …Run Code Online (Sandbox Code Playgroud) singleton android nullpointerexception android-context sharedpreferences
背景是:
DexClassLoader.要获取DexClassLoader实例,我需要第三方应用程序的Context实例.我搜索了stackoverflow,发现有人说你可以在Activity或Receiver中挂钩方法来检索它们的上下文.但我检查了Activity.class,发现没有返回Context类型值的方法,只有一个方法有Context类型参数,onCreateView(String name, Context context, AttributeSet attrs).
有没有办法获得上下文?
我真的很纳闷.我有一个上下文:
Context context= getActivity();
Run Code Online (Sandbox Code Playgroud)
当我context在片段中使用UI时,像webview app这样的东西给了我NullPointerException(Forceclose),但是当我使用getActivity()它时效果很好.有什么区别!?让我解释一下用法.我有一个名为"A"和"B"的活动.活动"B"从活动"B"继承NavigationDrawer和Actionbar.所以有:
public class B extends A
Run Code Online (Sandbox Code Playgroud)
我们在NavigationDrawer中知道有一个主要内容.活动"B"使用片段来提供主要内容,我在该片段中使用Context.我真的很想知道!抱歉英语不好.
编辑:这是我的代码:
public class PlaceholderFragment extends Fragment {
public Context context = getActivity();
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_text, container, false);
String text = "<html><head><link href=\"file:///android_asset/style_css.css\" rel=\"stylesheet\" type=\"text/css\"></head> <body class=\"body\"> title1 <hr> <div align=\"center\"> <img src= "+imagePath1_1+" width= \"95% \" /></div>les1</body></html>";
WebView webView= new WebView(context);
webView.loadDataWithBaseURL(null,text, …Run Code Online (Sandbox Code Playgroud) 我有一个Activity,它本身就有三个Fragment.
在其中一个片段中,有一个RecyclerView带有自定义适配器,并且单击其中一个项目将转到另一个页面,这是相同的新实例Activity.但是,某种行为会导致我的应用程序出错.
在我的Activity中,单击其中一个项会显示同一个Activity的新实例,这很好.然后我按下后退按钮,我被带回第一个活动.但是再次单击其中一个项目(以启动相同Activity的新实例)会导致以下错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.content.Context.getPackageName()'
同样重要的是要考虑我在我的Activity中的一个片段中调用Activity的新实例(即三个项目的位置).所以,当我打电话给它时,我有类似的东西:
public class MyActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
ViewPager viewPager = (ViewPager) findViewById(R.id.detail_viewpager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
TabLayout tabLayout = (TabLayout) findViewById(R.id.detail_tabs);
tabLayout.setTabTextColors(
ContextCompat.getColor(this, R.color.text_white_secondary),
ContextCompat.getColor(this, R.color.text_white));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.white));
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
...
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch …Run Code Online (Sandbox Code Playgroud) android nullpointerexception android-context android-fragments
我正在使用Dagger 2 DataBindng和新的Android Lifecycle组件,这些组件具有ViewModels。
在我的内部,ViewModel如何访问我的strings.xml?我在想,在第一,注入Context到viewModel,然而,这只会泄漏内存。
还有其他方法吗?
android android-context dagger-2 android-viewmodel android-architecture-components
A)为什么当我尝试运行应用程序时出现此错误?
我的崩溃日志错误
java.lang.RuntimeException:无法启动活动java.lang.IllegalStateException:未注册任何检测!必须在注册仪器下运行.在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)在android.app.ActivityThread.-wrap14(ActivityThread.java)在android.app.ActivityThread $ 6.handleMessage(ActivityThread.java:1650)位于android.app.A.Toread.main的android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:154) ActivityThread.java:6688)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)在com.android.internal.os. ZygoteInit.main(ZygoteInit.java:1358)引起:java.lang.IllegalStateException:没有注册的工具!必须在注册仪器下运行.在android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)在android.support.test.InstrumentationRegistry.getContext(InstrumentationRegistry.java:75)在com.wachipi.challengetv.GuidaTvActivity $ GetData.onPreExecute(main.java :76)在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:613)在android.os.AsyncTask.execute(AsyncTask.java:560)在com.wachipi.challengetv.GuidaTvActivity.onCreate(main.java:68)在android.app.Activity.performCreate(Activity.java:6912)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)在android.app. ActivityThread.handleLaunchActivity(ActivityThread.java:3008)在android.app.ActivityThread.-wrap14(ActivityThread.java)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1650)在android.os.Handler.dispatchMessage( Handler.java:102)在android.app.ActivityThrea的android.os.Looper.loop(Looper.java:154)d.main(ActivityThread.java:6688)位于com.android的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)的java.lang.reflect.Method.invoke(Native Method). internal.os.ZygoteInit.main(ZygoteInit.java:1358)
无法启动活动ComponentInfojava.lang.IllegalStateException:未注册任何工具!必须在注册仪器下运行.
是什么造成的?我还在学习,如果你能解释得更多,我会很感激.
这是我的代码
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.support.design.widget.TabLayout;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static android.support.test.InstrumentationRegistry.getContext;
import static com.google.android.gms.internal.zzahn.runOnUiThread;
/**
* Created by GB on 28/03/18.
*/
public class Main extends AppCompatActivity {
private …Run Code Online (Sandbox Code Playgroud) 我想在片段中创建一个recyclerView,但它显示错误"java.lang.IllegalStateException:recylerView_Main在com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)中不能为null"
但是相同的代码在Fragment中不起作用,它显示错误,所以我将"recylerView_Main.layoutManager = LinearLayoutManager(this)"更改为"recylerView_Main.layoutManager = LinearLayoutManager(context)"它显示没有错误,我可以在simlulator中运行,但是我单击片段的导航按钮,应用程序停止并显示此错误.请帮助解决它.

这里有FragmentMe.kt的代码:
class FragmentMe : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
recylerView_Main.layoutManager = LinearLayoutManager(context)
recylerView_Main.adapter = Mainadapter()
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_me, container, false)
}
}
Run Code Online (Sandbox Code Playgroud)
这里使用MainActivity.kt的代码:
class MainActivity : AppCompatActivity() {
val manager = supportFragmentManager
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
//message.setText(R.string.title_home)
createFragmentQpon()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
//message.setText(R.string.title_dashboard)
createFragmentMe()
return@OnNavigationItemSelectedListener true
} …Run Code Online (Sandbox Code Playgroud) android fragment android-context kotlin android-recyclerview