在应用程序内部具有两个活动的清单的代码。
AnroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nexus.movierate">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DirectoryName"></activity>
</application>
</manifest>
mainactivity包含布局和按钮单击事件,它将用于MovieRatingDataHelper.java,还会在insert()方法内插入代码污染数据。
MainActivity.java
package com.example.nexus.movierate;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.content.Intent;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends AppCompatActivity implements
RatingBar.OnRatingBarChangeListener {
private EditText moviename;
private EditText userReview;
private EditText releaseYear;
private EditText movieDuration;
private RatingBar yourRating;
private EditText movieStarring;
private EditText movieDirector;
private Button mSaveButton;
private Spinner mGeners;
String name;
String year;
String duration;
String review;
String director;
String starcast;
double rating;
String genre;
SQLiteOpenHelper openHelper;
SQLiteDatabase db;
private ArrayAdapter <String> mAdapter;
MovieRatingDataHelper mDataHelper;
/*Array containing the genres to which a movie can belong*/
private String[] mGenresData = {
"-Select Genre-",
"Action", "Adventure", "Animation", "Biography", "Comedy", "Crime", "Documentry", "Drama", "Family", "Fantasy", "FilmNoir",
"Game-Show", "History", "Horror", "Music", "Musical", "Mystery", "News", "RealityTV", "Romance", "Scifi", "Sport", "TalkShow",
"Thriller", "War", "Western"};
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
/* Initialized variable */
moviename = (EditText) findViewById( R.id.editText2 );
userReview = (EditText) findViewById( R.id.editText1 );
releaseYear = (EditText) findViewById( R.id.editText3 );
movieDuration = (EditText) findViewById( R.id.editText4 );
yourRating = (RatingBar) findViewById( R.id.ratingBar1 );
movieStarring = (EditText) findViewById( R.id.editText5 );
movieDirector = (EditText) findViewById( R.id.editText6 );
mSaveButton = (Button) findViewById( R.id.button1 );
yourRating.setOnRatingBarChangeListener( this );
mGeners = (Spinner) findViewById( R.id.spinner1 );
// mDataHelper = new MovieRatingDataHelper(this);
mAdapter = new ArrayAdapter <String>( this, android.R.layout.simple_spinner_item, mGenresData );
mAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
mGeners.setAdapter( mAdapter );
movieDirector.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View arg0 ) {
//TODO Auto - generated method stub
Intent mAutoCompleteIntent = new Intent( MainActivity.this, DirectoryName.class );
startActivityForResult( mAutoCompleteIntent, 0123456 );
}
} );
mSaveButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View arg0 ) {
db=openHelper.getWritableDatabase();
name = moviename.getText().toString();
year = releaseYear.getText().toString();
duration = movieDuration.getText().toString();
review = userReview.getText().toString();
director = movieDirector.getText().toString();
starcast = movieStarring.getText().toString();
rating = yourRating.getRating();
genre = mGeners.getSelectedItem().toString();
if (mGeners.getSelectedItem().toString().equalsIgnoreCase( "-Select Genre-" )) {
Toast.makeText( MainActivity.this, "Please Select valid Genre", Toast.LENGTH_SHORT ).show();
return;
}
/*Check if any of the values is blank.Display a message accordingly */
if (year.equalsIgnoreCase( "" ) && duration.equalsIgnoreCase( "" )
&& starcast.equalsIgnoreCase( "" ) || name.equalsIgnoreCase( "" )
|| director.equalsIgnoreCase( "" ) || review.equalsIgnoreCase( "" )) {
Toast.makeText( MainActivity.this, "Please fill all the fields.", Toast.LENGTH_SHORT ).show();
} else {
Toast.makeText( MainActivity.this, "Validation successful", Toast.LENGTH_SHORT ).show();
Toast.makeText( MainActivity.this, "MOVIE NAME IS " + rating, Toast.LENGTH_LONG ).show();
insert(name,genre,year,duration,rating,review,starcast,director);
Intent mReviewListIntent = new Intent( MainActivity.this, ReviewListActivity.class );
startActivity(mReviewListIntent);
}
}
} );
}
private void insert( String name, String genre, String year, String duration, double rating, String review, String starcast, String director ) {
try
{
ContentValues cv = new ContentValues();
cv.put(MovieRatingDataHelper.V_moviename,name);
cv.put(MovieRatingDataHelper.V_genre,genre);
cv.put(MovieRatingDataHelper.V_year,year);
cv.put(MovieRatingDataHelper.V_duration,duration);
cv.put(MovieRatingDataHelper.V_rate,rating);
cv.put(MovieRatingDataHelper.V_review,review);
cv.put(MovieRatingDataHelper.V_starring,starcast);
cv.put(MovieRatingDataHelper.V_director,director);
db.insertOrThrow(MovieRatingDataHelper.Table_Name, null,cv);
// sqlDB.execSQL("INSERT INTO movie_rate(moviename,genre,year,duration,rate,review,starring,director) values (name,genre,year,duration,4,review,starcast,director)");
Toast.makeText(MainActivity.this,"Data Successfully Inserted", Toast.LENGTH_SHORT).show();
}
catch (Exception ex)
{
Log.e("Issue" , ex.toString());
}
}
/*protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try
{
if((requestCode == 0123456) && (resultCode == Activity.RESULT_OK))
{
Bundle myResults = data.getExtras();
String vresult = myResults.getString("key");
movieDirector.setText(vresult);
}
}
catch(Exception e)
{
movieDirector.setText("Oops! - " + requestCode + "" + resultCode);
}
}
*/
@Override
public void onRatingChanged( RatingBar ratingBar, float arg1, boolean fromUser ) {
yourRating.setRating( arg1 );
}
}
MovieRatingDataHelper.java
package com.example.nexus.movierate;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
class MovieRatingDataHelper extends SQLiteOpenHelper
{
public static final String DataBase_Name = "MyDB.db";
public static final String Table_Name = "movie_rate";
public static final String V_moviename = "moviename";
public static final String V_genre = "genre";
public static final String V_year = "year";
public static final String V_duration = "duration";
public static final String V_rate = "rate";
public static final String V_review = "review";
public static final String V_starring = "starring";
public static final String V_director = "director";
public static final int DataBase_Version = 1;
public MovieRatingDataHelper( Context context )
{
super( context, DataBase_Name, null, DataBase_Version );
}
@Override
public void onCreate( SQLiteDatabase db )
{
db.execSQL("Create Table "+Table_Name+"(moviename TEXT,genre TEXT,year INT,duration INT,rate INT,review TEXT,starring TEXT,director TEXT)");
}
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion )
{
db.execSQL(" DROP TABLE IF EXISTS "+Table_Name);
onCreate(db);
}
}
我已经实现了movieRate应用,其中已插入了电影名称,评分,导演名称,主演等数据。 我正在使用android studio,并且无法在android studio ADM中找到数据库和表,因此我在android studio中没有tools / android / ADM选项。我在哪里可以找到该选项,也无法正确插入数据...
我在运行应用程序时发现此错误,并且应用程序停止运行。
java.lang.NullPointerException:尝试调用虚拟方法 'android.database.sqlite.SQLiteDatabase android.database.sqlite.SQLiteOpenHelper.getWritableDatabase()' 空对象引用
答案 0 :(得分:2)
无论您是使用移动设备还是在仿真器上工作,数据库文件都存储在路径“ /data/data/[your_package_name]/databases
”中。
每当您动态创建数据库时,它实际上都会创建两个文件,一个是database
文件,第二个是-journal
文件。
答案 1 :(得分:1)
在Android Studio中,您可以找到您的设备。在底部的右侧。您将看到Device File Explorer
请参见下图
在data/data/
文件夹下的找到您的包和数据库文件夹。您可以将该数据库保存在本地系统中。
您还需要一个SQLite浏览器来读取数据库。
SQLite浏览器Download链接。
答案 2 :(得分:0)
如果像我这样的设备浏览器窗口中存在一些问题(通常发生在三星手机中),建议您使用Debug-Db库。链接:https://github.com/amitshekhariitbhu/Android-Debug-Database