Mikhail_dev писал(а):Конкретнее говорите что за ошибки.
ок
1.\
Урок
http://developer.alexanderklimov.ru/and ... droid3.php
Ошибка:
Caused by: java.lang.NullPointerException
at com.dwa.natalyatereshchenko.dwa.MyActivity.onCreate(MyActivity.java:22)
[syntax=xml]08-06 14:27:40.880 2946-2946/com.dwa.natalyatereshchenko.dwa E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dwa.natalyatereshchenko.dwa/com.dwa.natalyatereshchenko.dwa.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3770)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.dwa.natalyatereshchenko.dwa.MyActivity.onCreate(MyActivity.java:22)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3770)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
[/syntax]
Коротко: в уроке передается в функцию объект (new OnClickListener().
А у меня получилось (new View.OnClickListener()...
[syntax=java]package com.dwa.natalyatereshchenko.dwa;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity {
private Button mButtonCrowsCounter;
private int mCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mButtonCrowsCounter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText("I counted " + ++mCounter + "crows");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void buttonHello_Click(View view) {
TextView tvHello = (TextView) findViewById(R.id.textView);
tvHello.setText("Hello Kitty!");
}
}
[/syntax]
2.
Урок
http://developer.alexanderklimov.ru/and ... droid2.php
Работа с переменными и ресурсами
Лог
[syntax=xml]08-06 14:44:34.840 4174-4174/com.example.natalyatereshchenko.hellokitty2 D/dalvikvm﹕ GC_EXTERNAL_ALLOC freed 91K, 47% free 2864K/5379K, external 0K/0K, paused 57ms
08-06 14:45:27.570 4457-4457/com.example.natalyatereshchenko.hellokitty2 D/dalvikvm﹕ GC_EXTERNAL_ALLOC freed 91K, 47% free 2864K/5379K, external 0K/0K, paused 50ms
08-06 14:45:27.690 4457-4457/com.example.natalyatereshchenko.hellokitty2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4001e578)
08-06 14:45:27.700 4457-4457/com.example.natalyatereshchenko.hellokitty2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.natalyatereshchenko.hellokitty2/com.example.natalyatereshchenko.hellokitty2.MainActivity}: java.lang.ClassCastException: android.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3770)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.TextView
at com.example.natalyatereshchenko.hellokitty2.MainActivity.onCreate(MainActivity.java:22)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3770)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
08-06 14:45:29.710 4457-4460/com.example.natalyatereshchenko.hellokitty2 I/dalvikvm﹕ threadid=4: reacting to signal 3
08-06 14:45:29.720 4457-4460/com.example.natalyatereshchenko.hellokitty2 I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'[/syntax]
Загадочная линейка 22. Про запуске проекта - сдалась. Приняла как условно работающий.
код MainActivity.java
[syntax=java]package com.example.natalyatereshchenko.hellokitty2;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private TextView mHelloTextView;
private EditText mNameEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelloTextView = (TextView) findViewById(R.id.textView);
mNameEditText = (EditText) findViewById(R.id.editText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view) {
if (mNameEditText.getText().length() == 0) {
mHelloTextView.setText("Hello Kitty!");
} else {
mHelloTextView.setText("Привет, " + mNameEditText.getText());
}
}
}
[/syntax]
на всякий случай build.gradle
[syntax=javascript]pply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.natalyatereshchenko.hellokitty2"
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
[/syntax]