بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
علت خطا nullpointerexeption کمک فوری
سلام.برنامه یک لیست ویو داره که از بانک تغذیه میشه.الان مشکلی که دارم اینه که همیشه در اولین اجرا که میخام لیست ویو رو برا اولین بار باز کنم با ارور مواجه میشم.و متوجه شدم بانک کپی نمیشه ولی حجمش درسته وقتی داخل گوشی چک میکنم.ولی وقتی بار دوم برناه رو اجرا میکنم مشکل نداره و اطلاعات لود میشه ممنون میشم کمک کنید...سرچ کردم به نتیجه نرسیدم.اینم از کلاس G
publicclass G extendsApplication{
publicstaticArrayList<StructNote> notes =newArrayList<StructNote>();
publicstaticActivity currentactivity;
publicstaticContext contex;
publicstaticLayoutInflater inflater;
publicstaticSQLiteDatabase database;
publicstaticfinalString DIR_SDCARD =Environment.getExternalStorageDirectory().getAbsolutePath();
publicstaticfinalString DIR_DATABASE = DIR_SDCARD +"/database-Test/";
@Override
publicvoid onCreate(){
super.onCreate();
contex =this.getApplicationContext();
inflater =(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newFile(DIR_DATABASE).mkdirs();
database =SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE +"/mydb.sqlite",null);
copyDataBase();
}
privatevoid copyDataBase()
{
Log.i("Database",
"New database is being copied to device!");
byte[] buffer =newbyte[1024];
OutputStream myOutput =null;
int length;
// Open your local db as the input stream
InputStream myInput =null;
try
{
myInput = G.contex.getAssets().open("mydb.sqlite");
// transfer bytes from the inputfile to the
// outputfile
myOutput =newFileOutputStream(DIR_DATABASE +"mydb.sqlite");
while((length = myInput.read(buffer))>0)
{
myOutput.write(buffer,0, length);
}
myOutput.close();
myOutput.flush();
myInput.close();
Log.i("Database",
"New database has been copied to device!");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال
Salamon
11 سال پیش
0
0
در ضمن اکتیویتی که از اون ایراد میگره هم میزارم. میدهnullpointerexeption
publicclassActivityResultextendsActivity{
@Override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
finalTextView txtName =(TextView) findViewById(R.id.txtName);
finalTextView txtInsta =(TextView) findViewById(R.id.txtInsta);
TextView txtFace =(TextView) findViewById(R.id.txtFace);
TextView txtWiki =(TextView) findViewById(R.id.txtWiki);
TextView txtWeb =(TextView) findViewById(R.id.txtWeb);
ImageView imgshow =(ImageView) findViewById(R.id.imgShow);
finalImageView imgFav =(ImageView) findViewById(R.id.imgFav);
imgFav.setOnClickListener(newOnClickListener(
){
@Override
publicvoid onClick(View arg0){
Bundle extras = getIntent().getExtras();
int position =0;
if(extras !=null){
position = extras.getInt("ID");
}
StructNote note = G.notes.get(position);
int idd = note.id;
String name = txtName.getText().toString();
Cursor cursor = G.database.rawQuery("SELECT * FROM dastanha WHERE onvan='"+ name +"'",null);
while(cursor.moveToNext()){
//String matn = cursor.getString(cursor.getColumnIndex("matn"));
// String id = cursor.getString(cursor.getColumnIndex("id"));
ContentValues cv =newContentValues();
cv.put("fav",1);
G.database.update("dastanha", cv,"id "+"="+ idd,null);
}
cursor.close();
}
});
txtInsta.setOnClickListener(newOnClickListener(){
@Override
publicvoid onClick(View arg0){
String instaLink = txtInsta.getText().toString();
Intent myWebLink =newIntent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse(instaLink));
startActivity(myWebLink);
}
});
Bundle extras = getIntent().getExtras();
int position =0;
if(extras !=null){
position = extras.getInt("ID");
}
StructNote note = G.notes.get(position);
txtName.setText(note.title);
String name = txtName.getText().toString();
Cursor cursor = G.database.rawQuery("SELECT * FROM dastanha WHERE onvan='"+ name +"'",null);
while(cursor.moveToNext()){
String matn = cursor.getString(cursor.getColumnIndex("matn"));
String tasvir = cursor.getString(cursor.getColumnIndex("tasvir"));
int fav = cursor.getInt(cursor.getColumnIndex("fav"));
txtInsta.setText(matn);
SetDrawable(tasvir, imgshow);
if(fav !=0){
imgFav.setImageResource(R.drawable.favon);
}else{
imgFav.setImageResource(R.drawable.favoff);
}
}
cursor.close();
}
publicstaticvoidSetDrawable(String tasvir,ImageView imgshow){
int resID = G.contex.getResources().getIdentifier(tasvir,"drawable", G.contex.getPackageName());
imgshow.setImageResource(resID);
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .