2017-04-06 15 views
-1

sqliteデータベースからいくつかのデータを取得しようとしています - > RecyclerViewに表示 - >私は新しい活動を始めたいと思う。仮想メソッド 'java.lang.String android.content.Context.getPackageName()'をNullobjectreferenceで呼び出そうとしています

が、私は次のエラーがポップアップし、それらの一つをタップしたときに項目のリストを表示した後: のjava.lang.NullPointerException:仮想メソッドを呼び出そうと「はjava.lang.String型android.content.Context.getPackageName( )」ヌルオブジェクト参照に

マイmainActivityClass:

import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteException; 
import android.support.annotation.NonNull; 
import android.support.design.widget.NavigationView; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.MenuItem; 
import android.widget.Toast; 
import com.example.blue2dew.trakin.Item.item; 
import com.example.blue2dew.trakin.MovieAdapter; 


import java.util.ArrayList; 




public class MainActivity extends AppCompatActivity { 
    private DrawerLayout mDrawerLayout; 
private ActionBarDrawerToggle mtoggle; 
private NavigationView navigationView; 
private Toolbar toolbar; 
private Movie_det movie_det; 
Context context; 

private RecyclerView recyclerView; 
private DatabaseHelper databaseHelper; 
ArrayList<item> arrayList=new ArrayList<item>(); 
private Cursor cursor; 
private MovieAdapter movieAdapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    mDrawerLayout=(DrawerLayout) findViewById(R.id.activity_main); 
    navigationView=(NavigationView)findViewById(R.id.navigation_view); 
    toolbar=(Toolbar)findViewById(R.id.nav_action); 
    setSupportActionBar(toolbar); 
    mtoggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close); 

    recyclerView=(RecyclerView)findViewById(R.id.recycler_view); 
    mDrawerLayout.addDrawerListener(mtoggle); 
    mtoggle.syncState(); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      item.setChecked(true); 
      mDrawerLayout.closeDrawers(); 
      return false; 
     } 
    }); 

    loadDatabase(); 

} 


@Override 
public boolean onOptionsItemSelected(MenuItem item){ 
    if(mtoggle.onOptionsItemSelected(item)){ 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

public void loadDatabase(){ 
    databaseHelper=new DatabaseHelper(MainActivity.this); 

    try{ 
     databaseHelper.checkAndCopyDatabase(); 
     databaseHelper.openDatabase(); 
    }catch(SQLiteException e){ 
     e.printStackTrace(); 
    } 


    try { 
     cursor = databaseHelper.QueryData("select * from movieDetails"); 
     if (cursor != null) { 
      if (cursor.moveToFirst()) { 
       do { 
        item itemv = new item(); 
        itemv.setMovieName(cursor.getString(2)); 
        itemv.setReleaseDate(cursor.getString(5)); 
        itemv.setMovieImage(cursor.getBlob(1)); 

        arrayList.add(itemv); 


       } while (cursor.moveToNext()); 
      } 
     } 
    }catch (SQLiteException e){ 
     e.printStackTrace(); 
    } 


    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getApplicationContext()); 
    movieAdapter=new MovieAdapter(MainActivity.this,context,arrayList); 



    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(linearLayoutManager); 
    recyclerView.setAdapter(movieAdapter); 

} 



} 

マイアダプタクラス:

package com.example.blue2dew.trakin; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 


import com.example.blue2dew.trakin.Item.item; 


import java.util.Collections; 
import java.util.List; 



public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> { 
    Activity activity; 
    List<item> items= Collections.emptyList(); 
    Context context; 


public MovieAdapter(Activity activity,Context context, List<item> items){ 
    this.activity=activity; 
    this.context=context; 
    this.items=items; 
} 
@Override 
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_list_item,parent,false); 

    MovieViewHolder movieViewHolder=new MovieViewHolder(view,context,items); 
    return movieViewHolder; 
} 



public void onBindViewHolder(MovieViewHolder holder,final int position) { 
    holder.movie_name.setText(items.get(position).getMovieName()); 
    holder.release_date.setText(items.get(position).getReleaseDate()); 
    holder.imageView.setImageBitmap(items.get(position).getMovieImage()); 




} 

@Override 
public int getItemCount() { 
    return items.size(); 
} 




