应用程序即将使用工具栏上的按钮保存所有选项卡中的数据....
按钮存储数据以创建用户个人资料
我已经测试了我的数据库....正常工作..还会跟随NullPointerException
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)
答案 0 :(得分:0)
我没有看到你的&#34; activity_profile.xml&#34;。但是我猜你会把每个片段的EditText放在每个片段的单独布局文件中。在这种情况下,您应该在每个片段中找到EditText,而不是在Activity中,因为&#34; onCreateView&#34;除非您选择选项卡,否则不会调用方法,因此您将获得&#34; null&#34;当您在活动中找到该视图时的值。