アプリケーションは、ツールバー上に存在ボタンですべてのタブからのデータを保存しようとしている....セーブデータ
ボタンストアのデータ、ユーザプロファイル
を作成するために、私は私のデータベースをテストしています...取得次のNullPointerException細かい..stillを.working
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class PROFILE extends AppCompatActivity {
EditText etname,etdate,etmarital_status,etcity,etemail,etaddress,etqualification,
etobjective,etproject,etachievement,etwork;
Button svbtn;
String name;
String date;
String marital_status;
String city;
String email;
String address;
String qualification;
String objective;
String project;
String achievement;
String work;
Context ctx = this;
Context CTX = this;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
etname = (EditText) findViewById(R.id.name);
etdate = (EditText) findViewById(R.id.dob);
etmarital_status = (EditText) findViewById(R.id.ms);
etcity = (EditText) findViewById(R.id.city);
etemail = (EditText) findViewById(R.id.email);
etaddress = (EditText) findViewById(R.id.address);
etqualification = (EditText) findViewById(R.id.qualification);
etobjective = (EditText) findViewById(R.id.objective);
etproject = (EditText) findViewById(R.id.project);
etachievement = (EditText) findViewById(R.id.achevement);
etwork = (EditText) findViewById(R.id.work);
svbtn = (Button) findViewById(R.id.save);
//button to save data of all tabs
svbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// converting EditText data to string
name = etname.getText().toString();
city = etcity.getText().toString();
email = etemail.getText().toString();
address = etaddress.getText().toString();
qualification = etqualification.getText().toString();
objective = etobjective.getText().toString();
project = etproject.getText().toString();
achievement = etachievement.getText().toString();
work = etwork.getText().toString();
marital_status = etmarital_status.toString();
//calling database DatabaseHandler
DatabaseHandler handler = new DatabaseHandler(getApplicationContext());
handler.putInformation(handler,name,date,marital_status,city,email,address,objective,project,achievement,work);
Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG);
}
});
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new INFORMATION(), "GENERAL");
adapter.addFragment(new QUALIFICATION(), "QUALIFICATION");
adapter.addFragment(new OBJECTIVE(), "OBJECTIVE");
adapter.addFragment(new PROJECTS(), "PROJECTS");
adapter.addFragment(new ACHIEVEMENTS(), "ACHIEVEMENTS");
adapter.addFragment(new WORK(), "WORK");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
}
エラーログ:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vikas.renaz, PID: 17785
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.vikas.renaz.PROFILE$1.onClick(PROFILE.java:68)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
あなたは 'Edittext'要素' etname'を取得していないかもしれません。私たちにあなたのXMLコードも表示してください –