public class MovieViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 


    ImageView md_image; 
    TextView md_movie_name; 
    TextView md_length1; 
    TextView md_genre1; 
    TextView md_release_date1; 
    ImageView md_imdb_icon; 
    TextView md_rating1; 
    TextView md_director1; 
    TextView md_synopsis1; 

    TextView movie_name; 
    TextView release_date; 
    ImageView imageView; 

    List<item> items=Collections.emptyList(); 
    Context context; 

    public MovieViewHolder(View itemView,Context context,List<item> items) { 
     super(itemView); 
     this.context=context; 
     this.items=items; 
     itemView.setOnClickListener(this); 
     md_image = (ImageView) itemView.findViewById(R.id.md_image); 
     md_director1 = (TextView) itemView.findViewById(R.id.md_director1); 
     md_genre1 = (TextView) itemView.findViewById(R.id.md_genre1); 
     md_movie_name = (TextView) itemView.findViewById(R.id.md_movie_name); 
     md_length1 = (TextView) itemView.findViewById(R.id.md_length1); 
     md_release_date1 = (TextView) itemView.findViewById(R.id.md_release_date1); 
     md_rating1 = (TextView) itemView.findViewById(R.id.md_rating1); 
     md_synopsis1 = (TextView)itemView.findViewById(R.id.md_synopsis1); 
     md_imdb_icon = (ImageView) itemView.findViewById(R.id.md_imdb_icon); 

     movie_name=(TextView) itemView.findViewById(R.id.movie_name); 
     release_date=(TextView) itemView.findViewById(R.id.release_date); 
     imageView=(ImageView) itemView.findViewById(R.id.imageView); 


    } 


    @Override 
    public void onClick(View v) { 

     int position=getAdapterPosition(); 
     item item=this.items.get(position); 
     Intent intent=new Intent (this.context,Main2Activity.class); 
     this.context.startActivity(intent); 

    } 
} 


} 

マイデータベースヘルパークラス: パッケージcom.example.blue2dew.trakin;

import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.os.Build; 
import android.os.Environment; 
import android.util.Log; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 



public class DatabaseHelper extends SQLiteOpenHelper { 
private static String DB_Name="movie.db"; 
private static String DB_Path; 
private SQLiteDatabase mydatabase; 
private final Context mycontext; 




public DatabaseHelper(Context context) { 
    super(context,DB_Name , null, 1); 
    if(Build.VERSION.SDK_INT>=15){ 
     DB_Path=context.getApplicationInfo().dataDir+"/databases/"; 
    } 
    else{ 
     DB_Path= Environment.getDataDirectory()+"/data/"+context.getPackageName()+"/databases/"; 
    } 
    this.mycontext=context; 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    //using existing database so creation not needed 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    //the existing database need not be upgraded 

} 

public void checkAndCopyDatabase(){ 
    boolean dbExist=checkDatabase(); 
    if(dbExist){ 
     Log.d("TAG","database already exists"); 
    } 
    else { 
     this.getReadableDatabase(); 
    } 
    try { 
     copyDatabase(); 
    } 
    catch (IOException e){ 
     e.printStackTrace(); 
     Log.d("TAG","Error Copy Database"); 
    } 
} 

public boolean checkDatabase(){ 
    SQLiteDatabase checkDB=null; 
    try { 
     String mypath = DB_Path + DB_Name; 
     checkDB = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE); 
    }catch (SQLiteException e) { 
     e.printStackTrace(); 

    } 
    if(checkDB!=null){ 
     checkDB.close(); 
    } 
    return checkDB!=null?true:false; 

} 

public void copyDatabase() throws IOException{ 
    InputStream myInput=mycontext.getAssets().open(DB_Name); 
    String outfilename=DB_Path+DB_Name; 
    OutputStream myOutput=new FileOutputStream(outfilename); 
    byte[] buffer=new byte[1024]; 
    int length; 
    while ((length=myInput.read(buffer))>0){ 
     myOutput.write(buffer,0,length); 
    } 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 
} 

public void openDatabase(){ 
    String myPath=DB_Path+DB_Name; 
    mydatabase=SQLiteDatabase.openDatabase(myPath,null, SQLiteDatabase.OPEN_READWRITE); 
} 

public synchronized void close(){ 
    if(mydatabase!=null){ 
     mydatabase.close(); 
    } 
    super.close(); 
} 

public Cursor QueryData(String query){ 
    return mydatabase.rawQuery(query,null); 
} 
} 

エラー:あなたはこれを行うことができますいずれか

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.blue2dew.trakin, PID: 7430 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 
at android.content.ComponentName.(ComponentName.java:128) 
at android.content.Intent.(Intent.java:4521) at com.example.blue2dew.trakin.MovieAdapter$MovieViewHolder.onClick(MovieAdapter.java:127) 
at android.view.View.performClick(View.java:5209) 
at android.view.View$PerformClick.run(View.java:21179) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5437) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

おそらく無関係:あなたが得ることができたときに、ビューの所有者およびアダプタにコンテキストおよび活性を持っていませんビューからのコンテキスト。 –

+0

このNPEはAndroidの仕組みが理解できないために再オープンされました。 [提案されたオリジナルの質問](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)はあまりにも一般的であり、この場合は役に立ちません。 –

+0

私の悪い!私はアンドロイドの学習を始めたばかりです。返信いただきありがとうございます!コンテキストを取得するために使用されたビュー。 – user55572

答えて

0

@Override 
    public void onClick(View v) { 
    int position=getAdapterPosition(); 
    item item=this.items.get(position); 
    Intent intent=new Intent (v.getContext(),Main2Activity.class); 
    v.getContext().startActivity(intent); 
} 
+0

ありがとうございました! – user55572

関連する問題

 関連する問題