Java 无法将值设置为微调器。“显示”;android.content.res.Resources$NotFoundException:字符串资源ID“0x1”;在应用程序安装期间
尝试创建一个简单的项目,从数据库中获取数据并将其提供给微调器下拉列表。但是得到下面提到的错误(android.content.res.Resources$NotFoundException:String resource ID#0x1)。我正在使用一个数据库助手类来实现同样的功能。有人能帮我一下吗。 提前谢谢 致命异常:主Java 无法将值设置为微调器。“显示”;android.content.res.Resources$NotFoundException:字符串资源ID“0x1”;在应用程序安装期间,java,android,xml,Java,Android,Xml,尝试创建一个简单的项目,从数据库中获取数据并将其提供给微调器下拉列表。但是得到下面提到的错误(android.content.res.Resources$NotFoundException:String resource ID#0x1)。我正在使用一个数据库助手类来实现同样的功能。有人能帮我一下吗。 提前谢谢 致命异常:主 Process: com.ex
Process: com.example.sumeet.sqlitespinner, PID: 5993
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sumeet.sqlitespinner/com.example.sumeet.sqlitespinner.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:305)
at android.content.res.Resources.getString(Resources.java:400)
at android.content.Context.getString(Context.java:389)
at com.example.sumeet.sqlitespinner.MainActivity.loadSpinner(MainActivity.java:65)
at com.example.sumeet.sqlitespinner.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
MainActivity.java
package com.example.sumeet.sqlitespinner;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText et_name;
Spinner spinnerContactName;
Button btn_add;
SQLiteController controller;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
et_name = (EditText) findViewById(R.id.et_name);
spinnerContactName = (Spinner) findViewById(R.id.sp_data);
btn_add = (Button) findViewById(R.id.btn_add);
controller = new SQLiteController(this);
controller.Open();
loadSpinner();
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new MyAsnkTask().execute();
}
});
}
public void loadSpinner() {
ArrayList<String> dataList = new ArrayList<String>();
controller.Open();
Cursor c = controller.readData();
c.moveToFirst();
while (!c.isAfterLast()) {
String name = getString(c.getColumnIndex(DBHelper.Contact_name));
dataList.add(name);
c.moveToNext();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_view, R.id.txt_name, dataList);
spinnerContactName.setAdapter(adapter);
controller.close();
}
private class MyAsnkTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("please Wait");
pd.setMessage("loading");
pd.setCancelable(false);
pd.show();
}
@Override
protected Void doInBackground(Void... voids) {
controller.Open();
String name = et_name.getText().toString();
controller.insertData(name);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
loadSpinner();
pd.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify SQLiteController parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
DBHelper.java
package com.example.sumeet.sqlitespinner;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Sumeet on 20-04-2017.
*/
public class DBHelper extends SQLiteOpenHelper {
public static final String Table_name = "table_contact";
public static final String Contact_id = "id";
public static final String Contact_name = "name";
public static final String dataBase_name = "contacts_db";
public static final int dataBaseVersion = 1;
public static final String Create_table = "CREATE TABLE " + Table_name
+ "("
+ Contact_id + " INTEGER PRIMARY KEY AUTOINCREMENT , "
+ Contact_name + " TEXT NOT NULL "
+ ")";
private static final String Drop_table = "DROP TABLE IF EXISTS" + Table_name;
public DBHelper(Context context) {
super(context, dataBase_name, null, dataBaseVersion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(Create_table);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(Drop_table);
onCreate(sqLiteDatabase);
}
}
spinner_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.sumeet.sqlitespinner.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
context_main.xml
<fragment 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:id="@+id/fragment"
android:name="com.example.sumeet.sqlitespinner.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<Button
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="add" />
<Spinner
android:id="@+id/sp_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>
fragment_main.xml
<fragment 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:id="@+id/fragment"
android:name="com.example.sumeet.sqlitespinner.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<Button
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="add" />
<Spinner
android:id="@+id/sp_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>
您需要在loadSpinner()
–c.getString(…)
中的循环内的Cursor
上调用getString()
。这样就解决了错误,安装了应用程序。但我有点困惑,因为对于我所做的任何新输入,并按下添加按钮,值“1”显示在微调器下拉列表中。嗨,迈克,非常感谢你的回复。但我不明白你到底是什么意思。您能再次指出错误吗?在loadSpinner()
方法中,更改为:String name=c.getString(c.getColumnIndex(DBHelper.Contact_name))代码>。您只是在c.
前面添加了getString()
。非常感谢,:)的效果非常好。