gpt4 book ai didi

java - AutoCompleteTextView NullPointerException

转载 作者:行者123 更新时间:2023-11-29 22:45:54 25 4
gpt4 key购买 nike

我正在尝试使用 AutoCompleteTextView,但是当我打开应该显示表格中的消费者的 fragment 时,我的应用程序崩溃了。

这是我的数据库助手代码:

public String[] readingconsumerlist() {
try {
String consumer[] = null;
SQLiteDatabase db = this.getReadableDatabase();

String query = "select name from Consumer";
Cursor cursor = db.rawQuery(query, null);

if (cursor != null) {
if (cursor.moveToFirst()) {
consumer = new String[cursor.getCount()];

int i = 0;
do {
consumer[i]=cursor.getString(0);
i++;
} while (cursor.moveToNext());
}
}
cursor.close();
return consumer;
}
catch (Exception e) {
e.getMessage();
return null;
}
}

这是 fragment 中的代码:

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);

db = new DatabaseHelper(getActivity());
final String[] myconsumer = db.readingconsumerlist();

autoCompleteTextViewconsumername = getActivity().findViewById(R.id.consumer_name);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_dropdown_item_1line, myconsumer);
autoCompleteTextViewconsumername.setAdapter(adapter);

return root;
}

我不知道这是不是堆栈跟踪,我是新手。我已经尝试修复此错误近 2 天了,但我所做的一切都不起作用

Process: com.vicjames.finalmeterreader, PID: 11657
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vicjames.finalmeterreader/com.vicjames.finalmeterreader.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3066)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:202)
at android.app.ActivityThread.main(ActivityThread.java:6962)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3741)
at java.util.Arrays.asList(Arrays.java:3728)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:163)
at com.vicjames.finalmeterreader.ui.home.HomeFragment.onCreateView(HomeFragment.java:58)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at android.app.Activity.performStart(Activity.java:7247)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3066) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:202) 
at android.app.ActivityThread.main(ActivityThread.java:6962) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888) 

最佳答案

您应该以这种方式获取 fragment 的小部件:

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false) ;

autoCompleteTextViewconsumername =root.findViewById(R.id.consumer_name);
.
.
.
return root;

关于java - AutoCompleteTextView NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58513737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com